Zend_Db_Adapter.xml 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.adapter">
  4. <title>Zend_Db_Adapter</title>
  5. <para>
  6. <classname>Zend_Db</classname> and its related classes provide a simple
  7. <acronym>SQL</acronym> database interface for Zend Framework. The
  8. <classname>Zend_Db_Adapter</classname> is the basic class you use to connect your
  9. <acronym>PHP</acronym> application to an <acronym>RDBMS</acronym>. There is a different
  10. Adapter class for each brand of <acronym>RDBMS</acronym>.
  11. </para>
  12. <para>
  13. The <classname>Zend_Db</classname> adapters create a bridge from the vendor-specific
  14. <acronym>PHP</acronym> extensions to a common interface to help you write
  15. <acronym>PHP</acronym> applications once and deploy with multiple brands of
  16. <acronym>RDBMS</acronym> with very little effort.
  17. </para>
  18. <para>
  19. The interface of the adapter class is similar to the interface of the
  20. <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink> extension.
  21. <classname>Zend_Db</classname> provides Adapter classes to <acronym>PDO</acronym> drivers
  22. for the following <acronym>RDBMS</acronym> brands:
  23. </para>
  24. <itemizedlist>
  25. <listitem>
  26. <para>
  27. IBM DB2 and Informix Dynamic Server (IDS), using the
  28. <ulink url="http://www.php.net/pdo-ibm">pdo_ibm</ulink> <acronym>PHP</acronym>
  29. extension
  30. </para>
  31. </listitem>
  32. <listitem>
  33. <para>
  34. MySQL, using the <ulink url="http://www.php.net/pdo-mysql">pdo_mysql</ulink>
  35. <acronym>PHP</acronym> extension
  36. </para>
  37. </listitem>
  38. <listitem>
  39. <para>
  40. Microsoft <acronym>SQL</acronym> Server, using the <ulink
  41. url="http://www.php.net/pdo-dblib">pdo_dblib</ulink> <acronym>PHP</acronym>
  42. extension
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. Oracle, using the <ulink url="http://www.php.net/pdo-oci">pdo_oci</ulink>
  48. <acronym>PHP</acronym> extension
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. PostgreSQL, using the <ulink url="http://www.php.net/pdo-pgsql">pdo_pgsql</ulink>
  54. <acronym>PHP</acronym> extension
  55. </para>
  56. </listitem>
  57. <listitem>
  58. <para>
  59. SQLite, using the <ulink url="http://www.php.net/pdo-sqlite">pdo_sqlite</ulink>
  60. <acronym>PHP</acronym> extension
  61. </para>
  62. </listitem>
  63. </itemizedlist>
  64. <para>
  65. In addition, <classname>Zend_Db</classname> provides Adapter classes that utilize
  66. <acronym>PHP</acronym> database extensions for the following <acronym>RDBMS</acronym>
  67. brands:
  68. </para>
  69. <itemizedlist>
  70. <listitem>
  71. <para>
  72. MySQL, using the <ulink url="http://www.php.net/mysqli">mysqli</ulink>
  73. <acronym>PHP</acronym> extension
  74. </para>
  75. </listitem>
  76. <listitem>
  77. <para>
  78. Oracle, using the <ulink url="http://www.php.net/oci8">oci8</ulink>
  79. <acronym>PHP</acronym> extension
  80. </para>
  81. </listitem>
  82. <listitem>
  83. <para>
  84. IBM DB2 and DB2/i5, using the <ulink
  85. url="http://www.php.net/ibm_db2">ibm_db2</ulink> <acronym>PHP</acronym>
  86. extension
  87. </para>
  88. </listitem>
  89. <listitem>
  90. <para>
  91. Firebird/Interbase, using the <ulink
  92. url="http://www.php.net/ibase">php_interbase</ulink> <acronym>PHP</acronym>
  93. extension
  94. </para>
  95. </listitem>
  96. </itemizedlist>
  97. <note>
  98. <para>
  99. Each <classname>Zend_Db</classname> Adapter uses a <acronym>PHP</acronym> extension. You
  100. must have the respective <acronym>PHP</acronym> extension enabled in your
  101. <acronym>PHP</acronym> environment to use a <classname>Zend_Db</classname> Adapter. For
  102. example, if you use any of the <acronym>PDO</acronym> <classname>Zend_Db</classname>
  103. Adapters, you need to enable both the <acronym>PDO</acronym> extension and the
  104. <acronym>PDO</acronym> driver for the brand of <acronym>RDBMS</acronym> you use.
  105. </para>
  106. </note>
  107. <sect2 id="zend.db.adapter.connecting">
  108. <title>Connecting to a Database Using an Adapter</title>
  109. <para>
  110. This section describes how to create an instance of a database Adapter.
  111. This corresponds to making a connection to your <acronym>RDBMS</acronym> server from
  112. your <acronym>PHP</acronym> application.
  113. </para>
  114. <sect3 id="zend.db.adapter.connecting.constructor">
  115. <title>Using a Zend_Db Adapter Constructor</title>
  116. <para>
  117. You can create an instance of an adapter using its constructor.
  118. An adapter constructor takes one argument, which is an array
  119. of parameters used to declare the connection.
  120. </para>
  121. <example id="zend.db.adapter.connecting.constructor.example">
  122. <title>Using an Adapter Constructor</title>
  123. <programlisting language="php"><![CDATA[
  124. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  125. 'host' => '127.0.0.1',
  126. 'username' => 'webuser',
  127. 'password' => 'xxxxxxxx',
  128. 'dbname' => 'test'
  129. ));
  130. ]]></programlisting>
  131. </example>
  132. </sect3>
  133. <sect3 id="zend.db.adapter.connecting.factory">
  134. <title>Using the Zend_Db Factory</title>
  135. <para>
  136. As an alternative to using an adapter constructor directly, you
  137. can create an instance of an adapter using the static method
  138. <methodname>Zend_Db::factory()</methodname>. This method dynamically loads
  139. the adapter class file on demand using
  140. <link linkend="zend.loader.load.class">Zend_Loader::loadClass()</link>.
  141. </para>
  142. <para>
  143. The first argument is a string that names the base name of the
  144. adapter class. For example the string '<classname>Pdo_Mysql</classname>' corresponds
  145. to the class <classname>Zend_Db_Adapter_Pdo_Mysql</classname>. The second argument
  146. is the same array of parameters you would have given to the
  147. adapter constructor.
  148. </para>
  149. <example id="zend.db.adapter.connecting.factory.example">
  150. <title>Using the Adapter Factory Method</title>
  151. <programlisting language="php"><![CDATA[
  152. // We don't need the following statement because the
  153. // Zend_Db_Adapter_Pdo_Mysql file will be loaded for us by the Zend_Db
  154. // factory method.
  155. // require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  156. // Automatically load class Zend_Db_Adapter_Pdo_Mysql
  157. // and create an instance of it.
  158. $db = Zend_Db::factory('Pdo_Mysql', array(
  159. 'host' => '127.0.0.1',
  160. 'username' => 'webuser',
  161. 'password' => 'xxxxxxxx',
  162. 'dbname' => 'test'
  163. ));
  164. ]]></programlisting>
  165. </example>
  166. <para>
  167. If you create your own class that extends
  168. <classname>Zend_Db_Adapter_Abstract</classname>, but you do not name your
  169. class with the "<classname>Zend_Db_Adapter</classname>" package prefix, you can use
  170. the <methodname>factory()</methodname> method to load your adapter if you
  171. specify the leading portion of the adapter class with the
  172. 'adapterNamespace' key in the parameters array.
  173. </para>
  174. <example id="zend.db.adapter.connecting.factory.example2">
  175. <title>Using the Adapter Factory Method for a Custom Adapter Class</title>
  176. <programlisting language="php"><![CDATA[
  177. // We don't need to load the adapter class file
  178. // because it will be loaded for us by the Zend_Db factory method.
  179. // Automatically load class MyProject_Db_Adapter_Pdo_Mysql and create
  180. // an instance of it.
  181. $db = Zend_Db::factory('Pdo_Mysql', array(
  182. 'host' => '127.0.0.1',
  183. 'username' => 'webuser',
  184. 'password' => 'xxxxxxxx',
  185. 'dbname' => 'test',
  186. 'adapterNamespace' => 'MyProject_Db_Adapter'
  187. ));
  188. ]]></programlisting>
  189. </example>
  190. </sect3>
  191. <sect3 id="zend.db.adapter.connecting.factory-config">
  192. <title>Using Zend_Config with the Zend_Db Factory</title>
  193. <para>
  194. Optionally, you may specify either argument of the
  195. <methodname>factory()</methodname> method as an object of type
  196. <link linkend="zend.config">Zend_Config</link>.
  197. </para>
  198. <para>
  199. If the first argument is a config object, it is expected to
  200. contain a property named <property>adapter</property>, containing the
  201. string naming the adapter class name base. Optionally, the object
  202. may contain a property named <property>params</property>, with
  203. subproperties corresponding to adapter parameter names.
  204. This is used only if the second argument of the
  205. <methodname>factory()</methodname> method is absent.
  206. </para>
  207. <example id="zend.db.adapter.connecting.factory.example1">
  208. <title>Using the Adapter Factory Method with a Zend_Config Object</title>
  209. <para>
  210. In the example below, a <classname>Zend_Config</classname> object is created
  211. from an array. You can also load data from an external file using classes such
  212. as <link linkend="zend.config.adapters.ini">Zend_Config_Ini</link>
  213. and <link linkend="zend.config.adapters.xml">Zend_Config_Xml</link>.
  214. </para>
  215. <programlisting language="php"><![CDATA[
  216. $config = new Zend_Config(
  217. array(
  218. 'database' => array(
  219. 'adapter' => 'Mysqli',
  220. 'params' => array(
  221. 'host' => '127.0.0.1',
  222. 'dbname' => 'test',
  223. 'username' => 'webuser',
  224. 'password' => 'secret',
  225. )
  226. )
  227. )
  228. );
  229. $db = Zend_Db::factory($config->database);
  230. ]]></programlisting>
  231. </example>
  232. <para>
  233. The second argument of the <methodname>factory()</methodname> method may be
  234. an associative array containing entries corresponding to
  235. adapter parameters. This argument is optional. If the first
  236. argument is of type <classname>Zend_Config</classname>, it is assumed to contain all
  237. parameters, and the second argument is ignored.
  238. </para>
  239. </sect3>
  240. <sect3 id="zend.db.adapter.connecting.parameters">
  241. <title>Adapter Parameters</title>
  242. <para>
  243. The following list explains common parameters recognized by
  244. <classname>Zend_Db</classname> Adapter classes.
  245. </para>
  246. <itemizedlist>
  247. <listitem>
  248. <para>
  249. <emphasis>host</emphasis>:
  250. a string containing a hostname or IP address of the
  251. database server. If the database is running on the
  252. same host as the <acronym>PHP</acronym> application, you may use
  253. 'localhost' or '127.0.0.1'.
  254. </para>
  255. </listitem>
  256. <listitem>
  257. <para>
  258. <emphasis>username</emphasis>:
  259. account identifier for authenticating a connection to the
  260. <acronym>RDBMS</acronym> server.
  261. </para>
  262. </listitem>
  263. <listitem>
  264. <para>
  265. <emphasis>password</emphasis>:
  266. account password credential for authenticating a
  267. connection to the <acronym>RDBMS</acronym> server.
  268. </para>
  269. </listitem>
  270. <listitem>
  271. <para>
  272. <emphasis>dbname</emphasis>:
  273. database instance name on the <acronym>RDBMS</acronym> server.
  274. </para>
  275. </listitem>
  276. <listitem>
  277. <para>
  278. <emphasis>port</emphasis>:
  279. some <acronym>RDBMS</acronym> servers can accept network connections on a
  280. administrator-specified port number. The port
  281. parameter allow you to specify the port to which your
  282. <acronym>PHP</acronym> application connects, to match the port configured
  283. on the <acronym>RDBMS</acronym> server.
  284. </para>
  285. </listitem>
  286. <listitem>
  287. <para>
  288. <emphasis>charset</emphasis>:
  289. specify the charset used for the connection.
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <emphasis>options</emphasis>:
  295. this parameter is an associative array of options
  296. that are generic to all <classname>Zend_Db_Adapter</classname> classes.
  297. </para>
  298. </listitem>
  299. <listitem>
  300. <para>
  301. <emphasis>driver_options</emphasis>:
  302. this parameter is an associative array of additional
  303. options that are specific to a given database
  304. extension. One typical use of this parameter is to
  305. set attributes of a <acronym>PDO</acronym> driver.
  306. </para>
  307. </listitem>
  308. <listitem>
  309. <para>
  310. <emphasis>adapterNamespace</emphasis>:
  311. names the initial part of the class name for the
  312. adapter, instead of '<classname>Zend_Db_Adapter</classname>'. Use this if
  313. you need to use the <methodname>factory()</methodname> method to
  314. load a non-Zend database adapter class.
  315. </para>
  316. </listitem>
  317. </itemizedlist>
  318. <example id="zend.db.adapter.connecting.parameters.example1">
  319. <title>Passing the Case-Folding Option to the Factory</title>
  320. <para>
  321. You can specify this option by the constant
  322. <constant>Zend_Db::CASE_FOLDING</constant>.
  323. This corresponds to the <constant>ATTR_CASE</constant> attribute in
  324. <acronym>PDO</acronym> and IBM DB2 database drivers, adjusting the case of
  325. string keys in query result sets. The option takes values
  326. <constant>Zend_Db::CASE_NATURAL</constant> (the default),
  327. <constant>Zend_Db::CASE_UPPER</constant>, and
  328. <constant>Zend_Db::CASE_LOWER</constant>.
  329. </para>
  330. <programlisting language="php"><![CDATA[
  331. $options = array(
  332. Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
  333. );
  334. $params = array(
  335. 'host' => '127.0.0.1',
  336. 'username' => 'webuser',
  337. 'password' => 'xxxxxxxx',
  338. 'dbname' => 'test',
  339. 'options' => $options
  340. );
  341. $db = Zend_Db::factory('Db2', $params);
  342. ]]></programlisting>
  343. </example>
  344. <example id="zend.db.adapter.connecting.parameters.example2">
  345. <title>Passing the Auto-Quoting Option to the Factory</title>
  346. <para>
  347. You can specify this option by the constant
  348. <constant>Zend_Db::AUTO_QUOTE_IDENTIFIERS</constant>. If the value
  349. is <constant>TRUE</constant> (the default), identifiers like table
  350. names, column names, and even aliases are delimited in all
  351. <acronym>SQL</acronym> syntax generated by the Adapter object. This makes it
  352. simple to use identifiers that contain <acronym>SQL</acronym> keywords, or
  353. special characters. If the value is <constant>FALSE</constant>,
  354. identifiers are not delimited automatically. If you need
  355. to delimit identifiers, you must do so yourself using the
  356. <methodname>quoteIdentifier()</methodname> method.
  357. </para>
  358. <programlisting language="php"><![CDATA[
  359. $options = array(
  360. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  361. );
  362. $params = array(
  363. 'host' => '127.0.0.1',
  364. 'username' => 'webuser',
  365. 'password' => 'xxxxxxxx',
  366. 'dbname' => 'test',
  367. 'options' => $options
  368. );
  369. $db = Zend_Db::factory('Pdo_Mysql', $params);
  370. ]]></programlisting>
  371. </example>
  372. <example id="zend.db.adapter.connecting.parameters.example3">
  373. <title>Passing PDO Driver Options to the Factory</title>
  374. <programlisting language="php"><![CDATA[
  375. $pdoParams = array(
  376. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  377. );
  378. $params = array(
  379. 'host' => '127.0.0.1',
  380. 'username' => 'webuser',
  381. 'password' => 'xxxxxxxx',
  382. 'dbname' => 'test',
  383. 'driver_options' => $pdoParams
  384. );
  385. $db = Zend_Db::factory('Pdo_Mysql', $params);
  386. echo $db->getConnection()
  387. ->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
  388. ]]></programlisting>
  389. </example>
  390. <example id="zend.db.adapter.connecting.parameters.example4">
  391. <title>Passing Serialization Options to the Factory</title>
  392. <programlisting language="php"><![CDATA[
  393. $options = array(
  394. Zend_Db::ALLOW_SERIALIZATION => false
  395. );
  396. $params = array(
  397. 'host' => '127.0.0.1',
  398. 'username' => 'webuser',
  399. 'password' => 'xxxxxxxx',
  400. 'dbname' => 'test',
  401. 'options' => $options
  402. );
  403. $db = Zend_Db::factory('Pdo_Mysql', $params);
  404. ]]></programlisting>
  405. </example>
  406. </sect3>
  407. <sect3 id="zend.db.adapter.connecting.getconnection">
  408. <title>Managing Lazy Connections</title>
  409. <para>
  410. Creating an instance of an Adapter class does not immediately
  411. connect to the <acronym>RDBMS</acronym> server. The Adapter saves the connection
  412. parameters, and makes the actual connection on demand, the
  413. first time you need to execute a query. This ensures that
  414. creating an Adapter object is quick and inexpensive. You can
  415. create an instance of an Adapter even if you are not certain
  416. that you need to run any database queries during the current
  417. request your application is serving.
  418. </para>
  419. <para>
  420. If you need to force the Adapter to connect to the <acronym>RDBMS</acronym>, use
  421. the <methodname>getConnection()</methodname> method. This method returns
  422. an object for the connection as represented by the respective
  423. <acronym>PHP</acronym> database extension. For example, if you use any of the
  424. Adapter classes for <acronym>PDO</acronym> drivers, then
  425. <methodname>getConnection()</methodname> returns the <acronym>PDO</acronym> object,
  426. after initiating it as a live connection to the specific database.
  427. </para>
  428. <para>
  429. It can be useful to force the connection if you want to catch
  430. any exceptions it throws as a result of invalid account
  431. credentials, or other failure to connect to the <acronym>RDBMS</acronym> server.
  432. These exceptions are not thrown until the connection is made,
  433. so it can help simplify your application code if you handle the
  434. exceptions in one place, instead of at the time of
  435. the first query against the database.
  436. </para>
  437. <para>
  438. Additionally, an adapter can get serialized to store it, for example,
  439. in a session variable. This can be very useful not only for the
  440. adapter itself, but for other objects that aggregate it, like a
  441. <classname>Zend_Db_Select</classname> object. By default, adapters are allowed
  442. to be serialized, if you don't want it, you should consider passing the
  443. <constant>Zend_Db::ALLOW_SERIALIZATION</constant> option with
  444. <constant>FALSE</constant>, see the example above. To respect lazy connections
  445. principle, the adapter won't reconnect itself after being unserialized. You must
  446. then call <methodname>getConnection()</methodname> yourself. You can make the
  447. adapter auto-reconnect by passing the
  448. <constant>Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE</constant> with
  449. <constant>TRUE</constant> as an adapter option.
  450. </para>
  451. <example id="zend.db.adapter.connecting.getconnection.example">
  452. <title>Handling Connection Exceptions</title>
  453. <programlisting language="php"><![CDATA[
  454. try {
  455. $db = Zend_Db::factory('Pdo_Mysql', $parameters);
  456. $db->getConnection();
  457. } catch (Zend_Db_Adapter_Exception $e) {
  458. // perhaps a failed login credential, or perhaps the RDBMS is not running
  459. } catch (Zend_Exception $e) {
  460. // perhaps factory() failed to load the specified Adapter class
  461. }
  462. ]]></programlisting>
  463. </example>
  464. </sect3>
  465. </sect2>
  466. <sect2 id="zend.db.adapter.example-database">
  467. <title>Example Database</title>
  468. <para>
  469. In the documentation for <classname>Zend_Db</classname> classes, we use a set of simple
  470. tables to illustrate usage of the classes and methods. These
  471. example tables could store information for tracking bugs in a
  472. software development project. The database contains four tables:
  473. </para>
  474. <itemizedlist>
  475. <listitem>
  476. <para>
  477. <emphasis>accounts</emphasis> stores
  478. information about each user of the bug-tracking database.
  479. </para>
  480. </listitem>
  481. <listitem>
  482. <para>
  483. <emphasis>products</emphasis> stores
  484. information about each product for which a bug can be
  485. logged.
  486. </para>
  487. </listitem>
  488. <listitem>
  489. <para>
  490. <emphasis>bugs</emphasis> stores information
  491. about bugs, including that current state of the bug, the
  492. person who reported the bug, the person who is assigned to
  493. fix the bug, and the person who is assigned to verify the
  494. fix.
  495. </para>
  496. </listitem>
  497. <listitem>
  498. <para>
  499. <emphasis>bugs_products</emphasis> stores a
  500. relationship between bugs and products. This implements a
  501. many-to-many relationship, because a given bug may be
  502. relevant to multiple products, and of course a given
  503. product can have multiple bugs.
  504. </para>
  505. </listitem>
  506. </itemizedlist>
  507. <para>
  508. The following <acronym>SQL</acronym> data definition language pseudocode describes the
  509. tables in this example database. These example tables are used
  510. extensively by the automated unit tests for <classname>Zend_Db</classname>.
  511. </para>
  512. <programlisting language="sql"><![CDATA[
  513. CREATE TABLE accounts (
  514. account_name VARCHAR(100) NOT NULL PRIMARY KEY
  515. );
  516. CREATE TABLE products (
  517. product_id INTEGER NOT NULL PRIMARY KEY,
  518. product_name VARCHAR(100)
  519. );
  520. CREATE TABLE bugs (
  521. bug_id INTEGER NOT NULL PRIMARY KEY,
  522. bug_description VARCHAR(100),
  523. bug_status VARCHAR(20),
  524. reported_by VARCHAR(100) REFERENCES accounts(account_name),
  525. assigned_to VARCHAR(100) REFERENCES accounts(account_name),
  526. verified_by VARCHAR(100) REFERENCES accounts(account_name)
  527. );
  528. CREATE TABLE bugs_products (
  529. bug_id INTEGER NOT NULL REFERENCES bugs,
  530. product_id INTEGER NOT NULL REFERENCES products,
  531. PRIMARY KEY (bug_id, product_id)
  532. );
  533. ]]></programlisting>
  534. <para>
  535. Also notice that the 'bugs' table contains multiple
  536. foreign key references to the 'accounts' table.
  537. Each of these foreign keys may reference a different row in the
  538. 'accounts' table for a given bug.
  539. </para>
  540. <para>
  541. The diagram below illustrates the physical data model of the
  542. example database.
  543. </para>
  544. <para>
  545. <inlinegraphic width="387" scale="100" align="center" valign="middle"
  546. fileref="figures/zend.db.adapter.example-database.png" format="PNG" />
  547. </para>
  548. </sect2>
  549. <sect2 id="zend.db.adapter.select">
  550. <title>Reading Query Results</title>
  551. <para>
  552. This section describes methods of the Adapter class with which you
  553. can run <acronym>SELECT</acronym> queries and retrieve the query results.
  554. </para>
  555. <sect3 id="zend.db.adapter.select.fetchall">
  556. <title>Fetching a Complete Result Set</title>
  557. <para>
  558. You can run a <acronym>SQL</acronym> <acronym>SELECT</acronym> query and retrieve
  559. its results in one step using the <methodname>fetchAll()</methodname> method.
  560. </para>
  561. <para>
  562. The first argument to this method is a string containing a
  563. <acronym>SELECT</acronym> statement. Alternatively, the first argument can be an
  564. object of class <link linkend="zend.db.select">Zend_Db_Select</link>.
  565. The Adapter automatically converts this object to a string
  566. representation of the <acronym>SELECT</acronym> statement.
  567. </para>
  568. <para>
  569. The second argument to <methodname>fetchAll()</methodname> is an array of
  570. values to substitute for parameter placeholders in the <acronym>SQL</acronym>
  571. statement.
  572. </para>
  573. <example id="zend.db.adapter.select.fetchall.example">
  574. <title>Using fetchAll()</title>
  575. <programlisting language="php"><![CDATA[
  576. $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
  577. $result = $db->fetchAll($sql, 2);
  578. ]]></programlisting>
  579. </example>
  580. </sect3>
  581. <sect3 id="zend.db.adapter.select.fetch-mode">
  582. <title>Changing the Fetch Mode</title>
  583. <para>
  584. By default, <methodname>fetchAll()</methodname> returns an array of
  585. rows, each of which is an associative array. The keys of the
  586. associative array are the columns or column aliases named in
  587. the select query.
  588. </para>
  589. <para>
  590. You can specify a different style of fetching results using the
  591. <methodname>setFetchMode()</methodname> method. The modes supported are
  592. identified by constants:
  593. </para>
  594. <itemizedlist>
  595. <listitem>
  596. <para>
  597. <emphasis>Zend_Db::FETCH_ASSOC</emphasis>:
  598. return data in an array of associative arrays.
  599. The array keys are column names, as strings. This is the default fetch mode
  600. for <classname>Zend_Db_Adapter</classname> classes.
  601. </para>
  602. <para>
  603. Note that if your select-list contains more than one
  604. column with the same name, for example if they are from
  605. two different tables in a <acronym>JOIN</acronym>, there can be only one
  606. entry in the associative array for a given name.
  607. If you use the <acronym>FETCH_ASSOC</acronym> mode, you should specify
  608. column aliases in your <acronym>SELECT</acronym> query to ensure that the
  609. names result in unique array keys.
  610. </para>
  611. <para>
  612. By default, these strings are returned as they are
  613. returned by the database driver. This is typically the
  614. spelling of the column in the <acronym>RDBMS</acronym> server. You can
  615. specify the case for these strings, using the
  616. <constant>Zend_Db::CASE_FOLDING</constant> option.
  617. Specify this when instantiating the Adapter.
  618. See <xref linkend="zend.db.adapter.connecting.parameters.example1" />.
  619. </para>
  620. </listitem>
  621. <listitem>
  622. <para>
  623. <emphasis>Zend_Db::FETCH_NUM</emphasis>:
  624. return data in an array of arrays. The arrays are
  625. indexed by integers, corresponding to the position of
  626. the respective field in the select-list of the query.
  627. </para>
  628. </listitem>
  629. <listitem>
  630. <para>
  631. <emphasis>Zend_Db::FETCH_BOTH</emphasis>:
  632. return data in an array of arrays. The array keys are
  633. both strings as used in the FETCH_ASSOC mode, and
  634. integers as used in the FETCH_NUM mode. Note that the
  635. number of elements in the array is double that which
  636. would be in the array if you used either FETCH_ASSOC
  637. or FETCH_NUM.
  638. </para>
  639. </listitem>
  640. <listitem>
  641. <para>
  642. <emphasis>Zend_Db::FETCH_COLUMN</emphasis>:
  643. return data in an array of values. The value in each array
  644. is the value returned by one column of the result set.
  645. By default, this is the first column, indexed by 0.
  646. </para>
  647. </listitem>
  648. <listitem>
  649. <para>
  650. <emphasis>Zend_Db::FETCH_OBJ</emphasis>:
  651. return data in an array of objects. The default class
  652. is the <acronym>PHP</acronym> built-in class stdClass. Columns of the
  653. result set are available as public properties of the
  654. object.
  655. </para>
  656. </listitem>
  657. </itemizedlist>
  658. <example id="zend.db.adapter.select.fetch-mode.example">
  659. <title>Using setFetchMode()</title>
  660. <programlisting language="php"><![CDATA[
  661. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  662. $result = $db->fetchAll('SELECT * FROM bugs WHERE bug_id = ?', 2);
  663. // $result is an array of objects
  664. echo $result[0]->bug_description;
  665. ]]></programlisting>
  666. </example>
  667. </sect3>
  668. <sect3 id="zend.db.adapter.select.fetchassoc">
  669. <title>Fetching a Result Set as an Associative Array</title>
  670. <para>
  671. The <methodname>fetchAssoc()</methodname> method returns data in an array
  672. of associative arrays, regardless of what value you have set
  673. for the fetch mode.
  674. </para>
  675. <example id="zend.db.adapter.select.fetchassoc.example">
  676. <title>Using fetchAssoc()</title>
  677. <programlisting language="php"><![CDATA[
  678. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  679. $result = $db->fetchAssoc('SELECT * FROM bugs WHERE bug_id = ?', 2);
  680. // $result is an array of associative arrays, in spite of the fetch mode
  681. echo $result[0]['bug_description'];
  682. ]]></programlisting>
  683. </example>
  684. </sect3>
  685. <sect3 id="zend.db.adapter.select.fetchcol">
  686. <title>Fetching a Single Column from a Result Set</title>
  687. <para>
  688. The <methodname>fetchCol()</methodname> method returns data in an array
  689. of values, regardless of the value you have set for the fetch mode.
  690. This only returns the first column returned by the query.
  691. Any other columns returned by the query are discarded.
  692. If you need to return a column other than the first, see
  693. <xref linkend="zend.db.statement.fetching.fetchcolumn" />.
  694. </para>
  695. <example id="zend.db.adapter.select.fetchcol.example">
  696. <title>Using fetchCol()</title>
  697. <programlisting language="php"><![CDATA[
  698. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  699. $result = $db->fetchCol(
  700. 'SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
  701. // contains bug_description; bug_id is not returned
  702. echo $result[0];
  703. ]]></programlisting>
  704. </example>
  705. </sect3>
  706. <sect3 id="zend.db.adapter.select.fetchpairs">
  707. <title>Fetching Key-Value Pairs from a Result Set</title>
  708. <para>
  709. The <methodname>fetchPairs()</methodname> method returns data in an array
  710. of key-value pairs, as an associative array with a single entry
  711. per row. The key of this associative array is taken from the
  712. first column returned by the <acronym>SELECT</acronym> query. The value is taken
  713. from the second column returned by the <acronym>SELECT</acronym> query. Any other
  714. columns returned by the query are discarded.
  715. </para>
  716. <para>
  717. You should design the <acronym>SELECT</acronym> query so that the first column
  718. returned has unique values. If there are duplicates values in
  719. the first column, entries in the associative array will be
  720. overwritten.
  721. </para>
  722. <example id="zend.db.adapter.select.fetchpairs.example">
  723. <title>Using fetchPairs()</title>
  724. <programlisting language="php"><![CDATA[
  725. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  726. $result = $db->fetchPairs('SELECT bug_id, bug_status FROM bugs');
  727. echo $result[2];
  728. ]]></programlisting>
  729. </example>
  730. </sect3>
  731. <sect3 id="zend.db.adapter.select.fetchrow">
  732. <title>Fetching a Single Row from a Result Set</title>
  733. <para>
  734. The <methodname>fetchRow()</methodname> method returns data using the
  735. current fetch mode, but it returns only the first row
  736. fetched from the result set.
  737. </para>
  738. <example id="zend.db.adapter.select.fetchrow.example">
  739. <title>Using fetchRow()</title>
  740. <programlisting language="php"><![CDATA[
  741. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  742. $result = $db->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
  743. // note that $result is a single object, not an array of objects
  744. echo $result->bug_description;
  745. ]]></programlisting>
  746. </example>
  747. </sect3>
  748. <sect3 id="zend.db.adapter.select.fetchone">
  749. <title>Fetching a Single Scalar from a Result Set</title>
  750. <para>
  751. The <methodname>fetchOne()</methodname> method is like a combination
  752. of <methodname>fetchRow()</methodname> with <methodname>fetchCol()</methodname>,
  753. in that it returns data only for the first row fetched from
  754. the result set, and it returns only the value of the first
  755. column in that row. Therefore it returns only a single
  756. scalar value, not an array or an object.
  757. </para>
  758. <example id="zend.db.adapter.select.fetchone.example">
  759. <title>Using fetchOne()</title>
  760. <programlisting language="php"><![CDATA[
  761. $result = $db->fetchOne('SELECT bug_status FROM bugs WHERE bug_id = 2');
  762. // this is a single string value
  763. echo $result;
  764. ]]></programlisting>
  765. </example>
  766. </sect3>
  767. </sect2>
  768. <sect2 id="zend.db.adapter.write">
  769. <title>Writing Changes to the Database</title>
  770. <para>
  771. You can use the Adapter class to write new data or change existing
  772. data in your database. This section describes methods to do these
  773. operations.
  774. </para>
  775. <sect3 id="zend.db.adapter.write.insert">
  776. <title>Inserting Data</title>
  777. <para>
  778. You can add new rows to a table in your database using the
  779. <methodname>insert()</methodname> method. The first argument is a string
  780. that names the table, and the second argument is an associative
  781. array, mapping column names to data values.
  782. </para>
  783. <example id="zend.db.adapter.write.insert.example">
  784. <title>Inserting in a Table</title>
  785. <programlisting language="php"><![CDATA[
  786. $data = array(
  787. 'created_on' => '2007-03-22',
  788. 'bug_description' => 'Something wrong',
  789. 'bug_status' => 'NEW'
  790. );
  791. $db->insert('bugs', $data);
  792. ]]></programlisting>
  793. </example>
  794. <para>
  795. Columns you exclude from the array of data are not specified to
  796. the database. Therefore, they follow the same rules that an
  797. <acronym>SQL</acronym> <acronym>INSERT</acronym> statement follows: if the column
  798. has a <acronym>DEFAULT</acronym> clause, the column takes that value in the row
  799. created, otherwise the column is left in a <constant>NULL</constant> state.
  800. </para>
  801. <para>
  802. By default, the values in your data array are inserted using
  803. parameters. This reduces risk of some types of security
  804. issues. You don't need to apply escaping or quoting to values
  805. in the data array.
  806. </para>
  807. <para>
  808. You might need values in the data array to be treated as <acronym>SQL</acronym>
  809. expressions, in which case they should not be quoted. By
  810. default, all data values passed as strings are treated as
  811. string literals. To specify that the value is an <acronym>SQL</acronym>
  812. expression and therefore should not be quoted, pass the value
  813. in the data array as an object of type <classname>Zend_Db_Expr</classname> instead
  814. of a plain string.
  815. </para>
  816. <example id="zend.db.adapter.write.insert.example2">
  817. <title>Inserting Expressions in a Table</title>
  818. <programlisting language="php"><![CDATA[
  819. $data = array(
  820. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  821. 'bug_description' => 'Something wrong',
  822. 'bug_status' => 'NEW'
  823. );
  824. $db->insert('bugs', $data);
  825. ]]></programlisting>
  826. </example>
  827. </sect3>
  828. <sect3 id="zend.db.adapter.write.lastinsertid">
  829. <title>Retrieving a Generated Value</title>
  830. <para>
  831. Some <acronym>RDBMS</acronym> brands support auto-incrementing primary keys.
  832. A table defined this way generates a primary key value
  833. automatically during an <acronym>INSERT</acronym> of a new row. The return value
  834. of the <methodname>insert()</methodname> method is <emphasis>not</emphasis>
  835. the last inserted ID, because the table might not have an
  836. auto-incremented column. Instead, the return value is the
  837. number of rows affected (usually 1).
  838. </para>
  839. <para>
  840. If your table is defined with an auto-incrementing primary key,
  841. you can call the <methodname>lastInsertId()</methodname> method after the
  842. insert. This method returns the last value generated in the
  843. scope of the current database connection.
  844. </para>
  845. <example id="zend.db.adapter.write.lastinsertid.example-1">
  846. <title>Using lastInsertId() for an Auto-Increment Key</title>
  847. <programlisting language="php"><![CDATA[
  848. $db->insert('bugs', $data);
  849. // return the last value generated by an auto-increment column
  850. $id = $db->lastInsertId();
  851. ]]></programlisting>
  852. </example>
  853. <para>
  854. Some <acronym>RDBMS</acronym> brands support a sequence object, which generates
  855. unique values to serve as primary key values. To support
  856. sequences, the <methodname>lastInsertId()</methodname> method accepts two
  857. optional string arguments. These arguments name the table and
  858. the column, assuming you have followed the convention that a
  859. sequence is named using the table and column names for which
  860. the sequence generates values, and a suffix "_seq". This is
  861. based on the convention used by PostgreSQL when naming
  862. sequences for SERIAL columns. For example, a table "bugs" with
  863. primary key column "bug_id" would use a sequence named
  864. "bugs_bug_id_seq".
  865. </para>
  866. <example id="zend.db.adapter.write.lastinsertid.example-2">
  867. <title>Using lastInsertId() for a Sequence</title>
  868. <programlisting language="php"><![CDATA[
  869. $db->insert('bugs', $data);
  870. // return the last value generated by sequence 'bugs_bug_id_seq'.
  871. $id = $db->lastInsertId('bugs', 'bug_id');
  872. // alternatively, return the last value generated by sequence 'bugs_seq'.
  873. $id = $db->lastInsertId('bugs');
  874. ]]></programlisting>
  875. </example>
  876. <para>
  877. If the name of your sequence object does not follow this naming
  878. convention, use the <methodname>lastSequenceId()</methodname> method
  879. instead. This method takes a single string argument, naming
  880. the sequence literally.
  881. </para>
  882. <example id="zend.db.adapter.write.lastinsertid.example-3">
  883. <title>Using lastSequenceId()</title>
  884. <programlisting language="php"><![CDATA[
  885. $db->insert('bugs', $data);
  886. // return the last value generated by sequence 'bugs_id_gen'.
  887. $id = $db->lastSequenceId('bugs_id_gen');
  888. ]]></programlisting>
  889. </example>
  890. <para>
  891. For <acronym>RDBMS</acronym> brands that don't support sequences, including MySQL,
  892. Microsoft <acronym>SQL</acronym> Server, and SQLite, the arguments to the
  893. <methodname>lastInsertId()</methodname> method are ignored, and the value returned
  894. is the most recent value generated for any table by <acronym>INSERT</acronym>
  895. operations during the current connection. For these <acronym>RDBMS</acronym> brands,
  896. the <methodname>lastSequenceId()</methodname> method always returns
  897. <constant>NULL</constant>.
  898. </para>
  899. <note>
  900. <title>Why Not Use "SELECT MAX(id) FROM table"?</title>
  901. <para>
  902. Sometimes this query returns the most recent primary key
  903. value inserted into the table. However, this technique
  904. is not safe to use in an environment where multiple clients are
  905. inserting records to the database. It is possible, and
  906. therefore is bound to happen eventually, that another
  907. client inserts another row in the instant between the
  908. insert performed by your client application and your query
  909. for the <methodname>MAX(id)</methodname> value. Thus the value returned does
  910. not identify the row you inserted, it identifies the row
  911. inserted by some other client. There is no way to know
  912. when this has happened.
  913. </para>
  914. <para>
  915. Using a strong transaction isolation mode such as
  916. "repeatable read" can mitigate this risk, but some <acronym>RDBMS</acronym>
  917. brands don't support the transaction isolation required for
  918. this, or else your application may use a lower transaction
  919. isolation mode by design.
  920. </para>
  921. <para>
  922. Furthermore, using an expression like "MAX(id)+1" to generate
  923. a new value for a primary key is not safe, because two clients
  924. could do this query simultaneously, and then both use the same
  925. calculated value for their next <acronym>INSERT</acronym> operation.
  926. </para>
  927. <para>
  928. All <acronym>RDBMS</acronym> brands provide mechanisms to generate unique
  929. values, and to return the last value generated. These
  930. mechanisms necessarily work outside of the scope of
  931. transaction isolation, so there is no chance of two clients
  932. generating the same value, and there is no chance that the
  933. value generated by another client could be reported to your
  934. client's connection as the last value generated.
  935. </para>
  936. </note>
  937. </sect3>
  938. <sect3 id="zend.db.adapter.write.update">
  939. <title>Updating Data</title>
  940. <para>
  941. You can update rows in a database table using the
  942. <methodname>update()</methodname> method of an Adapter. This method takes
  943. three arguments: the first is the name of the table; the
  944. second is an associative array mapping columns to change to new
  945. values to assign to these columns.
  946. </para>
  947. <para>
  948. The values in the data array are treated as string literals.
  949. See <xref linkend="zend.db.adapter.write.insert" />
  950. for information on using <acronym>SQL</acronym> expressions in the data array.
  951. </para>
  952. <para>
  953. The third argument is a string containing an <acronym>SQL</acronym> expression
  954. that is used as criteria for the rows to change. The values
  955. and identifiers in this argument are not quoted or escaped.
  956. You are responsible for ensuring that any dynamic content is
  957. interpolated into this string safely.
  958. See <xref linkend="zend.db.adapter.quoting" />
  959. for methods to help you do this.
  960. </para>
  961. <para>
  962. The return value is the number of rows affected by the update
  963. operation.
  964. </para>
  965. <example id="zend.db.adapter.write.update.example">
  966. <title>Updating Rows</title>
  967. <programlisting language="php"><![CDATA[
  968. $data = array(
  969. 'updated_on' => '2007-03-23',
  970. 'bug_status' => 'FIXED'
  971. );
  972. $n = $db->update('bugs', $data, 'bug_id = 2');
  973. ]]></programlisting>
  974. </example>
  975. <para>
  976. If you omit the third argument, then all rows in the database
  977. table are updated with the values specified in the data array.
  978. </para>
  979. <para>
  980. If you provide an array of strings as the third argument, these
  981. strings are joined together as terms in an expression separated
  982. by <constant>AND</constant> operators.
  983. </para>
  984. <para>
  985. If you provide an array of arrays as the third argument, the
  986. values will be automatically quoted into the keys. These
  987. will then be joined together as terms, separated by
  988. <constant>AND</constant> operators.
  989. </para>
  990. <example id="zend.db.adapter.write.update.example-array">
  991. <title>Updating Rows Using an Array of Expressions</title>
  992. <programlisting language="php"><![CDATA[
  993. $data = array(
  994. 'updated_on' => '2007-03-23',
  995. 'bug_status' => 'FIXED'
  996. );
  997. $where[] = "reported_by = 'goofy'";
  998. $where[] = "bug_status = 'OPEN'";
  999. $n = $db->update('bugs', $data, $where);
  1000. // Resulting SQL is:
  1001. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  1002. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
  1003. ]]></programlisting>
  1004. </example>
  1005. <example id="zend.db.adapter.write.update.example-arrayofarrays">
  1006. <title>Updating Rows Using an Array of Arrays</title>
  1007. <programlisting language="php"><![CDATA[
  1008. $data = array(
  1009. 'updated_on' => '2007-03-23',
  1010. 'bug_status' => 'FIXED'
  1011. );
  1012. $where['reported_by = ?'] = 'goofy';
  1013. $where['bug_status = ?'] = 'OPEN';
  1014. $n = $db->update('bugs', $data, $where);
  1015. // Resulting SQL is:
  1016. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  1017. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
  1018. ]]></programlisting>
  1019. </example>
  1020. </sect3>
  1021. <sect3 id="zend.db.adapter.write.delete">
  1022. <title>Deleting Data</title>
  1023. <para>
  1024. You can delete rows from a database table using the
  1025. <methodname>delete()</methodname> method. This method takes two arguments:
  1026. the first is a string naming the table.
  1027. </para>
  1028. <para>
  1029. The second argument is a string containing an <acronym>SQL</acronym> expression
  1030. that is used as criteria for the rows to delete. The values
  1031. and identifiers in this argument are not quoted or escaped.
  1032. You are responsible for ensuring that any dynamic content is
  1033. interpolated into this string safely.
  1034. See <xref linkend="zend.db.adapter.quoting" />
  1035. for methods to help you do this.
  1036. </para>
  1037. <para>
  1038. The return value is the number of rows affected by the delete
  1039. operation.
  1040. </para>
  1041. <example id="zend.db.adapter.write.delete.example">
  1042. <title>Deleting Rows</title>
  1043. <programlisting language="php"><![CDATA[
  1044. $n = $db->delete('bugs', 'bug_id = 3');
  1045. ]]></programlisting>
  1046. </example>
  1047. <para>
  1048. If you omit the second argument, the result is that all rows in
  1049. the database table are deleted.
  1050. </para>
  1051. <para>
  1052. If you provide an array of strings as the second argument, these
  1053. strings are joined together as terms in an expression separated
  1054. by <constant>AND</constant> operators.
  1055. </para>
  1056. <para>
  1057. If you provide an array of arrays as the second argument, the
  1058. values will be automatically quoted into the keys. These
  1059. will then be joined together as terms, separated by
  1060. <constant>AND</constant> operators.
  1061. </para>
  1062. </sect3>
  1063. </sect2>
  1064. <sect2 id="zend.db.adapter.quoting">
  1065. <title>Quoting Values and Identifiers</title>
  1066. <para>
  1067. When you form <acronym>SQL</acronym> queries, often it is the case that you need to
  1068. include the values of <acronym>PHP</acronym> variables in <acronym>SQL</acronym>
  1069. expressions. This is risky, because if the value in a <acronym>PHP</acronym> string
  1070. contains certain symbols, such as the quote symbol, it could result in invalid
  1071. <acronym>SQL</acronym>. For example, notice the imbalanced quote characters in the
  1072. following query:
  1073. <programlisting language="php"><![CDATA[
  1074. $name = "O'Reilly";
  1075. $sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
  1076. echo $sql;
  1077. // SELECT * FROM bugs WHERE reported_by = 'O'Reilly'
  1078. ]]></programlisting>
  1079. </para>
  1080. <para>
  1081. Even worse is the risk that such code mistakes might be exploited
  1082. deliberately by a person who is trying to manipulate the function
  1083. of your web application. If they can specify the value of a <acronym>PHP</acronym>
  1084. variable through the use of an <acronym>HTTP</acronym> parameter or other mechanism,
  1085. they might be able to make your <acronym>SQL</acronym> queries do things that you
  1086. didn't intend them to do, such as return data to which the person
  1087. should not have privilege to read. This is a serious and widespread
  1088. technique for violating application security, known as "SQL Injection" (see <ulink
  1089. url="http://en.wikipedia.org/wiki/SQL_Injection">http://en.wikipedia.org/wiki/SQL_Injection</ulink>).
  1090. </para>
  1091. <para>
  1092. The <classname>Zend_Db</classname> Adapter class provides convenient functions to help
  1093. you reduce vulnerabilities to <acronym>SQL</acronym> Injection attacks in your
  1094. <acronym>PHP</acronym> code. The solution is to escape special characters such as quotes
  1095. in <acronym>PHP</acronym> values before they are interpolated into your
  1096. <acronym>SQL</acronym> strings. This protects against both accidental and deliberate
  1097. manipulation of <acronym>SQL</acronym> strings by <acronym>PHP</acronym> variables that
  1098. contain special characters.
  1099. </para>
  1100. <sect3 id="zend.db.adapter.quoting.quote">
  1101. <title>Using quote()</title>
  1102. <para>
  1103. The <methodname>quote()</methodname> method accepts a single argument, a
  1104. scalar string value. It returns the value with special
  1105. characters escaped in a manner appropriate for the <acronym>RDBMS</acronym> you
  1106. are using, and surrounded by string value delimiters. The
  1107. standard <acronym>SQL</acronym> string value delimiter is the single-quote
  1108. (').
  1109. </para>
  1110. <example id="zend.db.adapter.quoting.quote.example">
  1111. <title>Using quote()</title>
  1112. <programlisting language="php"><![CDATA[
  1113. $name = $db->quote("O'Reilly");
  1114. echo $name;
  1115. // 'O\'Reilly'
  1116. $sql = "SELECT * FROM bugs WHERE reported_by = $name";
  1117. echo $sql;
  1118. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1119. ]]></programlisting>
  1120. </example>
  1121. <para>
  1122. Note that the return value of <methodname>quote()</methodname> includes the
  1123. quote delimiters around the string. This is different from
  1124. some functions that escape special characters but do not add
  1125. the quote delimiters, for example <ulink
  1126. url="http://www.php.net/mysqli_real_escape_string">mysql_real_escape_string()</ulink>.
  1127. </para>
  1128. <para>
  1129. Values may need to be quoted or not quoted according to the <acronym>SQL</acronym>
  1130. datatype context in which they are used. For instance, in some
  1131. RDBMS brands, an integer value must not be quoted as a string
  1132. if it is compared to an integer-type column or expression.
  1133. In other words, the following is an error in some <acronym>SQL</acronym>
  1134. implementations, assuming <property>intColumn</property> has a
  1135. <acronym>SQL</acronym> datatype of <constant>INTEGER</constant>
  1136. <programlisting language="php"><![CDATA[
  1137. SELECT * FROM atable WHERE intColumn = '123'
  1138. ]]></programlisting>
  1139. </para>
  1140. <para>
  1141. You can use the optional second argument to the
  1142. <methodname>quote()</methodname> method to apply quoting selectively for
  1143. the <acronym>SQL</acronym> datatype you specify.
  1144. </para>
  1145. <example id="zend.db.adapter.quoting.quote.example-2">
  1146. <title>Using quote() with a SQL Type</title>
  1147. <programlisting language="php"><![CDATA[
  1148. $value = '1234';
  1149. $sql = 'SELECT * FROM atable WHERE intColumn = '
  1150. . $db->quote($value, 'INTEGER');
  1151. ]]></programlisting>
  1152. </example>
  1153. <para>
  1154. Each <classname>Zend_Db_Adapter</classname> class has encoded the names of numeric
  1155. <acronym>SQL</acronym> datatypes for the respective brand of
  1156. <acronym>RDBMS</acronym>. You can also use the constants
  1157. <constant>Zend_Db::INT_TYPE</constant>, <constant>Zend_Db::BIGINT_TYPE</constant>,
  1158. and <constant>Zend_Db::FLOAT_TYPE</constant> to write code in a more
  1159. <acronym>RDBMS</acronym>-independent way.
  1160. </para>
  1161. <para>
  1162. <classname>Zend_Db_Table</classname> specifies <acronym>SQL</acronym> types to
  1163. <methodname>quote()</methodname> automatically when generating
  1164. <acronym>SQL</acronym> queries that reference a table's key columns.
  1165. </para>
  1166. </sect3>
  1167. <sect3 id="zend.db.adapter.quoting.quote-into">
  1168. <title>Using quoteInto()</title>
  1169. <para>
  1170. The most typical usage of quoting is to interpolate a <acronym>PHP</acronym>
  1171. variable into a <acronym>SQL</acronym> expression or statement. You can use the
  1172. <methodname>quoteInto()</methodname> method to do this in one step. This
  1173. method takes two arguments: the first argument is a string
  1174. containing a placeholder symbol (?), and the
  1175. second argument is a value or <acronym>PHP</acronym> variable that should be
  1176. substituted for that placeholder.
  1177. </para>
  1178. <para>
  1179. The placeholder symbol is the same symbol used by many <acronym>RDBMS</acronym>
  1180. brands for positional parameters, but the
  1181. <methodname>quoteInto()</methodname> method only emulates query parameters.
  1182. The method simply interpolates the value into the string,
  1183. escapes special characters, and applies quotes around it.
  1184. True query parameters maintain the separation between the <acronym>SQL</acronym>
  1185. string and the parameters as the statement is parsed in the
  1186. <acronym>RDBMS</acronym> server.
  1187. </para>
  1188. <example id="zend.db.adapter.quoting.quote-into.example">
  1189. <title>Using quoteInto()</title>
  1190. <programlisting language="php"><![CDATA[
  1191. $sql = $db->quoteInto("SELECT * FROM bugs WHERE reported_by = ?", "O'Reilly");
  1192. echo $sql;
  1193. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1194. ]]></programlisting>
  1195. </example>
  1196. <para>
  1197. You can use the optional third parameter of
  1198. <methodname>quoteInto()</methodname> to specify the <acronym>SQL</acronym> datatype.
  1199. Numeric datatypes are not quoted, and other types are quoted.
  1200. </para>
  1201. <example id="zend.db.adapter.quoting.quote-into.example-2">
  1202. <title>Using quoteInto() with a SQL Type</title>
  1203. <programlisting language="php"><![CDATA[
  1204. $sql = $db
  1205. ->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
  1206. echo $sql;
  1207. // SELECT * FROM bugs WHERE reported_by = 1234
  1208. ]]></programlisting>
  1209. </example>
  1210. </sect3>
  1211. <sect3 id="zend.db.adapter.quoting.quote-identifier">
  1212. <title>Using quoteIdentifier()</title>
  1213. <para>
  1214. Values are not the only part of <acronym>SQL</acronym> syntax that might need to
  1215. be variable. If you use <acronym>PHP</acronym> variables to name tables, columns,
  1216. or other identifiers in your <acronym>SQL</acronym> statements, you might need to
  1217. quote these strings too. By default, <acronym>SQL</acronym> identifiers have
  1218. syntax rules like <acronym>PHP</acronym> and most other programming languages.
  1219. For example, identifiers should not contain spaces, certain
  1220. punctuation or special characters, or international characters.
  1221. Also certain words are reserved for <acronym>SQL</acronym> syntax, and should not
  1222. be used as identifiers.
  1223. </para>
  1224. <para>
  1225. However, <acronym>SQL</acronym> has a feature called
  1226. <emphasis>delimited identifiers</emphasis>, which allows broader choices for the
  1227. spelling of identifiers. If you enclose a <acronym>SQL</acronym> identifier in the
  1228. proper types of quotes, you can use identifiers with spellings that would be invalid
  1229. without the quotes. Delimited identifiers can contain spaces,
  1230. punctuation, or international characters. You can also use <acronym>SQL</acronym>
  1231. reserved words if you enclose them in identifier delimiters.
  1232. </para>
  1233. <para>
  1234. The <methodname>quoteIdentifier()</methodname> method works like
  1235. <methodname>quote()</methodname>, but it applies the identifier delimiter
  1236. characters to the string according to the type of Adapter you
  1237. use. For example, standard <acronym>SQL</acronym> uses double-quotes
  1238. (") for identifier delimiters, and most <acronym>RDBMS</acronym>
  1239. brands use that symbol. MySQL uses back-quotes (`) by default. The
  1240. <methodname>quoteIdentifier()</methodname> method also escapes special
  1241. characters within the string argument.
  1242. </para>
  1243. <example id="zend.db.adapter.quoting.quote-identifier.example">
  1244. <title>Using quoteIdentifier()</title>
  1245. <programlisting language="php"><![CDATA[
  1246. // we might have a table name that is an SQL reserved word
  1247. $tableName = $db->quoteIdentifier("order");
  1248. $sql = "SELECT * FROM $tableName";
  1249. echo $sql
  1250. // SELECT * FROM "order"
  1251. ]]></programlisting>
  1252. </example>
  1253. <para>
  1254. <acronym>SQL</acronym> delimited identifiers are case-sensitive, unlike unquoted
  1255. identifiers. Therefore, if you use delimited identifiers, you
  1256. must use the spelling of the identifier exactly as it is stored
  1257. in your schema, including the case of the letters.
  1258. </para>
  1259. <para>
  1260. In most cases where <acronym>SQL</acronym> is generated within
  1261. <classname>Zend_Db</classname> classes, the default is that all identifiers are
  1262. delimited automatically. You can change this behavior with the option
  1263. <constant>Zend_Db::AUTO_QUOTE_IDENTIFIERS</constant>. Specify this
  1264. when instantiating the Adapter.
  1265. See <xref linkend="zend.db.adapter.connecting.parameters.example2" />.
  1266. </para>
  1267. </sect3>
  1268. </sect2>
  1269. <sect2 id="zend.db.adapter.transactions">
  1270. <title>Controlling Database Transactions</title>
  1271. <para>
  1272. Databases define transactions as logical units of work that can be
  1273. committed or rolled back as a single change, even if they operate
  1274. on multiple tables. All queries to a database are executed within
  1275. the context of a transaction, even if the database driver manages
  1276. them implicitly. This is called <emphasis>auto-commit</emphasis>
  1277. mode, in which the database driver creates a transaction for every
  1278. statement you execute, and commits that transaction after your
  1279. <acronym>SQL</acronym> statement has been executed. By default, all
  1280. <classname>Zend_Db</classname> Adapter classes operate in auto-commit mode.
  1281. </para>
  1282. <para>
  1283. Alternatively, you can specify the beginning and resolution of a
  1284. transaction, and thus control how many <acronym>SQL</acronym> queries are included in
  1285. a single group that is committed (or rolled back) as a single
  1286. operation. Use the <methodname>beginTransaction()</methodname> method to
  1287. initiate a transaction. Subsequent <acronym>SQL</acronym> statements are executed in
  1288. the context of the same transaction until you resolve it
  1289. explicitly.
  1290. </para>
  1291. <para>
  1292. To resolve the transaction, use either the <methodname>commit()</methodname> or
  1293. <methodname>rollBack()</methodname> methods. The <methodname>commit()</methodname>
  1294. method marks changes made during your transaction as committed, which
  1295. means the effects of these changes are shown in queries run in
  1296. other transactions.
  1297. </para>
  1298. <para>
  1299. The <methodname>rollBack()</methodname> method does the opposite: it discards
  1300. the changes made during your transaction. The changes are
  1301. effectively undone, and the state of the data returns to how it was
  1302. before you began your transaction. However, rolling back your
  1303. transaction has no effect on changes made by other transactions
  1304. running concurrently.
  1305. </para>
  1306. <para>
  1307. After you resolve this transaction, <classname>Zend_Db_Adapter</classname>
  1308. returns to auto-commit mode until you call
  1309. <methodname>beginTransaction()</methodname> again.
  1310. </para>
  1311. <example id="zend.db.adapter.transactions.example">
  1312. <title>Managing a Transaction to Ensure Consistency</title>
  1313. <programlisting language="php"><![CDATA[
  1314. // Start a transaction explicitly.
  1315. $db->beginTransaction();
  1316. try {
  1317. // Attempt to execute one or more queries:
  1318. $db->query(...);
  1319. $db->query(...);
  1320. $db->query(...);
  1321. // If all succeed, commit the transaction and all changes
  1322. // are committed at once.
  1323. $db->commit();
  1324. } catch (Exception $e) {
  1325. // If any of the queries failed and threw an exception,
  1326. // we want to roll back the whole transaction, reversing
  1327. // changes made in the transaction, even those that succeeded.
  1328. // Thus all changes are committed together, or none are.
  1329. $db->rollBack();
  1330. echo $e->getMessage();
  1331. }
  1332. ]]></programlisting>
  1333. </example>
  1334. </sect2>
  1335. <sect2 id="zend.db.adapter.list-describe">
  1336. <title>Listing and Describing Tables</title>
  1337. <para>
  1338. The <methodname>listTables()</methodname> method returns an array of strings,
  1339. naming all tables in the current database.
  1340. </para>
  1341. <para>
  1342. The <methodname>describeTable()</methodname> method returns an associative
  1343. array of metadata about a table. Specify the name of the table
  1344. as a string in the first argument to this method. The second
  1345. argument is optional, and names the schema in which the table
  1346. exists.
  1347. </para>
  1348. <para>
  1349. The keys of the associative array returned are the column names of
  1350. the table. The value corresponding to each column is also an
  1351. associative array, with the following keys and values:
  1352. </para>
  1353. <table frame="all" cellpadding="5" id="zend.db.adapter.list-describe.metadata">
  1354. <title>Metadata Fields Returned by describeTable()</title>
  1355. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  1356. <thead>
  1357. <row>
  1358. <entry>Key</entry>
  1359. <entry>Type</entry>
  1360. <entry>Description</entry>
  1361. </row>
  1362. </thead>
  1363. <tbody>
  1364. <row>
  1365. <entry><constant>SCHEMA_NAME</constant></entry>
  1366. <entry>(string)</entry>
  1367. <entry>Name of the database schema in which this table exists.</entry>
  1368. </row>
  1369. <row>
  1370. <entry><constant>TABLE_NAME</constant></entry>
  1371. <entry>(string)</entry>
  1372. <entry>Name of the table to which this column belongs.</entry>
  1373. </row>
  1374. <row>
  1375. <entry><constant>COLUMN_NAME</constant></entry>
  1376. <entry>(string)</entry>
  1377. <entry>Name of the column.</entry>
  1378. </row>
  1379. <row>
  1380. <entry><constant>COLUMN_POSITION</constant></entry>
  1381. <entry>(integer)</entry>
  1382. <entry>Ordinal position of the column in the table.</entry>
  1383. </row>
  1384. <row>
  1385. <entry><constant>DATA_TYPE</constant></entry>
  1386. <entry>(string)</entry>
  1387. <entry>RDBMS name of the datatype of the column.</entry>
  1388. </row>
  1389. <row>
  1390. <entry><constant>DEFAULT</constant></entry>
  1391. <entry>(string)</entry>
  1392. <entry>Default value for the column, if any.</entry>
  1393. </row>
  1394. <row>
  1395. <entry><constant>NULLABLE</constant></entry>
  1396. <entry>(boolean)</entry>
  1397. <entry>
  1398. <constant>TRUE</constant> if the column accepts <acronym>SQL</acronym>
  1399. <constant>NULL</constant>'s, <constant>FALSE</constant> if the
  1400. column has a <constant>NOT</constant> <constant>NULL</constant>
  1401. constraint.
  1402. </entry>
  1403. </row>
  1404. <row>
  1405. <entry><constant>LENGTH</constant></entry>
  1406. <entry>(integer)</entry>
  1407. <entry>
  1408. Length or size of the column as reported by the
  1409. <acronym>RDBMS</acronym>.
  1410. </entry>
  1411. </row>
  1412. <row>
  1413. <entry><constant>SCALE</constant></entry>
  1414. <entry>(integer)</entry>
  1415. <entry>
  1416. Scale of <acronym>SQL</acronym> NUMERIC or <constant>DECIMAL</constant>
  1417. type.
  1418. </entry>
  1419. </row>
  1420. <row>
  1421. <entry><constant>PRECISION</constant></entry>
  1422. <entry>(integer)</entry>
  1423. <entry>
  1424. Precision of <acronym>SQL</acronym> NUMERIC or
  1425. <constant>DECIMAL</constant> type.
  1426. </entry>
  1427. </row>
  1428. <row>
  1429. <entry><constant>UNSIGNED</constant></entry>
  1430. <entry>(boolean)</entry>
  1431. <entry>
  1432. <constant>TRUE</constant> if an integer-based type is reported as
  1433. <constant>UNSIGNED</constant>.
  1434. </entry>
  1435. </row>
  1436. <row>
  1437. <entry><constant>PRIMARY</constant></entry>
  1438. <entry>(boolean)</entry>
  1439. <entry>
  1440. <constant>TRUE</constant> if the column is part of the primary key of
  1441. this table.
  1442. </entry>
  1443. </row>
  1444. <row>
  1445. <entry><constant>PRIMARY_POSITION</constant></entry>
  1446. <entry>(integer)</entry>
  1447. <entry>Ordinal position (1-based) of the column in the primary key.</entry>
  1448. </row>
  1449. <row>
  1450. <entry><constant>IDENTITY</constant></entry>
  1451. <entry>(boolean)</entry>
  1452. <entry>
  1453. <constant>TRUE</constant> if the column uses an auto-generated value.
  1454. </entry>
  1455. </row>
  1456. </tbody>
  1457. </tgroup>
  1458. </table>
  1459. <note>
  1460. <title>How the IDENTITY Metadata Field Relates to Specific RDBMSs</title>
  1461. <para>
  1462. The IDENTITY metadata field was chosen as an 'idiomatic' term to
  1463. represent a relation to surrogate keys. This field can be
  1464. commonly known by the following values:-
  1465. </para>
  1466. <itemizedlist>
  1467. <listitem>
  1468. <para>
  1469. <constant>IDENTITY</constant> - DB2, MSSQL
  1470. </para>
  1471. </listitem>
  1472. <listitem>
  1473. <para>
  1474. <constant>AUTO_INCREMENT</constant> - MySQL
  1475. </para>
  1476. </listitem>
  1477. <listitem>
  1478. <para>
  1479. <constant>SERIAL</constant> - PostgreSQL
  1480. </para>
  1481. </listitem>
  1482. <listitem>
  1483. <para>
  1484. <constant>SEQUENCE</constant> - Oracle
  1485. </para>
  1486. </listitem>
  1487. </itemizedlist>
  1488. </note>
  1489. <para>
  1490. If no table exists matching the table name and optional schema name
  1491. specified, then <methodname>describeTable()</methodname> returns an empty array.
  1492. </para>
  1493. </sect2>
  1494. <sect2 id="zend.db.adapter.closing">
  1495. <title>Closing a Connection</title>
  1496. <para>
  1497. Normally it is not necessary to close a database connection. <acronym>PHP</acronym>
  1498. automatically cleans up all resources and the end of a request.
  1499. Database extensions are designed to close the connection as the
  1500. reference to the resource object is cleaned up.
  1501. </para>
  1502. <para>
  1503. However, if you have a long-duration <acronym>PHP</acronym> script that initiates many
  1504. database connections, you might need to close the connection, to avoid
  1505. exhausting the capacity of your <acronym>RDBMS</acronym> server. You can use the
  1506. Adapter's <methodname>closeConnection()</methodname> method to explicitly close
  1507. the underlying database connection.
  1508. </para>
  1509. <para>
  1510. Since release 1.7.2, you could check you are currently connected to the
  1511. <acronym>RDBMS</acronym> server with the method <methodname>isConnected()</methodname>.
  1512. This means that a connection resource has been initiated and wasn't closed. This
  1513. function is not currently able to test for example a server side closing of the
  1514. connection. This is internally use to close the connection. It allow you to close the
  1515. connection multiple times without errors. It was already the case before 1.7.2 for
  1516. <acronym>PDO</acronym> adapters but not for the others.
  1517. </para>
  1518. <example id="zend.db.adapter.closing.example">
  1519. <title>Closing a Database Connection</title>
  1520. <programlisting language="php"><![CDATA[
  1521. $db->closeConnection();
  1522. ]]></programlisting>
  1523. </example>
  1524. <note>
  1525. <title>Does Zend_Db Support Persistent Connections?</title>
  1526. <para>
  1527. Yes, persistence is supported through the addition of
  1528. the <property>persistent</property> flag set to <constant>TRUE</constant> in the
  1529. configuration (not driver_configuration) of an adapter
  1530. in <classname>Zend_Db</classname>.
  1531. </para>
  1532. <example id="zend.db.adapter.connecting.persistence.example">
  1533. <title>Using the Persitence Flag with the Oracle Adapter</title>
  1534. <programlisting language="php"><![CDATA[
  1535. $db = Zend_Db::factory('Oracle', array(
  1536. 'host' => '127.0.0.1',
  1537. 'username' => 'webuser',
  1538. 'password' => 'xxxxxxxx',
  1539. 'dbname' => 'test',
  1540. 'persistent' => true
  1541. ));
  1542. ]]></programlisting>
  1543. </example>
  1544. <para>
  1545. Please note that using persistent connections can cause an
  1546. excess of idle connections on the <acronym>RDBMS</acronym> server, which causes
  1547. more problems than any performance gain you might achieve by
  1548. reducing the overhead of making connections.
  1549. </para>
  1550. <para>
  1551. Database connections have state. That is, some objects in the
  1552. <acronym>RDBMS</acronym> server exist in session scope. Examples are locks, user
  1553. variables, temporary tables, and information about the most
  1554. recently executed query, such as rows affected, and last
  1555. generated id value. If you use persistent connections, your
  1556. application could access invalid or privileged data that were
  1557. created in a previous <acronym>PHP</acronym> request.
  1558. </para>
  1559. <para>
  1560. Currently, only Oracle, DB2, and the <acronym>PDO</acronym> adapters (where
  1561. specified by <acronym>PHP</acronym>) support persistence in
  1562. <classname>Zend_Db</classname>.
  1563. </para>
  1564. </note>
  1565. </sect2>
  1566. <sect2 id="zend.db.adapter.other-statements">
  1567. <title>Running Other Database Statements</title>
  1568. <para>
  1569. There might be cases in which you need to access the connection
  1570. object directly, as provided by the <acronym>PHP</acronym> database extension. Some
  1571. of these extensions may offer features that are not surfaced by
  1572. methods of <classname>Zend_Db_Adapter_Abstract</classname>.
  1573. </para>
  1574. <para>
  1575. For example, all <acronym>SQL</acronym> statements run by <classname>Zend_Db</classname>
  1576. are prepared, then executed. However, some database features are incompatible with
  1577. prepared statements. DDL statements like CREATE and ALTER cannot
  1578. be prepared in MySQL. Also, <acronym>SQL</acronym> statements don't benefit from the
  1579. <ulink url="http://dev.mysql.com/doc/refman/5.1/en/query-cache-how.html">MySQL Query
  1580. Cache</ulink>, prior to MySQL 5.1.17.
  1581. </para>
  1582. <para>
  1583. Most <acronym>PHP</acronym> database extensions provide a method to execute
  1584. <acronym>SQL</acronym> statements without preparing them. For example, in
  1585. <acronym>PDO</acronym>, this method is <methodname>exec()</methodname>. You can access
  1586. the connection object in the <acronym>PHP</acronym> extension directly using
  1587. <methodname>getConnection()</methodname>.
  1588. </para>
  1589. <example id="zend.db.adapter.other-statements.example">
  1590. <title>Running a Non-Prepared Statement in a PDO Adapter</title>
  1591. <programlisting language="php"><![CDATA[
  1592. $result = $db->getConnection()->exec('DROP TABLE bugs');
  1593. ]]></programlisting>
  1594. </example>
  1595. <para>
  1596. Similarly, you can access other methods or properties that are
  1597. specific to <acronym>PHP</acronym> database extensions. Be aware, though, that by
  1598. doing this you might constrain your application to the interface
  1599. provided by the extension for a specific brand of <acronym>RDBMS</acronym>.
  1600. </para>
  1601. <para>
  1602. In future versions of <classname>Zend_Db</classname>, there will be opportunities to
  1603. add method entry points for functionality that is common to
  1604. the supported <acronym>PHP</acronym> database extensions. This will not affect
  1605. backward compatibility.
  1606. </para>
  1607. </sect2>
  1608. <sect2 id="zend.db.adapter.server-version">
  1609. <title>Retrieving Server Version</title>
  1610. <para>
  1611. Since release 1.7.2, you could retrieve the server version in <acronym>PHP</acronym>
  1612. syntax style to be able to use <methodname>version_compare()</methodname>. If the
  1613. information isn't available, you will receive <constant>NULL</constant>.
  1614. </para>
  1615. <example id="zend.db.adapter.server-version.example">
  1616. <title>Verifying server version before running a query</title>
  1617. <programlisting language="php"><![CDATA[
  1618. $version = $db->getServerVersion();
  1619. if (!is_null($version)) {
  1620. if (version_compare($version, '5.0.0', '>=')) {
  1621. // do something
  1622. } else {
  1623. // do something else
  1624. }
  1625. } else {
  1626. // impossible to read server version
  1627. }
  1628. ]]></programlisting>
  1629. </example>
  1630. </sect2>
  1631. <sect2 id="zend.db.adapter.adapter-notes">
  1632. <title>Notes on Specific Adapters</title>
  1633. <para>
  1634. This section lists differences between the Adapter classes of which
  1635. you should be aware.
  1636. </para>
  1637. <sect3 id="zend.db.adapter.adapter-notes.ibm-db2">
  1638. <title>IBM DB2</title>
  1639. <itemizedlist>
  1640. <listitem>
  1641. <para>
  1642. Specify this Adapter to the <methodname>factory()</methodname> method with
  1643. the name 'Db2'.
  1644. </para>
  1645. </listitem>
  1646. <listitem>
  1647. <para>
  1648. This Adapter uses the <acronym>PHP</acronym> extension ibm_db2.
  1649. </para>
  1650. </listitem>
  1651. <listitem>
  1652. <para>
  1653. IBM DB2 supports both sequences and auto-incrementing
  1654. keys. Therefore the arguments to
  1655. <methodname>lastInsertId()</methodname> are optional. If you give
  1656. no arguments, the Adapter returns the last value
  1657. generated for an auto-increment key. If you give
  1658. arguments, the Adapter returns the last value generated
  1659. by the sequence named according to the convention
  1660. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1661. </para>
  1662. </listitem>
  1663. </itemizedlist>
  1664. </sect3>
  1665. <sect3 id="zend.db.adapter.adapter-notes.mysqli">
  1666. <title>MySQLi</title>
  1667. <itemizedlist>
  1668. <listitem>
  1669. <para>
  1670. Specify this Adapter to the <methodname>factory()</methodname>
  1671. method with the name 'Mysqli'.
  1672. </para>
  1673. </listitem>
  1674. <listitem>
  1675. <para>
  1676. This Adapter utilizes the <acronym>PHP</acronym> extension mysqli.
  1677. </para>
  1678. </listitem>
  1679. <listitem>
  1680. <para>
  1681. MySQL does not support sequences, so
  1682. <methodname>lastInsertId()</methodname> ignores its arguments and
  1683. always returns the last value generated for an
  1684. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1685. method returns <constant>NULL</constant>.
  1686. </para>
  1687. </listitem>
  1688. </itemizedlist>
  1689. </sect3>
  1690. <sect3 id="zend.db.adapter.adapter-notes.oracle">
  1691. <title>Oracle</title>
  1692. <itemizedlist>
  1693. <listitem>
  1694. <para>
  1695. Specify this Adapter to the <methodname>factory()</methodname>
  1696. method with the name 'Oracle'.
  1697. </para>
  1698. </listitem>
  1699. <listitem>
  1700. <para>
  1701. This Adapter uses the <acronym>PHP</acronym> extension oci8.
  1702. </para>
  1703. </listitem>
  1704. <listitem>
  1705. <para>
  1706. Oracle does not support auto-incrementing keys, so you
  1707. should specify the name of a sequence to
  1708. <methodname>lastInsertId()</methodname> or
  1709. <methodname>lastSequenceId()</methodname>.
  1710. </para>
  1711. </listitem>
  1712. <listitem>
  1713. <para>
  1714. The Oracle extension does not support positional
  1715. parameters. You must use named parameters.
  1716. </para>
  1717. </listitem>
  1718. <listitem>
  1719. <para>
  1720. Currently the <constant>Zend_Db::CASE_FOLDING</constant> option
  1721. is not supported by the Oracle adapter. To use this
  1722. option with Oracle, you must use the <acronym>PDO</acronym> OCI adapter.
  1723. </para>
  1724. </listitem>
  1725. <listitem>
  1726. <para>
  1727. By default, LOB fields are returned as OCI-Lob objects. You could
  1728. retrieve them as string for all requests by using driver options
  1729. '<property>lob_as_string</property>' or for particular request by using
  1730. <methodname>setLobAsString(boolean)</methodname> on adapter or on
  1731. statement.
  1732. </para>
  1733. </listitem>
  1734. </itemizedlist>
  1735. </sect3>
  1736. <sect3 id="zend.db.adapter.adapter-notes.sqlsrv">
  1737. <title>Microsoft SQL Server</title>
  1738. <itemizedlist>
  1739. <listitem>
  1740. <para>
  1741. Specify this Adapter to the <methodname>factory()</methodname> method with
  1742. the name 'Sqlsrv'.
  1743. </para>
  1744. </listitem>
  1745. <listitem>
  1746. <para>
  1747. This Adapter uses the <acronym>PHP</acronym> extension sqlsrv
  1748. </para>
  1749. </listitem>
  1750. <listitem>
  1751. <para>
  1752. Microsoft <acronym>SQL</acronym> Server does not support sequences, so
  1753. <methodname>lastInsertId()</methodname> ignores primary key argument and
  1754. returns the last value generated for an auto-increment key if a table name
  1755. is specified or a last insert query returned id. The
  1756. <methodname>lastSequenceId()</methodname> method returns
  1757. <constant>NULL</constant>.
  1758. </para>
  1759. </listitem>
  1760. <listitem>
  1761. <para>
  1762. <classname>Zend_Db_Adapter_Sqlsrv</classname> sets
  1763. <constant>QUOTED_IDENTIFIER ON</constant> immediately after connecting to
  1764. a <acronym>SQL</acronym> Server database. This makes the driver use the
  1765. standard <acronym>SQL</acronym> identifier delimiter symbol
  1766. (<emphasis>"</emphasis>) instead of the proprietary square-brackets
  1767. syntax <acronym>SQL</acronym> Server uses for delimiting identifiers.
  1768. </para>
  1769. </listitem>
  1770. <listitem>
  1771. <para>
  1772. You can specify <property>driver_options</property> as a key in the options
  1773. array. The value can be a anything from here <ulink
  1774. url="http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx">http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx</ulink>.
  1775. </para>
  1776. </listitem>
  1777. <listitem>
  1778. <para>
  1779. You can use <methodname>setTransactionIsolationLevel()</methodname> to set
  1780. isolation level for current connection. The value can be
  1781. <constant>SQLSRV_TXN_READ_UNCOMMITTED</constant>,
  1782. <constant>SQLSRV_TXN_READ_COMMITTED</constant>,
  1783. <constant>SQLSRV_TXN_REPEATABLE_READ</constant>,
  1784. <constant>SQLSRV_TXN_SNAPSHOT</constant> or
  1785. <constant>SQLSRV_TXN_SERIALIZABLE</constant>.
  1786. </para>
  1787. </listitem>
  1788. <listitem>
  1789. <para>
  1790. As of Zend Framework 1.9, the minimal supported build of the
  1791. <acronym>PHP</acronym> <acronym>SQL</acronym> Server extension from
  1792. Microsoft is 1.0.1924.0. and the <acronym>MSSQL</acronym> Server Native
  1793. Client version 9.00.3042.00.
  1794. </para>
  1795. </listitem>
  1796. </itemizedlist>
  1797. </sect3>
  1798. <sect3 id="zend.db.adapter.adapter-notes.pdo-ibm">
  1799. <title>PDO for IBM DB2 and Informix Dynamic Server (IDS)</title>
  1800. <itemizedlist>
  1801. <listitem>
  1802. <para>
  1803. Specify this Adapter to the <methodname>factory()</methodname>
  1804. method with the name '<classname>Pdo_Ibm</classname>'.
  1805. </para>
  1806. </listitem>
  1807. <listitem>
  1808. <para>
  1809. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_ibm.
  1810. </para>
  1811. </listitem>
  1812. <listitem>
  1813. <para>
  1814. You must use at least PDO_IBM extension version 1.2.2.
  1815. If you have an earlier version of this extension, you
  1816. must upgrade the PDO_IBM extension from <acronym>PECL</acronym>.
  1817. </para>
  1818. </listitem>
  1819. </itemizedlist>
  1820. </sect3>
  1821. <sect3 id="zend.db.adapter.adapter-notes.pdo-mssql">
  1822. <title>PDO Microsoft SQL Server</title>
  1823. <itemizedlist>
  1824. <listitem>
  1825. <para>
  1826. Specify this Adapter to the <methodname>factory()</methodname>
  1827. method with the name '<classname>Pdo_Mssql</classname>'.
  1828. </para>
  1829. </listitem>
  1830. <listitem>
  1831. <para>
  1832. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_dblib.
  1833. </para>
  1834. </listitem>
  1835. <listitem>
  1836. <para>
  1837. Microsoft <acronym>SQL</acronym> Server does not support sequences, so
  1838. <methodname>lastInsertId()</methodname> ignores its arguments and
  1839. always returns the last value generated for an
  1840. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1841. method returns <constant>NULL</constant>.
  1842. </para>
  1843. </listitem>
  1844. <listitem>
  1845. <para>
  1846. If you are working with unicode strings in an encoding other than UCS-2
  1847. (such as UTF-8), you may have to perform a conversion in your application
  1848. code or store the data in a binary column. Please refer to <ulink
  1849. url="http://support.microsoft.com/kb/232580">Microsoft's Knowledge
  1850. Base</ulink> for more information.
  1851. </para>
  1852. </listitem>
  1853. <listitem>
  1854. <para>
  1855. <classname>Zend_Db_Adapter_Pdo_Mssql</classname> sets
  1856. <constant>QUOTED_IDENTIFIER ON</constant> immediately after connecting to a
  1857. <acronym>SQL</acronym> Server database. This makes the driver use the
  1858. standard <acronym>SQL</acronym> identifier delimiter symbol (")
  1859. instead of the proprietary square-brackets syntax <acronym>SQL</acronym>
  1860. Server uses for delimiting identifiers.
  1861. </para>
  1862. </listitem>
  1863. <listitem>
  1864. <para>
  1865. You can specify <property>pdoType</property> as a key in the
  1866. options array. The value can be "mssql" (the default),
  1867. "dblib", "freetds", or "sybase". This option affects
  1868. the <acronym>DSN</acronym> prefix the adapter uses when constructing the
  1869. <acronym>DSN</acronym> string. Both "freetds" and "sybase" imply a prefix
  1870. of "sybase:", which is used for the
  1871. <ulink url="http://www.freetds.org/">FreeTDS</ulink> set
  1872. of libraries.
  1873. See also
  1874. <ulink url="http://www.php.net/manual/en/ref.pdo-dblib.connection.php">
  1875. http://www.php.net/manual/en/ref.pdo-dblib.connection.php</ulink>
  1876. for more information on the <acronym>DSN</acronym> prefixes used in this
  1877. driver.
  1878. </para>
  1879. </listitem>
  1880. </itemizedlist>
  1881. </sect3>
  1882. <sect3 id="zend.db.adapter.adapter-notes.pdo-mysql">
  1883. <title>PDO MySQL</title>
  1884. <itemizedlist>
  1885. <listitem>
  1886. <para>
  1887. Specify this Adapter to the <methodname>factory()</methodname>
  1888. method with the name '<classname>Pdo_Mysql</classname>'.
  1889. </para>
  1890. </listitem>
  1891. <listitem>
  1892. <para>
  1893. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_mysql.
  1894. </para>
  1895. </listitem>
  1896. <listitem>
  1897. <para>
  1898. MySQL does not support sequences, so
  1899. <methodname>lastInsertId()</methodname> ignores its arguments and
  1900. always returns the last value generated for an
  1901. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1902. method returns <constant>NULL</constant>.
  1903. </para>
  1904. </listitem>
  1905. </itemizedlist>
  1906. </sect3>
  1907. <sect3 id="zend.db.adapter.adapter-notes.pdo-oci">
  1908. <title>PDO Oracle</title>
  1909. <itemizedlist>
  1910. <listitem>
  1911. <para>
  1912. Specify this Adapter to the <methodname>factory()</methodname>
  1913. method with the name '<classname>Pdo_Oci</classname>'.
  1914. </para>
  1915. </listitem>
  1916. <listitem>
  1917. <para>
  1918. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_oci.
  1919. </para>
  1920. </listitem>
  1921. <listitem>
  1922. <para>
  1923. Oracle does not support auto-incrementing keys, so you
  1924. should specify the name of a sequence to
  1925. <methodname>lastInsertId()</methodname> or
  1926. <methodname>lastSequenceId()</methodname>.
  1927. </para>
  1928. </listitem>
  1929. </itemizedlist>
  1930. </sect3>
  1931. <sect3 id="zend.db.adapter.adapter-notes.pdo-pgsql">
  1932. <title>PDO PostgreSQL</title>
  1933. <itemizedlist>
  1934. <listitem>
  1935. <para>
  1936. Specify this Adapter to the <methodname>factory()</methodname>
  1937. method with the name '<classname>Pdo_Pgsql</classname>'.
  1938. </para>
  1939. </listitem>
  1940. <listitem>
  1941. <para>
  1942. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_pgsql.
  1943. </para>
  1944. </listitem>
  1945. <listitem>
  1946. <para>
  1947. PostgreSQL supports both sequences and auto-incrementing
  1948. keys. Therefore the arguments to
  1949. <methodname>lastInsertId()</methodname> are optional. If you give
  1950. no arguments, the Adapter returns the last value
  1951. generated for an auto-increment key. If you give
  1952. arguments, the Adapter returns the last value generated
  1953. by the sequence named according to the convention
  1954. '<emphasis>table</emphasis>_<emphasis>column</emphasis>_seq'.
  1955. </para>
  1956. </listitem>
  1957. </itemizedlist>
  1958. </sect3>
  1959. <sect3 id="zend.db.adapter.adapter-notes.pdo-sqlite">
  1960. <title>PDO SQLite</title>
  1961. <itemizedlist>
  1962. <listitem>
  1963. <para>
  1964. Specify this Adapter to the <methodname>factory()</methodname>
  1965. method with the name '<classname>Pdo_Sqlite</classname>'.
  1966. </para>
  1967. </listitem>
  1968. <listitem>
  1969. <para>
  1970. This Adapter uses the <acronym>PHP</acronym> extensions pdo and pdo_sqlite.
  1971. </para>
  1972. </listitem>
  1973. <listitem>
  1974. <para>
  1975. SQLite does not support sequences, so
  1976. <methodname>lastInsertId()</methodname> ignores its arguments and
  1977. always returns the last value generated for an
  1978. auto-increment key. The <methodname>lastSequenceId()</methodname>
  1979. method returns <constant>NULL</constant>.
  1980. </para>
  1981. </listitem>
  1982. <listitem>
  1983. <para>
  1984. To connect to an SQLite2 database, specify
  1985. <command>'sqlite2' => true</command> in the array of
  1986. parameters when creating an instance of the
  1987. <classname>Pdo_Sqlite</classname> Adapter.
  1988. </para>
  1989. </listitem>
  1990. <listitem>
  1991. <para>
  1992. To connect to an in-memory SQLite database,
  1993. specify <command>'dbname' => ':memory:'</command> in the
  1994. array of parameters when creating an instance of
  1995. the <classname>Pdo_Sqlite</classname> Adapter.
  1996. </para>
  1997. </listitem>
  1998. <listitem>
  1999. <para>
  2000. Older versions of the SQLite driver for <acronym>PHP</acronym> do not seem
  2001. to support the PRAGMA commands necessary to ensure that
  2002. short column names are used in result sets. If you
  2003. have problems that your result sets are returned with
  2004. keys of the form "tablename.columnname" when you do a
  2005. join query, then you should upgrade to the current
  2006. version of <acronym>PHP</acronym>.
  2007. </para>
  2008. </listitem>
  2009. </itemizedlist>
  2010. </sect3>
  2011. <sect3 id="zend.db.adapter.adapter-notes.firebird">
  2012. <title>Firebird/Interbase</title>
  2013. <itemizedlist>
  2014. <listitem>
  2015. <para>
  2016. This Adapter uses the <acronym>PHP</acronym> extension php_interbase.
  2017. </para>
  2018. </listitem>
  2019. <listitem>
  2020. <para>
  2021. Firebird/interbase does not support auto-incrementing keys,
  2022. so you should specify the name of a sequence to
  2023. <methodname>lastInsertId()</methodname> or
  2024. <methodname>lastSequenceId()</methodname>.
  2025. </para>
  2026. </listitem>
  2027. <listitem>
  2028. <para>
  2029. Currently the <constant>Zend_Db::CASE_FOLDING</constant> option
  2030. is not supported by the Firebird/interbase adapter.
  2031. Unquoted identifiers are automatically returned in
  2032. upper case.
  2033. </para>
  2034. </listitem>
  2035. <listitem>
  2036. <para>Adapter name is <classname>ZendX_Db_Adapter_Firebird</classname>.</para>
  2037. <para>
  2038. Remember to use the param adapterNamespace with value
  2039. <classname>ZendX_Db_Adapter</classname>.
  2040. </para>
  2041. <para>
  2042. We recommend to update the <filename>gds32.dll</filename> (or linux
  2043. equivalent) bundled with php, to the same version of the server. For
  2044. Firebird the equivalent <filename>gds32.dll</filename> is
  2045. <filename>fbclient.dll</filename>.
  2046. </para>
  2047. <para>
  2048. By default all identifiers (tables names, fields) are returned in upper
  2049. case.
  2050. </para>
  2051. </listitem>
  2052. </itemizedlist>
  2053. </sect3>
  2054. </sect2>
  2055. </sect1>
  2056. <!--
  2057. vim:se ts=4 sw=4 et:
  2058. -->