Zend_Db_Adapter.xml 97 KB

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