Zend_Db_Adapter.xml 102 KB

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