Zend_Db_Adapter.xml 100 KB

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