Zend_Db_Adapter.xml 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19173 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.db.adapter">
  5. <title>Zend_Db_Adapter</title>
  6. <para>
  7. <classname>Zend_Db</classname>
  8. y sus clases relacionadas proporcionan una interfaz
  9. simple de base de datos
  10. <acronym>SQL</acronym>
  11. para Zend Framework. El
  12. <classname>Zend_Db_Adapter</classname>
  13. es la clase base que se utiliza para conectar su
  14. aplicación
  15. <acronym>PHP</acronym>
  16. A una base de datos (
  17. <acronym>RDBMS</acronym>
  18. ). Existen diferentes
  19. clases Adapters(Adaptador) para cada tipo de base de datos
  20. (
  21. <acronym>RDBMS</acronym>
  22. ).
  23. </para>
  24. <para>
  25. Las clases Adapters de
  26. <classname>Zend_Db</classname>
  27. crean un puente entre las extensiones de base de
  28. datos de
  29. <acronym>PHP</acronym>
  30. hacia una interfaz común, para ayudarle a escribir
  31. aplicaciones
  32. <acronym>PHP</acronym>
  33. una sola vez y poder desplegar múltiples
  34. tipos de base de datos (
  35. <acronym>RDBMS</acronym>
  36. ) con muy poco esfuerzo.
  37. </para>
  38. <para>
  39. La Interfaz de la clase adaptador (adapter) es similar a la
  40. intefaz de la extensión
  41. <ulink url="http://www.php.net/pdo">PHP Data Objects</ulink>
  42. .
  43. <classname>Zend_Db</classname>
  44. proporciona clases Adaptadoras para los drivers
  45. <acronym>PDO</acronym>
  46. de
  47. los siguientes tipos de
  48. <acronym>RDBMS</acronym>
  49. :
  50. </para>
  51. <itemizedlist>
  52. <listitem>
  53. <para>
  54. IBM DB2 e Informix Dynamic Server (IDS), usando la
  55. extensión
  56. <acronym>PHP</acronym>
  57. <ulink url="http://www.php.net/pdo-ibm">pdo_ibm</ulink>
  58. </para>
  59. </listitem>
  60. <listitem>
  61. <para>
  62. MySQL, usando la extensión
  63. <acronym>PHP</acronym>
  64. <ulink url="http://www.php.net/pdo-mysql">
  65. pdo_mysql
  66. </ulink>
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. Microsoft SQL Server, usando la extensión
  72. <acronym>PHP</acronym>
  73. <ulink url="http://www.php.net/pdo-mssql">
  74. pdo_mssql
  75. </ulink>
  76. </para>
  77. </listitem>
  78. <listitem>
  79. <para>
  80. Oracle, usando la extensión
  81. <acronym>PHP</acronym>
  82. <ulink url="http://www.php.net/pdo-oci">pdo_oci</ulink>
  83. </para>
  84. </listitem>
  85. <listitem>
  86. <para>
  87. PostgreSQL, usando la extensión
  88. <acronym>PHP</acronym>
  89. <ulink url="http://www.php.net/pdo-pgsql">
  90. pdo_pgsql
  91. </ulink>
  92. </para>
  93. </listitem>
  94. <listitem>
  95. <para>
  96. SQLite, usando la extensión
  97. <acronym>PHP</acronym>
  98. <ulink url="http://www.php.net/pdo-sqlite">
  99. pdo_sqlite
  100. </ulink>
  101. </para>
  102. </listitem>
  103. </itemizedlist>
  104. <para>
  105. Ademas,
  106. <classname>Zend_Db</classname>
  107. proporciona clases Adaptadoras que utilizan las
  108. extensiones de base de datos de
  109. <acronym>PHP</acronym>
  110. de los siguientes tipos:
  111. </para>
  112. <itemizedlist>
  113. <listitem>
  114. <para>
  115. MySQL, usando la extensión
  116. <acronym>PHP</acronym>
  117. <ulink url="http://www.php.net/mysqli">mysqli</ulink>
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para>
  122. Oracle, usando la extensión
  123. <acronym>PHP</acronym>
  124. <ulink url="http://www.php.net/oci8">oci8</ulink>
  125. </para>
  126. </listitem>
  127. <listitem>
  128. <para>
  129. IBM DB2, usando la extensión
  130. <acronym>PHP</acronym>
  131. <ulink url="http://www.php.net/ibm_db2">ibm_db2</ulink>
  132. </para>
  133. </listitem>
  134. <listitem>
  135. <para>
  136. Firebird/Interbase, usando la extensión
  137. <acronym>PHP</acronym>
  138. <ulink url="http://www.php.net/ibase">
  139. php_interbase
  140. </ulink>
  141. </para>
  142. </listitem>
  143. </itemizedlist>
  144. <note>
  145. <para>
  146. Cada Zend_Db_Adaptador utiliza una extensión
  147. <acronym>PHP</acronym>
  148. . Se debe de
  149. tener habilitada la respectiva extensión en su entorno
  150. <acronym>PHP</acronym>
  151. para utilizar un
  152. <classname>Zend_Db_Adapter</classname>
  153. . Por ejemplo, si se utiliza
  154. una clase
  155. <classname>Zend_Db_Adapter</classname>
  156. basada en
  157. <acronym>PDO</acronym>
  158. , tiene que
  159. habilitar tanto la extensión
  160. <acronym>PDO</acronym>
  161. como el driver
  162. <acronym>PDO</acronym>
  163. del tipo
  164. de base de datos que se utiliza.
  165. </para>
  166. </note>
  167. <sect2 id="zend.db.adapter.connecting">
  168. <title>
  169. Conexión a una Base de Datos utilizando un Adaptador
  170. </title>
  171. <para>
  172. Esta sección describe cómo crear una instancia de un
  173. Adaptador de base de datos. Esto
  174. corresponde a establecer
  175. una conexión a un servidor de Base de Datos (
  176. <acronym>RDBMS</acronym>
  177. ) desde su
  178. aplicación
  179. <acronym>PHP</acronym>
  180. .
  181. </para>
  182. <sect3 id="zend.db.adapter.connecting.constructor">
  183. <title>Usando un Constructor de Zend_Db Adapter</title>
  184. <para>
  185. Se puede crear una instancia de un Adaptador utilizando
  186. su constructor. Un
  187. constructor de adaptador toma un
  188. argumento, que es un conjunto de parámetros
  189. utilizados
  190. para declarar la conexión.
  191. </para>
  192. <example id="zend.db.adapter.connecting.constructor.example">
  193. <title>Usando el Constructor de un Adaptador</title>
  194. <programlisting language="php"><![CDATA[
  195. $db = new Zend_Db_Adapter_Pdo_Mysql(array(
  196. 'host' => '127.0.0.1',
  197. 'username' => 'webuser',
  198. 'password' => 'xxxxxxxx',
  199. 'dbname' => 'test'
  200. ));
  201. ]]></programlisting>
  202. </example>
  203. </sect3>
  204. <sect3 id="zend.db.adapter.connecting.factory">
  205. <title>Usando el Zend_Db Factory</title>
  206. <para>
  207. Como alternativa a la utilización directa del
  208. constructor de un adaptador, se puede
  209. crear una
  210. instancia del adaptador que use el método estático
  211. <methodname>Zend_Db::factory()</methodname>
  212. . Este método carga dinámicamente el archivo de clase
  213. Adaptador bajo demanda, usando
  214. <link linkend="zend.loader.load.class">
  215. Zend_Loader::loadClass()
  216. </link>
  217. .
  218. </para>
  219. <para>
  220. El primer argumento es una cadena que nombra al nombre base
  221. de la clase Adaptador.
  222. Por ejemplo, la cadena
  223. '
  224. <classname>Pdo_Mysql</classname>
  225. ' corresponde a la clase
  226. <classname>Zend_Db_Adapter_Pdo_Mysql</classname>
  227. . El segundo argumento es el
  228. mismo array de parámetros que hubiera enviado al
  229. constructor del adaptador.
  230. </para>
  231. <example id="zend.db.adapter.connecting.factory.example">
  232. <title>Usando el Adaptador del método factory</title>
  233. <programlisting language="php"><![CDATA[
  234. // No necesitamos la siguiente declaración, porque
  235. // el archivo Zend_Db_Adapter_Pdo_Mysql será cargado para nosotros por el método
  236. // factory de Zend_Db.
  237. // require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  238. // carga automaticamente la clase Zend_Db_Adapter_Pdo_Mysql
  239. // y crea una instancia de la misma
  240. $db = Zend_Db::factory('Pdo_Mysql', array(
  241. 'host' => '127.0.0.1',
  242. 'username' => 'webuser',
  243. 'password' => 'xxxxxxxx',
  244. 'dbname' => 'test'
  245. ));
  246. ]]></programlisting>
  247. </example>
  248. <para>
  249. Si crea su propia clase que extiende a
  250. <classname>Zend_Db_Adapter_Abstract</classname>
  251. , pero no nombra su clase con el prefijo
  252. de paquete "
  253. <classname>Zend_Db_Adapter</classname>
  254. ", se puede utilizar el método
  255. <methodname>factory()</methodname>
  256. para cargar su adaptador si se especifica la parte principal
  257. de la clase del
  258. adaptador con la clave "adapterNamespace" en
  259. el conjunto de parámetros
  260. </para>
  261. <example id="zend.db.adapter.connecting.factory.example2">
  262. <title>
  263. Usando el método factory para una clase Adaptador
  264. personalizada
  265. </title>
  266. <programlisting language="php"><![CDATA[
  267. // No tenemos que cargar el archivo de clase Adaptador
  268. // porque será cargado para nosotros por el método factory de Zend_Db.
  269. // Automáticamente carga la clase MyProject_Db_Adapter_Pdo_Mysql
  270. // y crea una instancia de ella.
  271. $db = Zend_Db::factory('Pdo_Mysql', array(
  272. 'host' => '127.0.0.1',
  273. 'username' => 'webuser',
  274. 'password' => 'xxxxxxxx',
  275. 'dbname' => 'test',
  276. 'adapterNamespace' => 'MyProject_Db_Adapter'
  277. ));
  278. ]]></programlisting>
  279. </example>
  280. </sect3>
  281. <sect3 id="zend.db.adapter.connecting.factory-config">
  282. <title>Uso de Zend_Config con Zend_Db Factory</title>
  283. <para>
  284. Opcionalmente, se puede especificar cualquier
  285. argumento del método
  286. <methodname>factory()</methodname>
  287. como un objeto de tipo
  288. <link linkend="zend.config">Zend_Config</link>
  289. .
  290. </para>
  291. <para>
  292. Si el primer argumento es un objeto de configuración, se
  293. espera que contenga una
  294. propiedad llamada
  295. <property>adapter</property>
  296. , conteniendo la cadena que da nombre al nombre base de la
  297. clase de adaptador.
  298. Opcionalmente, el objeto puede
  299. contener una propiedad llamada
  300. <property>params</property>
  301. , con subpropiedades correspondientes a nombres de parámetros
  302. del adaptador. Esto es
  303. usado sólo si el segundo
  304. argumento del método
  305. <methodname>factory()</methodname>
  306. se ha omitido.
  307. </para>
  308. <example id="zend.db.adapter.connecting.factory.example1">
  309. <title>
  310. Uso del método factory del Adaptador con un objeto Zend_Config
  311. </title>
  312. <para>
  313. En el siguiente ejemplo, un objeto
  314. <classname>Zend_Config</classname>
  315. es
  316. creado usando un array. También puedes cargar los datos de
  317. un archivo externo,
  318. por ejemplo con
  319. <link linkend="zend.config.adapters.ini">
  320. Zend_Config_Ini
  321. </link>
  322. o
  323. <link linkend="zend.config.adapters.xml">
  324. Zend_Config_Xml
  325. </link>
  326. .
  327. </para>
  328. <programlisting language="php"><![CDATA[
  329. $config = new Zend_Config(
  330. array(
  331. 'database' => array(
  332. 'adapter' => 'Mysqli',
  333. 'params' => array(
  334. 'dbname' => 'test',
  335. 'username' => 'webuser',
  336. 'password' => 'secret',
  337. )
  338. )
  339. )
  340. );
  341. $db = Zend_Db::factory($config->database);
  342. ]]></programlisting>
  343. </example>
  344. <para>
  345. El segundo argumento del método
  346. <methodname>factory()</methodname>
  347. puede ser un array asociativo con entradas
  348. correspondientes a los parámetros del
  349. adaptador. Este argumento es
  350. opcional. Si el primer argumento es de tipo
  351. <classname>Zend_Config</classname>
  352. ,
  353. se asume que tiene todos los parametros, y el segundo
  354. argumento es ignorado.
  355. </para>
  356. </sect3>
  357. <sect3 id="zend.db.adapter.connecting.parameters">
  358. <title>Parámetros del Adaptador</title>
  359. <para>
  360. El siguiente listado explica parámetros comunes reconocidos por
  361. Adaptador de clases
  362. <classname>Zend_Db</classname>
  363. .
  364. </para>
  365. <itemizedlist>
  366. <listitem>
  367. <para>
  368. <emphasis>host</emphasis>
  369. : una string conteniendo un nombre de host o dirección IP
  370. del servidor de
  371. base de datos. Si la base de datos está corriendo
  372. sobre el mismo host que la
  373. aplicación
  374. <acronym>PHP</acronym>
  375. ,
  376. usted puede utilizar 'localhost' o '127.0.0.1'.
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. <emphasis>username</emphasis>
  382. : identificador de cuenta para autenticar una conexión al
  383. servidor
  384. <acronym>RDBMS</acronym>
  385. .
  386. </para>
  387. </listitem>
  388. <listitem>
  389. <para>
  390. <emphasis>password</emphasis>
  391. : la contraseña de la cuenta para la autenticación de credenciales
  392. de
  393. conexión con el servidor
  394. <acronym>RDBMS</acronym>
  395. </para>
  396. </listitem>
  397. <listitem>
  398. <para>
  399. <emphasis>dbname</emphasis>
  400. : nombre de la base de datos en el servidor
  401. <acronym>RDBMS</acronym>
  402. .
  403. </para>
  404. </listitem>
  405. <listitem>
  406. <para>
  407. <emphasis>port</emphasis>
  408. : algunos servidores
  409. <acronym>RDBMS</acronym>
  410. pueden aceptar conexiones de red
  411. sobre un número de puerto específico.
  412. El
  413. parámetro del puerto le permite especificar el puerto al
  414. que su aplicación
  415. <acronym>PHP</acronym>
  416. se conecta, para que concuerde el puerto
  417. configurado en el servidor
  418. <acronym>RDBMS</acronym>
  419. .
  420. </para>
  421. </listitem>
  422. <listitem>
  423. <para>
  424. <emphasis>charset</emphasis>
  425. :
  426. specify the charset used for the connection.
  427. </para>
  428. </listitem>
  429. <listitem>
  430. <para>
  431. <emphasis>options</emphasis>
  432. :
  433. : este parámetro es un array asociativo de
  434. opciones que son genéricas a
  435. todas las clases
  436. <classname>Zend_Db_Adapter</classname>
  437. .
  438. </para>
  439. </listitem>
  440. <listitem>
  441. <para>
  442. <emphasis>
  443. driver_options
  444. </emphasis>
  445. : este parámetro es un array asociativo de opciones adicionales
  446. para una
  447. extensión de base de datos dada.
  448. un uso típico de este parámetro es
  449. establecer atributos
  450. de un driver
  451. <acronym>PDO</acronym>
  452. .
  453. </para>
  454. </listitem>
  455. <listitem>
  456. <para>
  457. <emphasis>
  458. adapterNamespace
  459. </emphasis>
  460. : nombre de la parte inicial del nombre de las clase para el
  461. adaptador, en
  462. lugar de '
  463. <classname>Zend_Db_Adapter</classname>
  464. '. Utilice
  465. esto si usted necesita usar el método
  466. <methodname>factory()</methodname>
  467. para cargar un adaptador de clase de base de datos que no sea
  468. de Zend.
  469. </para>
  470. </listitem>
  471. </itemizedlist>
  472. <example id="zend.db.adapter.connecting.parameters.example1">
  473. <title>
  474. Passing the case-folding option to the factory
  475. </title>
  476. <para>
  477. Usted puede pasar esta opción específica por la constante
  478. <constant>Zend_Db::CASE_FOLDING</constant>
  479. . Este corresponde al atributo
  480. <constant>ATTR_CASE</constant>
  481. en los drivers de base de datos
  482. <acronym>PDO</acronym>
  483. e IBM DB2,
  484. ajustando la sensibilidad de las claves tipo cadena en los resultados
  485. de consultas. La opción toma los valores
  486. <constant>Zend_Db::CASE_NATURAL</constant>
  487. (el predeterminado),
  488. <constant>Zend_Db::CASE_UPPER</constant>
  489. , y
  490. <constant>Zend_Db::CASE_LOWER</constant>
  491. .
  492. </para>
  493. <programlisting language="php"><![CDATA[
  494. $options = array(
  495. Zend_Db::CASE_FOLDING => Zend_Db::CASE_UPPER
  496. );
  497. $params = array(
  498. 'host' => '127.0.0.1',
  499. 'username' => 'webuser',
  500. 'password' => 'xxxxxxxx',
  501. 'dbname' => 'test',
  502. 'options' => $options
  503. );
  504. $db = Zend_Db::factory('Db2', $params);
  505. ]]></programlisting>
  506. </example>
  507. <example id="zend.db.adapter.connecting.parameters.example2">
  508. <title>
  509. Passing the auto-quoting option to the factory
  510. </title>
  511. <para>
  512. Usted puede especificar esta opción por la constante
  513. <constant>Zend_Db::AUTO_QUOTE_IDENTIFIERS</constant>
  514. . Si el valor es
  515. <constant>TRUE</constant>
  516. (el predeterminado), los identificadores como nombres de tabla,
  517. nombres de
  518. columna, e incluso los alias son delimitados en la
  519. sintaxis
  520. <acronym>SQL</acronym>
  521. generada por el Adatador del objeto.
  522. Esto hace que sea sencillo utilizar
  523. identificadores que contengan
  524. palabras reservadas de
  525. <acronym>SQL</acronym>
  526. , o caracteres especiales. Si el valor es
  527. <constant>FALSE</constant>
  528. , los identificadores no son delimitados automáticamente. Si
  529. usted necesita
  530. delimitar identificadores, debe hacer usted mismo
  531. utilizando el método
  532. <methodname>quoteIdentifier()</methodname>
  533. .
  534. </para>
  535. <programlisting language="php"><![CDATA[
  536. $options = array(
  537. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  538. );
  539. $params = array(
  540. 'host' => '127.0.0.1',
  541. 'username' => 'webuser',
  542. 'password' => 'xxxxxxxx',
  543. 'dbname' => 'test',
  544. 'options' => $options
  545. );
  546. $db = Zend_Db::factory('Pdo_Mysql', $params);
  547. ]]></programlisting>
  548. </example>
  549. <example id="zend.db.adapter.connecting.parameters.example3">
  550. <title>Passing PDO driver options to the factory</title>
  551. <programlisting language="php"><![CDATA[
  552. $pdoParams = array(
  553. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
  554. );
  555. $params = array(
  556. 'host' => '127.0.0.1',
  557. 'username' => 'webuser',
  558. 'password' => 'xxxxxxxx',
  559. 'dbname' => 'test',
  560. 'driver_options' => $pdoParams
  561. );
  562. $db = Zend_Db::factory('Pdo_Mysql', $params);
  563. echo $db->getConnection()
  564. ->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY);
  565. ]]></programlisting>
  566. </example>
  567. <example id="zend.db.adapter.connecting.parameters.example4">
  568. <title>Passing Serialization Options to the Factory</title>
  569. <programlisting language="php"><![CDATA[
  570. $options = array(
  571. Zend_Db::ALLOW_SERIALIZATION => false
  572. );
  573. $params = array(
  574. 'host' => '127.0.0.1',
  575. 'username' => 'webuser',
  576. 'password' => 'xxxxxxxx',
  577. 'dbname' => 'test',
  578. 'options' => $options
  579. );
  580. $db = Zend_Db::factory('Pdo_Mysql', $params);
  581. ]]></programlisting>
  582. </example>
  583. </sect3>
  584. <sect3 id="zend.db.adapter.connecting.getconnection">
  585. <title>Managing Lazy Connections</title>
  586. <para>
  587. Creating an instance of an Adapter class does not immediately
  588. connect to the
  589. <acronym>RDBMS</acronym>
  590. server. The Adapter saves the connection
  591. parameters, and makes the actual connection
  592. on demand, the
  593. first time you need to execute a query. This ensures that
  594. creating an
  595. Adapter object is quick and inexpensive. You can
  596. create an instance of an Adapter
  597. even if you are not certain
  598. that you need to run any database queries during the
  599. current
  600. request your application is serving.
  601. </para>
  602. <para>
  603. If you need to force the Adapter to connect to the
  604. <acronym>RDBMS</acronym>
  605. , use
  606. the
  607. <methodname>getConnection()</methodname>
  608. method. This method returns
  609. an object for the connection as represented by the
  610. respective
  611. <acronym>PHP</acronym>
  612. database extension. For example, if you use any of the
  613. Adapter classes for
  614. <acronym>PDO</acronym>
  615. drivers, then
  616. <methodname>getConnection()</methodname>
  617. returns the
  618. <acronym>PDO</acronym>
  619. object,
  620. after initiating it as a live connection to the specific database.
  621. </para>
  622. <para>
  623. It can be useful to force the connection if you want to catch
  624. any exceptions it
  625. throws as a result of invalid account
  626. credentials, or other failure to connect to the
  627. <acronym>RDBMS</acronym>
  628. server.
  629. These exceptions are not thrown until the connection is made,
  630. so it can help
  631. simplify your application code if you handle the
  632. exceptions in one place, instead of
  633. at the time of
  634. the first query against the database.
  635. </para>
  636. <para>
  637. Additionally, an adapter can get serialized to store it, for example,
  638. in a session
  639. variable. This can be very useful not only for the
  640. adapter itself, but for other
  641. objects that aggregate it, like a
  642. <classname>Zend_Db_Select</classname>
  643. object. By default, adapters are allowed
  644. to be serialized, if you don't want it, you
  645. should consider passing the
  646. <constant>Zend_Db::ALLOW_SERIALIZATION</constant>
  647. option with
  648. <constant>FALSE</constant>
  649. , see the example above. To respect lazy connections
  650. principle, the adapter won't
  651. reconnect itself after being unserialized. You must
  652. then call
  653. <methodname>getConnection()</methodname>
  654. yourself. You can make the
  655. adapter auto-reconnect by passing the
  656. <constant>Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE</constant>
  657. with
  658. <constant>TRUE</constant>
  659. as an adapter option.
  660. </para>
  661. <example id="zend.db.adapter.connecting.getconnection.example">
  662. <title>Handling connection exceptions</title>
  663. <programlisting language="php"><![CDATA[
  664. try {
  665. $db = Zend_Db::factory('Pdo_Mysql', $parameters);
  666. $db->getConnection();
  667. } catch (Zend_Db_Adapter_Exception $e) {
  668. // perhaps a failed login credential, or perhaps the RDBMS is not running
  669. } catch (Zend_Exception $e) {
  670. // perhaps factory() failed to load the specified Adapter class
  671. }
  672. ]]></programlisting>
  673. </example>
  674. </sect3>
  675. </sect2>
  676. <sect2 id="zend.db.adapter.example-database">
  677. <title>La base de datos de ejemplo</title>
  678. <para>
  679. En la documentación de las clases
  680. <classname>Zend_Db</classname>
  681. , usamos un
  682. conjunto sencillo de tablas para ilustrar el uso de las
  683. clases y métodos.
  684. Estas tablas de ejemplo permiten almacenar
  685. información para localizar bugs en un proyecto
  686. de desarrollo
  687. de software. La base de datos contiene cuatro tablas:
  688. </para>
  689. <itemizedlist>
  690. <listitem>
  691. <para>
  692. <emphasis>accounts</emphasis>
  693. almacena información sobre cada usuario que hace el
  694. seguimiento de bugs.
  695. </para>
  696. </listitem>
  697. <listitem>
  698. <para>
  699. <emphasis>products</emphasis>
  700. almacena información sobre cada producto para el que
  701. pueden registrarse bugs.
  702. </para>
  703. </listitem>
  704. <listitem>
  705. <para>
  706. <emphasis>bugs</emphasis>
  707. almacena información sobre bugs, incluyendo el
  708. estado actual del bug, la persona
  709. que informó sobre
  710. el bug, la persona que está asignada para corregir
  711. el bug, y la
  712. persona que está asignada para
  713. verificar la corrección.
  714. </para>
  715. </listitem>
  716. <listitem>
  717. <para>
  718. <emphasis>bugs_products</emphasis>
  719. stores a relationship between bugs and products.
  720. This implements a many-to-many
  721. relationship, because
  722. a given bug may be relevant to multiple products,
  723. and of
  724. course a given product can have multiple
  725. bugs.
  726. </para>
  727. </listitem>
  728. </itemizedlist>
  729. <para>
  730. La siguiente definición de datos
  731. <acronym>SQL</acronym>
  732. en lenguaje
  733. pseudocódigo describe las tablas de esta base de datos de
  734. ejemplo. Estas
  735. tablas de ejemplo son usadas ampliamente por
  736. los tests unitarios automatizados de
  737. <classname>Zend_Db</classname>
  738. .
  739. </para>
  740. <programlisting language="sql"><![CDATA[
  741. CREATE TABLE accounts (
  742. account_name VARCHAR(100) NOT NULL PRIMARY KEY
  743. );
  744. CREATE TABLE products (
  745. product_id INTEGER NOT NULL PRIMARY KEY,
  746. product_name VARCHAR(100)
  747. );
  748. CREATE TABLE bugs (
  749. bug_id INTEGER NOT NULL PRIMARY KEY,
  750. bug_description VARCHAR(100),
  751. bug_status VARCHAR(20),
  752. reported_by VARCHAR(100) REFERENCES accounts(account_name),
  753. assigned_to VARCHAR(100) REFERENCES accounts(account_name),
  754. verified_by VARCHAR(100) REFERENCES accounts(account_name)
  755. );
  756. CREATE TABLE bugs_products (
  757. bug_id INTEGER NOT NULL REFERENCES bugs,
  758. product_id INTEGER NOT NULL REFERENCES products,
  759. PRIMARY KEY (bug_id, product_id)
  760. );
  761. ]]></programlisting>
  762. <para>
  763. Also notice that the
  764. <code>bugs</code>
  765. table contains multiple
  766. foreign key references to the
  767. <code>accounts</code>
  768. table.
  769. Each of these foreign keys may reference a different row in the
  770. <code>accounts</code>
  771. table for a given bug.
  772. </para>
  773. <para>
  774. The diagram below illustrates the physical data model of the
  775. example database.
  776. </para>
  777. <para>
  778. <inlinegraphic width="387" scale="100" align="center" valign="middle"
  779. fileref="figures/zend.db.adapter.example-database.png" format="PNG"/>
  780. </para>
  781. </sect2>
  782. <sect2 id="zend.db.adapter.select">
  783. <title>Reading Query Results</title>
  784. <para>
  785. This section describes methods of the Adapter class with which you
  786. can run SELECT
  787. queries and retrieve the query results.
  788. </para>
  789. <sect3 id="zend.db.adapter.select.fetchall">
  790. <title>Fetching a Complete Result Set</title>
  791. <para>
  792. You can run a
  793. <acronym>SQL</acronym>
  794. SELECT query and retrieve its results in one
  795. step using the
  796. <methodname>fetchAll()</methodname>
  797. method.
  798. </para>
  799. <para>
  800. The first argument to this method is a string containing a
  801. SELECT statement.
  802. Alternatively, the first argument can be an
  803. object of class
  804. <link linkend="zend.db.select">Zend_Db_Select</link>
  805. .
  806. The Adapter automatically converts this object to a string
  807. representation of the
  808. SELECT statement.
  809. </para>
  810. <para>
  811. The second argument to
  812. <methodname>fetchAll()</methodname>
  813. is an array of
  814. values to substitute for parameter placeholders in the
  815. <acronym>SQL</acronym>
  816. statement.
  817. </para>
  818. <example id="zend.db.adapter.select.fetchall.example">
  819. <title>Using fetchAll()</title>
  820. <programlisting language="php"><![CDATA[
  821. $sql = 'SELECT * FROM bugs WHERE bug_id = ?';
  822. $result = $db->fetchAll($sql, 2);
  823. ]]></programlisting>
  824. </example>
  825. </sect3>
  826. <sect3 id="zend.db.adapter.select.fetch-mode">
  827. <title>Changing the Fetch Mode</title>
  828. <para>
  829. By default,
  830. <methodname>fetchAll()</methodname>
  831. returns an array of
  832. rows, each of which is an associative array. The keys of the
  833. associative array are the columns or column aliases named in
  834. the select query.
  835. </para>
  836. <para>
  837. You can specify a different style of fetching results using the
  838. <methodname>setFetchMode()</methodname>
  839. method. The modes supported are
  840. identified by constants:
  841. </para>
  842. <itemizedlist>
  843. <listitem>
  844. <para>
  845. <constant>Zend_Db::FETCH_ASSOC</constant>
  846. :
  847. return data in an array of associative arrays.
  848. The array keys are column
  849. names, as strings. This is the default fetch mode
  850. for
  851. <classname>Zend_Db_Adapter</classname>
  852. classes.
  853. </para>
  854. <para>
  855. Note that if your select-list contains more than one
  856. column with the same
  857. name, for example if they are from
  858. two different tables in a JOIN, there can
  859. be only one
  860. entry in the associative array for a given name.
  861. If you use the
  862. FETCH_ASSOC mode, you should specify
  863. column aliases in your SELECT query to
  864. ensure that the
  865. names result in unique array keys.
  866. </para>
  867. <para>
  868. By default, these strings are returned as they are
  869. returned by the database
  870. driver. This is typically the
  871. spelling of the column in the
  872. <acronym>RDBMS</acronym>
  873. server. You can
  874. specify the case for these strings, using the
  875. <constant>Zend_Db::CASE_FOLDING</constant>
  876. option.
  877. Specify this when instantiating the Adapter.
  878. See
  879. <xref linkend="zend.db.adapter.connecting.parameters.example1"/>
  880. .
  881. </para>
  882. </listitem>
  883. <listitem>
  884. <para>
  885. <constant>Zend_Db::FETCH_NUM</constant>
  886. :
  887. return data in an array of arrays. The arrays are
  888. indexed by integers,
  889. corresponding to the position of
  890. the respective field in the select-list of
  891. the query.
  892. </para>
  893. </listitem>
  894. <listitem>
  895. <para>
  896. <constant>Zend_Db::FETCH_BOTH</constant>
  897. :
  898. return data in an array of arrays. The array keys are
  899. both strings as used
  900. in the FETCH_ASSOC mode, and
  901. integers as used in the FETCH_NUM mode. Note
  902. that the
  903. number of elements in the array is double that which
  904. would be in the
  905. array if you used either FETCH_ASSOC
  906. or FETCH_NUM.
  907. </para>
  908. </listitem>
  909. <listitem>
  910. <para>
  911. <constant>Zend_Db::FETCH_COLUMN</constant>
  912. :
  913. return data in an array of values. The value in each array
  914. is the value
  915. returned by one column of the result set.
  916. By default, this is the first
  917. column, indexed by 0.
  918. </para>
  919. </listitem>
  920. <listitem>
  921. <para>
  922. <constant>Zend_Db::FETCH_OBJ</constant>
  923. :
  924. return data in an array of objects. The default class
  925. is the
  926. <acronym>PHP</acronym>
  927. built-in class stdClass. Columns of the
  928. result set are available as public
  929. properties of the
  930. object.
  931. </para>
  932. </listitem>
  933. </itemizedlist>
  934. <example id="zend.db.adapter.select.fetch-mode.example">
  935. <title>Using setFetchMode()</title>
  936. <programlisting language="php"><![CDATA[
  937. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  938. $result = $db->fetchAll('SELECT * FROM bugs WHERE bug_id = ?', 2);
  939. // $result is an array of objects
  940. echo $result[0]->bug_description;
  941. ]]></programlisting>
  942. </example>
  943. </sect3>
  944. <sect3 id="zend.db.adapter.select.fetchassoc">
  945. <title>Fetching a Result Set as an Associative Array</title>
  946. <para>
  947. The
  948. <methodname>fetchAssoc()</methodname>
  949. method returns data in an array
  950. of associative arrays, regardless of what value you
  951. have set
  952. for the fetch mode.
  953. </para>
  954. <example id="zend.db.adapter.select.fetchassoc.example">
  955. <title>Using fetchAssoc()</title>
  956. <programlisting language="php"><![CDATA[
  957. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  958. $result = $db->fetchAssoc('SELECT * FROM bugs WHERE bug_id = ?', 2);
  959. // $result is an array of associative arrays, in spite of the fetch mode
  960. echo $result[0]['bug_description'];
  961. ]]></programlisting>
  962. </example>
  963. </sect3>
  964. <sect3 id="zend.db.adapter.select.fetchcol">
  965. <title>Fetching a Single Column from a Result Set</title>
  966. <para>
  967. The
  968. <methodname>fetchCol()</methodname>
  969. method returns data in an array
  970. of values, regardless of the value you have set for
  971. the fetch mode.
  972. This only returns the first column returned by the query.
  973. Any other
  974. columns returned by the query are discarded.
  975. If you need to return a column other
  976. than the first, see
  977. <xref linkend="zend.db.statement.fetching.fetchcolumn"/>
  978. .
  979. </para>
  980. <example id="zend.db.adapter.select.fetchcol.example">
  981. <title>Using fetchCol()</title>
  982. <programlisting language="php"><![CDATA[
  983. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  984. $result = $db->fetchCol(
  985. 'SELECT bug_description, bug_id FROM bugs WHERE bug_id = ?', 2);
  986. // contains bug_description; bug_id is not returned
  987. echo $result[0];
  988. ]]></programlisting>
  989. </example>
  990. </sect3>
  991. <sect3 id="zend.db.adapter.select.fetchpairs">
  992. <title>Fetching Key-Value Pairs from a Result Set</title>
  993. <para>
  994. The
  995. <methodname>fetchPairs()</methodname>
  996. method returns data in an array
  997. of key-value pairs, as an associative array with a
  998. single entry
  999. per row. The key of this associative array is taken from the
  1000. first column
  1001. returned by the SELECT query. The value is taken
  1002. from the second column returned by
  1003. the SELECT query. Any other
  1004. columns returned by the query are discarded.
  1005. </para>
  1006. <para>
  1007. You should design the SELECT query so that the first column
  1008. returned has unique
  1009. values. If there are duplicates values in
  1010. the first column, entries in the
  1011. associative array will be
  1012. overwritten.
  1013. </para>
  1014. <example id="zend.db.adapter.select.fetchpairs.example">
  1015. <title>Using fetchPairs()</title>
  1016. <programlisting language="php"><![CDATA[
  1017. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  1018. $result = $db->fetchPairs('SELECT bug_id, bug_status FROM bugs');
  1019. echo $result[2];
  1020. ]]></programlisting>
  1021. </example>
  1022. </sect3>
  1023. <sect3 id="zend.db.adapter.select.fetchrow">
  1024. <title>Fetching a Single Row from a Result Set</title>
  1025. <para>
  1026. The
  1027. <methodname>fetchRow()</methodname>
  1028. method returns data using the
  1029. current fetch mode, but it returns only the first row
  1030. fetched from the result set.
  1031. </para>
  1032. <example id="zend.db.adapter.select.fetchrow.example">
  1033. <title>Using fetchRow()</title>
  1034. <programlisting language="php"><![CDATA[
  1035. $db->setFetchMode(Zend_Db::FETCH_OBJ);
  1036. $result = $db->fetchRow('SELECT * FROM bugs WHERE bug_id = 2');
  1037. // note that $result is a single object, not an array of objects
  1038. echo $result->bug_description;
  1039. ]]></programlisting>
  1040. </example>
  1041. </sect3>
  1042. <sect3 id="zend.db.adapter.select.fetchone">
  1043. <title>Fetching a Single Scalar from a Result Set</title>
  1044. <para>
  1045. The
  1046. <methodname>fetchOne()</methodname>
  1047. method is like a combination
  1048. of
  1049. <methodname>fetchRow()</methodname>
  1050. with
  1051. <methodname>fetchCol()</methodname>
  1052. ,
  1053. in that it returns data only for the first row fetched from
  1054. the result set, and it
  1055. returns only the value of the first
  1056. column in that row. Therefore it returns only a
  1057. single
  1058. scalar value, not an array or an object.
  1059. </para>
  1060. <example id="zend.db.adapter.select.fetchone.example">
  1061. <title>Using fetchOne()</title>
  1062. <programlisting language="php"><![CDATA[
  1063. $result = $db->fetchOne('SELECT bug_status FROM bugs WHERE bug_id = 2');
  1064. // this is a single string value
  1065. echo $result;
  1066. ]]></programlisting>
  1067. </example>
  1068. </sect3>
  1069. </sect2>
  1070. <sect2 id="zend.db.adapter.write">
  1071. <title>Writing Changes to the Database</title>
  1072. <para>
  1073. You can use the Adapter class to write new data or change existing
  1074. data in your
  1075. database. This section describes methods to do these
  1076. operations.
  1077. </para>
  1078. <sect3 id="zend.db.adapter.write.insert">
  1079. <title>Inserting Data</title>
  1080. <para>
  1081. You can add new rows to a table in your database using the
  1082. <methodname>insert()</methodname>
  1083. method. The first argument is a string
  1084. that names the table, and the second argument
  1085. is an associative
  1086. array, mapping column names to data values.
  1087. </para>
  1088. <example id="zend.db.adapter.write.insert.example">
  1089. <title>Inserting in a Table</title>
  1090. <programlisting language="php"><![CDATA[
  1091. $data = array(
  1092. 'created_on' => '2007-03-22',
  1093. 'bug_description' => 'Something wrong',
  1094. 'bug_status' => 'NEW'
  1095. );
  1096. $db->insert('bugs', $data);
  1097. ]]></programlisting>
  1098. </example>
  1099. <para>
  1100. Columns you exclude from the array of data are not specified to
  1101. the database.
  1102. Therefore, they follow the same rules that an
  1103. <acronym>SQL</acronym>
  1104. INSERT statement follows: if the column has a DEFAULT
  1105. clause, the column takes that
  1106. value in the row created,
  1107. otherwise the column is left in a NULL state.
  1108. </para>
  1109. <para>
  1110. By default, the values in your data array are inserted using
  1111. parameters. This
  1112. reduces risk of some types of security
  1113. issues. You don't need to apply escaping or
  1114. quoting to values
  1115. in the data array.
  1116. </para>
  1117. <para>
  1118. You might need values in the data array to be treated as
  1119. <acronym>SQL</acronym>
  1120. expressions, in which case they should not be quoted. By
  1121. default, all data values
  1122. passed as strings are treated as
  1123. string literals. To specify that the value is an
  1124. <acronym>SQL</acronym>
  1125. expression and therefore should not be quoted, pass the value
  1126. in the data array as an
  1127. object of type
  1128. <classname>Zend_Db_Expr</classname>
  1129. instead
  1130. of a plain string.
  1131. </para>
  1132. <example id="zend.db.adapter.write.insert.example2">
  1133. <title>Inserting Expressions in a Table</title>
  1134. <programlisting language="php"><![CDATA[
  1135. $data = array(
  1136. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  1137. 'bug_description' => 'Something wrong',
  1138. 'bug_status' => 'NEW'
  1139. );
  1140. $db->insert('bugs', $data);
  1141. ]]></programlisting>
  1142. </example>
  1143. </sect3>
  1144. <sect3 id="zend.db.adapter.write.lastinsertid">
  1145. <title>Retrieving a Generated Value</title>
  1146. <para>
  1147. Some
  1148. <acronym>RDBMS</acronym>
  1149. brands support auto-incrementing primary keys.
  1150. A table defined this way generates a
  1151. primary key value
  1152. automatically during an INSERT of a new row. The return value
  1153. of the
  1154. <methodname>insert()</methodname>
  1155. method is
  1156. <emphasis>not</emphasis>
  1157. the last inserted ID, because the table might not have an
  1158. auto-incremented column.
  1159. Instead, the return value is the
  1160. number of rows affected (usually 1).
  1161. </para>
  1162. <para>
  1163. If your table is defined with an auto-incrementing primary key,
  1164. you can call the
  1165. <methodname>lastInsertId()</methodname>
  1166. method after the
  1167. insert. This method returns the last value generated in the
  1168. scope of
  1169. the current database connection.
  1170. </para>
  1171. <example id="zend.db.adapter.write.lastinsertid.example-1">
  1172. <title>Using lastInsertId() for an Auto-Increment Key</title>
  1173. <programlisting language="php"><![CDATA[
  1174. $db->insert('bugs', $data);
  1175. // return the last value generated by an auto-increment column
  1176. $id = $db->lastInsertId();
  1177. ]]></programlisting>
  1178. </example>
  1179. <para>
  1180. Some
  1181. <acronym>RDBMS</acronym>
  1182. brands support a sequence object, which generates
  1183. unique values to serve as primary
  1184. key values. To support
  1185. sequences, the
  1186. <methodname>lastInsertId()</methodname>
  1187. method accepts two
  1188. optional string arguments. These arguments name the table and
  1189. the
  1190. column, assuming you have followed the convention that a
  1191. sequence is named using the
  1192. table and column names for which
  1193. the sequence generates values, and a suffix "_seq".
  1194. This is
  1195. based on the convention used by PostgreSQL when naming
  1196. sequences for SERIAL
  1197. columns. For example, a table "bugs" with
  1198. primary key column "bug_id" would use a
  1199. sequence named
  1200. "bugs_bug_id_seq".
  1201. </para>
  1202. <example id="zend.db.adapter.write.lastinsertid.example-2">
  1203. <title>Using lastInsertId() for a Sequence</title>
  1204. <programlisting language="php"><![CDATA[
  1205. $db->insert('bugs', $data);
  1206. // return the last value generated by sequence 'bugs_bug_id_seq'.
  1207. $id = $db->lastInsertId('bugs', 'bug_id');
  1208. // alternatively, return the last value generated by sequence 'bugs_seq'.
  1209. $id = $db->lastInsertId('bugs');
  1210. ]]></programlisting>
  1211. </example>
  1212. <para>
  1213. If the name of your sequence object does not follow this naming
  1214. convention, use the
  1215. <methodname>lastSequenceId()</methodname>
  1216. method
  1217. instead. This method takes a single string argument, naming
  1218. the sequence
  1219. literally.
  1220. </para>
  1221. <example id="zend.db.adapter.write.lastinsertid.example-3">
  1222. <title>Using lastSequenceId()</title>
  1223. <programlisting language="php"><![CDATA[
  1224. $db->insert('bugs', $data);
  1225. // return the last value generated by sequence 'bugs_id_gen'.
  1226. $id = $db->lastSequenceId('bugs_id_gen');
  1227. ]]></programlisting>
  1228. </example>
  1229. <para>
  1230. For
  1231. <acronym>RDBMS</acronym>
  1232. brands that don't support sequences, including MySQL,
  1233. Microsoft
  1234. <acronym>SQL</acronym>
  1235. Server, and SQLite, the arguments to the
  1236. lastInsertId() method are ignored, and the
  1237. value returned is the
  1238. most recent value generated for any table by INSERT operations
  1239. during the current connection. For these
  1240. <acronym>RDBMS</acronym>
  1241. brands, the
  1242. lastSequenceId() method always returns
  1243. <constant>NULL</constant>
  1244. .
  1245. </para>
  1246. <note>
  1247. <title>Why Not Use "SELECT MAX(id) FROM table"?</title>
  1248. <para>
  1249. Sometimes this query returns the most recent primary key
  1250. value inserted into
  1251. the table. However, this technique
  1252. is not safe to use in an environment where
  1253. multiple clients are
  1254. inserting records to the database. It is possible, and
  1255. therefore is bound to happen eventually, that another
  1256. client inserts another row
  1257. in the instant between the
  1258. insert performed by your client application and your
  1259. query
  1260. for the MAX(id) value. Thus the value returned does not
  1261. identify the row you
  1262. inserted, it identifies the row
  1263. inserted by some other client. There is no way to
  1264. know
  1265. when this has happened.
  1266. </para>
  1267. <para>
  1268. Using a strong transaction isolation mode such as
  1269. "repeatable read" can mitigate
  1270. this risk, but some
  1271. <acronym>RDBMS</acronym>
  1272. brands don't support the transaction isolation required for
  1273. this, or else your
  1274. application may use a lower transaction
  1275. isolation mode by design.
  1276. </para>
  1277. <para>
  1278. Furthermore, using an expression like "MAX(id)+1" to generate
  1279. a new value for a
  1280. primary key is not safe, because two clients
  1281. could do this query simultaneously,
  1282. and then both use the same
  1283. calculated value for their next INSERT operation.
  1284. </para>
  1285. <para>
  1286. All
  1287. <acronym>RDBMS</acronym>
  1288. brands provide mechanisms to generate unique
  1289. values, and to return the last value
  1290. generated. These
  1291. mechanisms necessarily work outside of the scope of
  1292. transaction
  1293. isolation, so there is no chance of two clients
  1294. generating the same value, and
  1295. there is no chance that the
  1296. value generated by another client could be reported
  1297. to your
  1298. client's connection as the last value generated.
  1299. </para>
  1300. </note>
  1301. </sect3>
  1302. <sect3 id="zend.db.adapter.write.update">
  1303. <title>Updating Data</title>
  1304. <para>
  1305. You can update rows in a database table using the
  1306. <methodname>update()</methodname>
  1307. method of an Adapter. This method takes
  1308. three arguments: the first is the name of the
  1309. table; the
  1310. second is an associative array mapping columns to change to new
  1311. values to
  1312. assign to these columns.
  1313. </para>
  1314. <para>
  1315. The values in the data array are treated as string literals.
  1316. See
  1317. <xref linkend="zend.db.adapter.write.insert"/>
  1318. for information on using
  1319. <acronym>SQL</acronym>
  1320. expressions in the data array.
  1321. </para>
  1322. <para>
  1323. The third argument is a string containing an
  1324. <acronym>SQL</acronym>
  1325. expression
  1326. that is used as criteria for the rows to change. The values
  1327. and identifiers
  1328. in this argument are not quoted or escaped.
  1329. You are responsible for ensuring that any
  1330. dynamic content is
  1331. interpolated into this string safely.
  1332. See
  1333. <xref linkend="zend.db.adapter.quoting"/>
  1334. for methods to help you do this.
  1335. </para>
  1336. <para>
  1337. The return value is the number of rows affected by the update
  1338. operation.
  1339. </para>
  1340. <example id="zend.db.adapter.write.update.example">
  1341. <title>Updating Rows</title>
  1342. <programlisting language="php"><![CDATA[
  1343. $data = array(
  1344. 'updated_on' => '2007-03-23',
  1345. 'bug_status' => 'FIXED'
  1346. );
  1347. $n = $db->update('bugs', $data, 'bug_id = 2');
  1348. ]]></programlisting>
  1349. </example>
  1350. <para>
  1351. If you omit the third argument, then all rows in the database
  1352. table are updated
  1353. with the values specified in the data array.
  1354. </para>
  1355. <para>
  1356. If you provide an array of strings as the third argument, these
  1357. strings are joined
  1358. together as terms in an expression separated
  1359. by
  1360. <constant>AND</constant>
  1361. operators.
  1362. </para>
  1363. <example id="zend.db.adapter.write.update.example-array">
  1364. <title>Updating Rows Using an Array of Expressions</title>
  1365. <programlisting language="php"><![CDATA[
  1366. $data = array(
  1367. 'updated_on' => '2007-03-23',
  1368. 'bug_status' => 'FIXED'
  1369. );
  1370. $where[] = "reported_by = 'goofy'";
  1371. $where[] = "bug_status = 'OPEN'";
  1372. $n = $db->update('bugs', $data, $where);
  1373. // Resulting SQL is:
  1374. // UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED'
  1375. // WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN')
  1376. ]]></programlisting>
  1377. </example>
  1378. </sect3>
  1379. <sect3 id="zend.db.adapter.write.delete">
  1380. <title>Deleting Data</title>
  1381. <para>
  1382. You can delete rows from a database table using the
  1383. <methodname>delete()</methodname>
  1384. method. This method takes two arguments:
  1385. the first is a string naming the table.
  1386. </para>
  1387. <para>
  1388. The second argument is a string containing an
  1389. <acronym>SQL</acronym>
  1390. expression
  1391. that is used as criteria for the rows to delete. The values
  1392. and identifiers
  1393. in this argument are not quoted or escaped.
  1394. You are responsible for ensuring that any
  1395. dynamic content is
  1396. interpolated into this string safely.
  1397. See
  1398. <xref linkend="zend.db.adapter.quoting"/>
  1399. for methods to help you do this.
  1400. </para>
  1401. <para>
  1402. The return value is the number of rows affected by the delete
  1403. operation.
  1404. </para>
  1405. <example id="zend.db.adapter.write.delete.example">
  1406. <title>Deleting Rows</title>
  1407. <programlisting language="php"><![CDATA[
  1408. $n = $db->delete('bugs', 'bug_id = 3');
  1409. ]]></programlisting>
  1410. </example>
  1411. <para>
  1412. If you omit the second argument, the result is that all rows in
  1413. the database table
  1414. are deleted.
  1415. </para>
  1416. <para>
  1417. If you provide an array of strings as the second argument, these
  1418. strings are joined
  1419. together as terms in an expression separated
  1420. by
  1421. <constant>AND</constant>
  1422. operators.
  1423. </para>
  1424. </sect3>
  1425. </sect2>
  1426. <sect2 id="zend.db.adapter.quoting">
  1427. <title>Quoting Values and Identifiers</title>
  1428. <para>
  1429. When you form
  1430. <acronym>SQL</acronym>
  1431. queries, often it is the case that you need to
  1432. include the values of PHP variables in
  1433. <acronym>SQL</acronym>
  1434. expressions. This is
  1435. risky, because if the value in a PHP string contains certain
  1436. symbols, such as the quote symbol, it could result in invalid
  1437. <acronym>SQL</acronym>
  1438. .
  1439. For example, notice the imbalanced quote characters in the
  1440. following query:
  1441. <programlisting language="php"><![CDATA[
  1442. $name = "O'Reilly";
  1443. $sql = "SELECT * FROM bugs WHERE reported_by = '$name'";
  1444. echo $sql;
  1445. // SELECT * FROM bugs WHERE reported_by = 'O'Reilly'
  1446. ]]></programlisting>
  1447. </para>
  1448. <para>
  1449. Even worse is the risk that such code mistakes might be exploited
  1450. deliberately by a
  1451. person who is trying to manipulate the function
  1452. of your web application. If they can
  1453. specify the value of a
  1454. <acronym>PHP</acronym>
  1455. variable through the use of an
  1456. <acronym>HTTP</acronym>
  1457. parameter or other mechanism,
  1458. they might be able to make your
  1459. <acronym>SQL</acronym>
  1460. queries do things that you
  1461. didn't intend them to do, such as return data to which the
  1462. person
  1463. should not have privilege to read. This is a serious and widespread
  1464. technique for
  1465. violating application security, known as "SQL Injection" (see
  1466. <ulink url="http://en.wikipedia.org/wiki/SQL_Injection">http://en.wikipedia.org/wiki/SQL_Injection</ulink>
  1467. ).
  1468. </para>
  1469. <para>
  1470. The
  1471. <classname>Zend_Db</classname>
  1472. Adapter class provides convenient functions to help
  1473. you reduce vulnerabilities to
  1474. <acronym>SQL</acronym>
  1475. Injection attacks in your
  1476. <acronym>PHP</acronym>
  1477. code.
  1478. The solution is to escape special characters such as quotes in
  1479. <acronym>PHP</acronym>
  1480. values before they are interpolated into your
  1481. <acronym>SQL</acronym>
  1482. strings.
  1483. This protects against both accidental and deliberate manipulation
  1484. of
  1485. <acronym>SQL</acronym>
  1486. strings by
  1487. <acronym>PHP</acronym>
  1488. variables that contain special characters.
  1489. </para>
  1490. <sect3 id="zend.db.adapter.quoting.quote">
  1491. <title>Using quote()</title>
  1492. <para>
  1493. The
  1494. <methodname>quote()</methodname>
  1495. method accepts a single argument, a
  1496. scalar string value. It returns the value with
  1497. special
  1498. characters escaped in a manner appropriate for the
  1499. <acronym>RDBMS</acronym>
  1500. you
  1501. are using, and surrounded by string value delimiters. The
  1502. standard
  1503. <acronym>SQL</acronym>
  1504. string value delimiter is the single-quote
  1505. (
  1506. <code>'</code>
  1507. ).
  1508. </para>
  1509. <example id="zend.db.adapter.quoting.quote.example">
  1510. <title>Using quote()</title>
  1511. <programlisting language="php"><![CDATA[
  1512. $name = $db->quote("O'Reilly");
  1513. echo $name;
  1514. // 'O\'Reilly'
  1515. $sql = "SELECT * FROM bugs WHERE reported_by = $name";
  1516. echo $sql;
  1517. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1518. ]]></programlisting>
  1519. </example>
  1520. <para>
  1521. Note that the return value of
  1522. <methodname>quote()</methodname>
  1523. includes the
  1524. quote delimiters around the string. This is different from
  1525. some functions
  1526. that escape special characters but do not add
  1527. the quote delimiters, for example
  1528. <ulink url="http://www.php.net/mysqli_real_escape_string">mysql_real_escape_string()</ulink>
  1529. .
  1530. </para>
  1531. <para>
  1532. Values may need to be quoted or not quoted according to the
  1533. <acronym>SQL</acronym>
  1534. datatype context in which they are used. For instance, in some
  1535. RDBMS brands, an
  1536. integer value must not be quoted as a string
  1537. if it is compared to an integer-type
  1538. column or expression.
  1539. In other words, the following is an error in some
  1540. <acronym>SQL</acronym>
  1541. implementations, assuming
  1542. <code>intColumn</code>
  1543. has a
  1544. <acronym>SQL</acronym>
  1545. datatype of
  1546. <constant>INTEGER</constant>
  1547. <programlisting language="php"><![CDATA[
  1548. SELECT * FROM atable WHERE intColumn = '123'
  1549. ]]></programlisting>
  1550. </para>
  1551. <para>
  1552. You can use the optional second argument to the
  1553. <methodname>quote()</methodname>
  1554. method to apply quoting selectively for
  1555. the
  1556. <acronym>SQL</acronym>
  1557. datatype you specify.
  1558. </para>
  1559. <example id="zend.db.adapter.quoting.quote.example-2">
  1560. <title>Using quote() with a SQL Type</title>
  1561. <programlisting language="php"><![CDATA[
  1562. $value = '1234';
  1563. $sql = 'SELECT * FROM atable WHERE intColumn = '
  1564. . $db->quote($value, 'INTEGER');
  1565. ]]></programlisting>
  1566. </example>
  1567. <para>
  1568. Each
  1569. <classname>Zend_Db_Adapter</classname>
  1570. class has encoded the names of numeric
  1571. <acronym>SQL</acronym>
  1572. datatypes for the respective brand of
  1573. <acronym>RDBMS</acronym>
  1574. . You can also
  1575. use the constants
  1576. <constant>Zend_Db::INT_TYPE</constant>
  1577. ,
  1578. <constant>Zend_Db::BIGINT_TYPE</constant>
  1579. , and
  1580. <constant>Zend_Db::FLOAT_TYPE</constant>
  1581. to write code in a more
  1582. <acronym>RDBMS</acronym>
  1583. -independent way.
  1584. </para>
  1585. <para>
  1586. <classname>Zend_Db_Table</classname>
  1587. specifies
  1588. <acronym>SQL</acronym>
  1589. types to
  1590. <methodname>quote()</methodname>
  1591. automatically when generating
  1592. <acronym>SQL</acronym>
  1593. queries that
  1594. reference a table's key columns.
  1595. </para>
  1596. </sect3>
  1597. <sect3 id="zend.db.adapter.quoting.quote-into">
  1598. <title>Using quoteInto()</title>
  1599. <para>
  1600. The most typical usage of quoting is to interpolate a
  1601. <acronym>PHP</acronym>
  1602. variable into a
  1603. <acronym>SQL</acronym>
  1604. expression or statement. You can use the
  1605. <methodname>quoteInto()</methodname>
  1606. method to do this in one step. This
  1607. method takes two arguments: the first argument is
  1608. a string
  1609. containing a placeholder symbol (
  1610. <code>?</code>
  1611. ), and the
  1612. second argument is a value or
  1613. <acronym>PHP</acronym>
  1614. variable that should be
  1615. substituted for that placeholder.
  1616. </para>
  1617. <para>
  1618. The placeholder symbol is the same symbol used by many
  1619. <acronym>RDBMS</acronym>
  1620. brands for positional parameters, but the
  1621. <methodname>quoteInto()</methodname>
  1622. method only emulates query parameters.
  1623. The method simply interpolates the value into
  1624. the string,
  1625. escapes special characters, and applies quotes around it.
  1626. True query
  1627. parameters maintain the separation between the
  1628. <acronym>SQL</acronym>
  1629. string and the parameters as the statement is parsed in the
  1630. <acronym>RDBMS</acronym>
  1631. server.
  1632. </para>
  1633. <example id="zend.db.adapter.quoting.quote-into.example">
  1634. <title>Using quoteInto()</title>
  1635. <programlisting language="php"><![CDATA[
  1636. $sql = $db->quoteInto("SELECT * FROM bugs WHERE reported_by = ?", "O'Reilly");
  1637. echo $sql;
  1638. // SELECT * FROM bugs WHERE reported_by = 'O\'Reilly'
  1639. ]]></programlisting>
  1640. </example>
  1641. <para>
  1642. You can use the optional third parameter of
  1643. <methodname>quoteInto()</methodname>
  1644. to specify the
  1645. <acronym>SQL</acronym>
  1646. datatype. Numeric
  1647. datatypes are not quoted, and other types are quoted.
  1648. </para>
  1649. <example id="zend.db.adapter.quoting.quote-into.example-2">
  1650. <title>Using quoteInto() with a SQL Type</title>
  1651. <programlisting language="php"><![CDATA[
  1652. $sql = $db
  1653. ->quoteInto("SELECT * FROM bugs WHERE bug_id = ?", '1234', 'INTEGER');
  1654. echo $sql;
  1655. // SELECT * FROM bugs WHERE reported_by = 1234
  1656. ]]></programlisting>
  1657. </example>
  1658. </sect3>
  1659. <sect3 id="zend.db.adapter.quoting.quote-identifier">
  1660. <title>Using quoteIdentifier()</title>
  1661. <para>
  1662. Values are not the only part of
  1663. <acronym>SQL</acronym>
  1664. syntax that might need to
  1665. be variable. If you use
  1666. <acronym>PHP</acronym>
  1667. variables to name tables, columns,
  1668. or other identifiers in your
  1669. <acronym>SQL</acronym>
  1670. statements, you might need to
  1671. quote these strings too. By default,
  1672. <acronym>SQL</acronym>
  1673. identifiers have
  1674. syntax rules like
  1675. <acronym>PHP</acronym>
  1676. and most other programming languages.
  1677. For example, identifiers should not contain
  1678. spaces, certain
  1679. punctuation or special characters, or international characters.
  1680. Also
  1681. certain words are reserved for
  1682. <acronym>SQL</acronym>
  1683. syntax, and should not
  1684. be used as identifiers.
  1685. </para>
  1686. <para>
  1687. However,
  1688. <acronym>SQL</acronym>
  1689. has a feature called
  1690. <emphasis>delimited identifiers</emphasis>
  1691. ,
  1692. which allows broader choices for the spelling of identifiers.
  1693. If you enclose a
  1694. <acronym>SQL</acronym>
  1695. identifier in the proper types of quotes,
  1696. you can use identifiers with spellings that
  1697. would be invalid
  1698. without the quotes. Delimited identifiers can contain spaces,
  1699. punctuation, or international characters. You can also use
  1700. <acronym>SQL</acronym>
  1701. reserved words if you enclose them in identifier delimiters.
  1702. </para>
  1703. <para>
  1704. The
  1705. <methodname>quoteIdentifier()</methodname>
  1706. method works like
  1707. <methodname>quote()</methodname>
  1708. , but it applies the identifier delimiter
  1709. characters to the string according to the
  1710. type of Adapter you
  1711. use. For example, standard
  1712. <acronym>SQL</acronym>
  1713. uses double-quotes
  1714. (
  1715. <code>"</code>
  1716. ) for identifier delimiters, and most
  1717. <acronym>RDBMS</acronym>
  1718. brands use that symbol. MySQL uses back-quotes
  1719. (
  1720. <code>`</code>
  1721. ) by default. The
  1722. <methodname>quoteIdentifier()</methodname>
  1723. method also escapes special
  1724. characters within the string argument.
  1725. </para>
  1726. <example id="zend.db.adapter.quoting.quote-identifier.example">
  1727. <title>Using quoteIdentifier()</title>
  1728. <programlisting language="php"><![CDATA[
  1729. // we might have a table name that is an SQL reserved word
  1730. $tableName = $db->quoteIdentifier("order");
  1731. $sql = "SELECT * FROM $tableName";
  1732. echo $sql
  1733. // SELECT * FROM "order"
  1734. ]]></programlisting>
  1735. </example>
  1736. <para>
  1737. <acronym>SQL</acronym>
  1738. delimited identifiers are case-sensitive, unlike unquoted
  1739. identifiers. Therefore, if
  1740. you use delimited identifiers, you
  1741. must use the spelling of the identifier exactly as
  1742. it is stored
  1743. in your schema, including the case of the letters.
  1744. </para>
  1745. <para>
  1746. In most cases where
  1747. <acronym>SQL</acronym>
  1748. is generated within
  1749. <classname>Zend_Db</classname>
  1750. classes,
  1751. the default is that all identifiers are delimited
  1752. automatically. You can
  1753. change this behavior with the option
  1754. <constant>Zend_Db::AUTO_QUOTE_IDENTIFIERS</constant>
  1755. . Specify this
  1756. when instantiating the Adapter.
  1757. See
  1758. <xref linkend="zend.db.adapter.connecting.parameters.example2"/>
  1759. .
  1760. </para>
  1761. </sect3>
  1762. </sect2>
  1763. <sect2 id="zend.db.adapter.transactions">
  1764. <title>Controlling Database Transactions</title>
  1765. <para>
  1766. Databases define transactions as logical units of work that can be
  1767. committed or rolled
  1768. back as a single change, even if they operate
  1769. on multiple tables. All queries to a
  1770. database are executed within
  1771. the context of a transaction, even if the database driver
  1772. manages
  1773. them implicitly. This is called
  1774. <emphasis>auto-commit</emphasis>
  1775. mode, in which the database driver creates a transaction for every
  1776. statement you execute,
  1777. and commits that transaction after your
  1778. <acronym>SQL</acronym>
  1779. statement has been executed. By default, all
  1780. <classname>Zend_Db</classname>
  1781. Adapter
  1782. classes operate in auto-commit mode.
  1783. </para>
  1784. <para>
  1785. Alternatively, you can specify the beginning and resolution of a
  1786. transaction, and thus
  1787. control how many
  1788. <acronym>SQL</acronym>
  1789. queries are included in
  1790. a single group that is committed (or rolled back) as a single
  1791. operation. Use the
  1792. <methodname>beginTransaction()</methodname>
  1793. method to
  1794. initiate a transaction. Subsequent
  1795. <acronym>SQL</acronym>
  1796. statements are executed in
  1797. the context of the same transaction until you resolve it
  1798. explicitly.
  1799. </para>
  1800. <para>
  1801. To resolve the transaction, use either the
  1802. <methodname>commit()</methodname>
  1803. or
  1804. <methodname>rollBack()</methodname>
  1805. methods. The
  1806. <methodname>commit()</methodname>
  1807. method marks changes made during your transaction as committed, which
  1808. means the effects
  1809. of these changes are shown in queries run in
  1810. other transactions.
  1811. </para>
  1812. <para>
  1813. The
  1814. <methodname>rollBack()</methodname>
  1815. method does the opposite: it discards
  1816. the changes made during your transaction. The
  1817. changes are
  1818. effectively undone, and the state of the data returns to how it was
  1819. before you
  1820. began your transaction. However, rolling back your
  1821. transaction has no effect on changes
  1822. made by other transactions
  1823. running concurrently.
  1824. </para>
  1825. <para>
  1826. After you resolve this transaction,
  1827. <classname>Zend_Db_Adapter</classname>
  1828. returns to auto-commit mode until you call
  1829. <methodname>beginTransaction()</methodname>
  1830. again.
  1831. </para>
  1832. <example id="zend.db.adapter.transactions.example">
  1833. <title>Managing a Transaction to Ensure Consistency</title>
  1834. <programlisting language="php"><![CDATA[
  1835. // Start a transaction explicitly.
  1836. $db->beginTransaction();
  1837. try {
  1838. // Attempt to execute one or more queries:
  1839. $db->query(...);
  1840. $db->query(...);
  1841. $db->query(...);
  1842. // If all succeed, commit the transaction and all changes
  1843. // are committed at once.
  1844. $db->commit();
  1845. } catch (Exception $e) {
  1846. // If any of the queries failed and threw an exception,
  1847. // we want to roll back the whole transaction, reversing
  1848. // changes made in the transaction, even those that succeeded.
  1849. // Thus all changes are committed together, or none are.
  1850. $db->rollBack();
  1851. echo $e->getMessage();
  1852. }
  1853. ]]></programlisting>
  1854. </example>
  1855. </sect2>
  1856. <sect2 id="zend.db.adapter.list-describe">
  1857. <title>Listing and Describing Tables</title>
  1858. <para>
  1859. The
  1860. <methodname>listTables()</methodname>
  1861. method returns an array of strings,
  1862. naming all tables in the current database.
  1863. </para>
  1864. <para>
  1865. The
  1866. <methodname>describeTable()</methodname>
  1867. method returns an associative
  1868. array of metadata about a table. Specify the name of the
  1869. table
  1870. as a string in the first argument to this method. The second
  1871. argument is optional,
  1872. and names the schema in which the table
  1873. exists.
  1874. </para>
  1875. <para>
  1876. The keys of the associative array returned are the column names of
  1877. the table. The value
  1878. corresponding to each column is also an
  1879. associative array, with the following keys and
  1880. values:
  1881. </para>
  1882. <table frame="all" cellpadding="5" id="zend.db.adapter.list-describe.metadata">
  1883. <title>Metadata Fields Returned by describeTable()</title>
  1884. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  1885. <thead>
  1886. <row>
  1887. <entry>Key</entry>
  1888. <entry>Type</entry>
  1889. <entry>Description</entry>
  1890. </row>
  1891. </thead>
  1892. <tbody>
  1893. <row>
  1894. <entry>
  1895. <constant>SCHEMA_NAME</constant>
  1896. </entry>
  1897. <entry>(string)</entry>
  1898. <entry>Name of the database schema in which this table exists.</entry>
  1899. </row>
  1900. <row>
  1901. <entry>
  1902. <constant>TABLE_NAME</constant>
  1903. </entry>
  1904. <entry>(string)</entry>
  1905. <entry>Name of the table to which this column belongs.</entry>
  1906. </row>
  1907. <row>
  1908. <entry>
  1909. <constant>COLUMN_NAME</constant>
  1910. </entry>
  1911. <entry>(string)</entry>
  1912. <entry>Name of the column.</entry>
  1913. </row>
  1914. <row>
  1915. <entry>
  1916. <constant>COLUMN_POSITION</constant>
  1917. </entry>
  1918. <entry>(integer)</entry>
  1919. <entry>Ordinal position of the column in the table.</entry>
  1920. </row>
  1921. <row>
  1922. <entry>
  1923. <constant>DATA_TYPE</constant>
  1924. </entry>
  1925. <entry>(string)</entry>
  1926. <entry>RDBMS name of the datatype of the column.</entry>
  1927. </row>
  1928. <row>
  1929. <entry>
  1930. <constant>DEFAULT</constant>
  1931. </entry>
  1932. <entry>(string)</entry>
  1933. <entry>Default value for the column, if any.</entry>
  1934. </row>
  1935. <row>
  1936. <entry>
  1937. <constant>NULLABLE</constant>
  1938. </entry>
  1939. <entry>(boolean)</entry>
  1940. <entry>
  1941. True if the column accepts
  1942. <acronym>SQL</acronym>
  1943. NULLs, false if the column has a NOT NULL
  1944. constraint.
  1945. </entry>
  1946. </row>
  1947. <row>
  1948. <entry>
  1949. <constant>LENGTH</constant>
  1950. </entry>
  1951. <entry>(integer)</entry>
  1952. <entry>
  1953. Length or size of the column as reported by the
  1954. <acronym>RDBMS</acronym>
  1955. .
  1956. </entry>
  1957. </row>
  1958. <row>
  1959. <entry>
  1960. <constant>SCALE</constant>
  1961. </entry>
  1962. <entry>(integer)</entry>
  1963. <entry>
  1964. Scale of
  1965. <acronym>SQL</acronym>
  1966. NUMERIC or
  1967. <constant>DECIMAL</constant>
  1968. type.
  1969. </entry>
  1970. </row>
  1971. <row>
  1972. <entry>
  1973. <constant>PRECISION</constant>
  1974. </entry>
  1975. <entry>(integer)</entry>
  1976. <entry>
  1977. Precision of
  1978. <acronym>SQL</acronym>
  1979. NUMERIC or
  1980. <constant>DECIMAL</constant>
  1981. type.
  1982. </entry>
  1983. </row>
  1984. <row>
  1985. <entry>
  1986. <constant>UNSIGNED</constant>
  1987. </entry>
  1988. <entry>(boolean)</entry>
  1989. <entry>
  1990. True if an integer-based type is reported as
  1991. <constant>UNSIGNED</constant>
  1992. .
  1993. </entry>
  1994. </row>
  1995. <row>
  1996. <entry>
  1997. <constant>PRIMARY</constant>
  1998. </entry>
  1999. <entry>(boolean)</entry>
  2000. <entry>True if the column is part of the primary key of this table.</entry>
  2001. </row>
  2002. <row>
  2003. <entry>
  2004. <constant>PRIMARY_POSITION</constant>
  2005. </entry>
  2006. <entry>(integer)</entry>
  2007. <entry>Ordinal position (1-based) of the column in the primary key.</entry>
  2008. </row>
  2009. <row>
  2010. <entry>
  2011. <constant>IDENTITY</constant>
  2012. </entry>
  2013. <entry>(boolean)</entry>
  2014. <entry>True if the column uses an auto-generated value.</entry>
  2015. </row>
  2016. </tbody>
  2017. </tgroup>
  2018. </table>
  2019. <note>
  2020. <title>How the IDENTITY Metadata Field Relates to Specific RDBMSs</title>
  2021. <para>
  2022. The IDENTITY metadata field was chosen as an 'idiomatic' term to
  2023. represent a
  2024. relation to surrogate keys. This field can be
  2025. commonly known by the following
  2026. values:-
  2027. </para>
  2028. <itemizedlist>
  2029. <listitem>
  2030. <para>
  2031. <constant>IDENTITY</constant>
  2032. - DB2, MSSQL
  2033. </para>
  2034. </listitem>
  2035. <listitem>
  2036. <para>
  2037. <constant>AUTO_INCREMENT</constant>
  2038. - MySQL
  2039. </para>
  2040. </listitem>
  2041. <listitem>
  2042. <para>
  2043. <constant>SERIAL</constant>
  2044. - PostgreSQL
  2045. </para>
  2046. </listitem>
  2047. <listitem>
  2048. <para>
  2049. <constant>SEQUENCE</constant>
  2050. - Oracle
  2051. </para>
  2052. </listitem>
  2053. </itemizedlist>
  2054. </note>
  2055. <para>
  2056. If no table exists matching the table name and optional schema name
  2057. specified, then
  2058. <methodname>describeTable()</methodname>
  2059. returns an empty array.
  2060. </para>
  2061. </sect2>
  2062. <sect2 id="zend.db.adapter.closing">
  2063. <title>Closing a Connection</title>
  2064. <para>
  2065. Normally it is not necessary to close a database connection.
  2066. <acronym>PHP</acronym>
  2067. automatically cleans up all resources and the end of a request.
  2068. Database extensions are
  2069. designed to close the connection as the
  2070. reference to the resource object is cleaned up.
  2071. </para>
  2072. <para>
  2073. However, if you have a long-duration
  2074. <acronym>PHP</acronym>
  2075. script that initiates many
  2076. database connections, you might need to close the connection,
  2077. to avoid
  2078. exhausting the capacity of your
  2079. <acronym>RDBMS</acronym>
  2080. server. You can use the
  2081. Adapter's
  2082. <methodname>closeConnection()</methodname>
  2083. method to explicitly close
  2084. the underlying database connection.
  2085. </para>
  2086. <para>
  2087. Since release 1.7.2, you could check you are currently connected to the
  2088. <acronym>RDBMS</acronym>
  2089. server with the method
  2090. <methodname>isConnected()</methodname>
  2091. . This means that a
  2092. connection resource has been initiated and wasn't closed. This
  2093. function is not
  2094. currently able to test for example a server side closing of the
  2095. connection. This is
  2096. internally use to close the connection. It allow you to close the
  2097. connection
  2098. multiple times without errors. It was already the case before 1.7.2 for
  2099. <acronym>PDO</acronym>
  2100. adapters but not for the others.
  2101. </para>
  2102. <example id="zend.db.adapter.closing.example">
  2103. <title>Closing a Database Connection</title>
  2104. <programlisting language="php"><![CDATA[
  2105. $db->closeConnection();
  2106. ]]></programlisting>
  2107. </example>
  2108. <note>
  2109. <title>Does Zend_Db Support Persistent Connections?</title>
  2110. <para>
  2111. Yes, persistence is supported through the addition of
  2112. the
  2113. <code>persistent</code>
  2114. flag set to true in the
  2115. configuration (not driver_configuration) of an adapter
  2116. in
  2117. <classname>Zend_Db</classname>
  2118. .
  2119. </para>
  2120. <example id="zend.db.adapter.connecting.persistence.example">
  2121. <title>Using the Persitence Flag with the Oracle Adapter</title>
  2122. <programlisting language="php"><![CDATA[
  2123. $db = Zend_Db::factory('Oracle', array(
  2124. 'host' => '127.0.0.1',
  2125. 'username' => 'webuser',
  2126. 'password' => 'xxxxxxxx',
  2127. 'dbname' => 'test',
  2128. 'persistent' => true
  2129. ));
  2130. ]]></programlisting>
  2131. </example>
  2132. <para>
  2133. Please note that using persistent connections can cause an
  2134. excess of idle connections
  2135. on the
  2136. <acronym>RDBMS</acronym>
  2137. server, which causes
  2138. more problems than any performance gain you might achieve by
  2139. reducing the overhead of making connections.
  2140. </para>
  2141. <para>
  2142. Database connections have state. That is, some objects in the
  2143. <acronym>RDBMS</acronym>
  2144. server exist in session scope. Examples are locks, user
  2145. variables, temporary tables,
  2146. and information about the most
  2147. recently executed query, such as rows affected, and
  2148. last
  2149. generated id value. If you use persistent connections, your
  2150. application could
  2151. access invalid or privileged data that were
  2152. created in a previous
  2153. <acronym>PHP</acronym>
  2154. request.
  2155. </para>
  2156. <para>
  2157. Currently, only Oracle, DB2, and the
  2158. <acronym>PDO</acronym>
  2159. adapters (where
  2160. specified by
  2161. <acronym>PHP</acronym>
  2162. ) support persistence in
  2163. <classname>Zend_Db</classname>
  2164. .
  2165. </para>
  2166. </note>
  2167. </sect2>
  2168. <sect2 id="zend.db.adapter.other-statements">
  2169. <title>Running Other Database Statements</title>
  2170. <para>
  2171. There might be cases in which you need to access the connection
  2172. object directly, as
  2173. provided by the
  2174. <acronym>PHP</acronym>
  2175. database extension. Some
  2176. of these extensions may offer features that are not surfaced by
  2177. methods of
  2178. <classname>Zend_Db_Adapter_Abstract</classname>
  2179. .
  2180. </para>
  2181. <para>
  2182. For example, all
  2183. <acronym>SQL</acronym>
  2184. statements run by
  2185. <classname>Zend_Db</classname>
  2186. are prepared, then
  2187. executed. However, some database features are incompatible with
  2188. prepared statements. DDL statements like CREATE and ALTER cannot
  2189. be prepared in MySQL.
  2190. Also,
  2191. <acronym>SQL</acronym>
  2192. statements don't benefit from the
  2193. <ulink url="http://dev.mysql.com/doc/refman/5.1/en/query-cache-how.html">MySQL Query
  2194. Cache</ulink>
  2195. , prior to MySQL 5.1.17.
  2196. </para>
  2197. <para>
  2198. Most
  2199. <acronym>PHP</acronym>
  2200. database extensions provide a method to execute
  2201. <acronym>SQL</acronym>
  2202. statements without preparing them. For example, in
  2203. <acronym>PDO</acronym>
  2204. , this
  2205. method is
  2206. <methodname>exec()</methodname>
  2207. . You can access the connection
  2208. object in the
  2209. <acronym>PHP</acronym>
  2210. extension directly using getConnection().
  2211. </para>
  2212. <example id="zend.db.adapter.other-statements.example">
  2213. <title>Running a Non-Prepared Statement in a PDO Adapter</title>
  2214. <programlisting language="php"><![CDATA[
  2215. $result = $db->getConnection()->exec('DROP TABLE bugs');
  2216. ]]></programlisting>
  2217. </example>
  2218. <para>
  2219. Similarly, you can access other methods or properties that are
  2220. specific to
  2221. <acronym>PHP</acronym>
  2222. database extensions. Be aware, though, that by
  2223. doing this you might constrain your
  2224. application to the interface
  2225. provided by the extension for a specific brand of
  2226. <acronym>RDBMS</acronym>
  2227. .
  2228. </para>
  2229. <para>
  2230. In future versions of
  2231. <classname>Zend_Db</classname>
  2232. , there will be opportunities to
  2233. add method entry points for functionality that is common
  2234. to
  2235. the supported
  2236. <acronym>PHP</acronym>
  2237. database extensions. This will not affect
  2238. backward compatibility.
  2239. </para>
  2240. </sect2>
  2241. <sect2 id="zend.db.adapter.server-version">
  2242. <title>Retrieving Server Version</title>
  2243. <para>
  2244. Since release 1.7.2, you could retrieve the server version in
  2245. <acronym>PHP</acronym>
  2246. syntax
  2247. style to be able to use
  2248. <methodname>version_compare()</methodname>
  2249. . If the information
  2250. isn't available, you will receive
  2251. <constant>NULL</constant>
  2252. .
  2253. </para>
  2254. <example id="zend.db.adapter.server-version.example">
  2255. <title>Verifying server version before running a query</title>
  2256. <programlisting language="php"><![CDATA[
  2257. $version = $db->getServerVersion();
  2258. if (!is_null($version)) {
  2259. if (version_compare($version, '5.0.0', '>=')) {
  2260. // do something
  2261. } else {
  2262. // do something else
  2263. }
  2264. } else {
  2265. // impossible to read server version
  2266. }
  2267. ]]></programlisting>
  2268. </example>
  2269. </sect2>
  2270. <sect2 id="zend.db.adapter.adapter-notes">
  2271. <title>Notes on Specific Adapters</title>
  2272. <para>
  2273. This section lists differences between the Adapter classes of which
  2274. you should be
  2275. aware.
  2276. </para>
  2277. <sect3 id="zend.db.adapter.adapter-notes.ibm-db2">
  2278. <title>IBM DB2</title>
  2279. <itemizedlist>
  2280. <listitem>
  2281. <para>
  2282. Specify this Adapter to the factory() method with the
  2283. name 'Db2'.
  2284. </para>
  2285. </listitem>
  2286. <listitem>
  2287. <para>
  2288. This Adapter uses the
  2289. <acronym>PHP</acronym>
  2290. extension ibm_db2.
  2291. </para>
  2292. </listitem>
  2293. <listitem>
  2294. <para>
  2295. IBM DB2 supports both sequences and auto-incrementing
  2296. keys. Therefore the
  2297. arguments to
  2298. <methodname>lastInsertId()</methodname>
  2299. are optional. If you give
  2300. no arguments, the Adapter returns the last value
  2301. generated for an auto-increment key. If you give
  2302. arguments, the Adapter
  2303. returns the last value generated
  2304. by the sequence named according to the
  2305. convention
  2306. '
  2307. <emphasis>table</emphasis>
  2308. _
  2309. <emphasis>column</emphasis>
  2310. _seq'.
  2311. </para>
  2312. </listitem>
  2313. </itemizedlist>
  2314. </sect3>
  2315. <sect3 id="zend.db.adapter.adapter-notes.mysqli">
  2316. <title>MySQLi</title>
  2317. <itemizedlist>
  2318. <listitem>
  2319. <para>
  2320. Specify this Adapter to the
  2321. <methodname>factory()</methodname>
  2322. method with the name 'Mysqli'.
  2323. </para>
  2324. </listitem>
  2325. <listitem>
  2326. <para>
  2327. This Adapter utilizes the
  2328. <acronym>PHP</acronym>
  2329. extension mysqli.
  2330. </para>
  2331. </listitem>
  2332. <listitem>
  2333. <para>
  2334. MySQL does not support sequences, so
  2335. <methodname>lastInsertId()</methodname>
  2336. ignores its arguments and
  2337. always returns the last value generated for an
  2338. auto-increment key. The
  2339. <methodname>lastSequenceId()</methodname>
  2340. method returns
  2341. <constant>NULL</constant>
  2342. .
  2343. </para>
  2344. </listitem>
  2345. </itemizedlist>
  2346. </sect3>
  2347. <sect3 id="zend.db.adapter.adapter-notes.oracle">
  2348. <title>Oracle</title>
  2349. <itemizedlist>
  2350. <listitem>
  2351. <para>
  2352. Specify this Adapter to the
  2353. <methodname>factory()</methodname>
  2354. method with the name 'Oracle'.
  2355. </para>
  2356. </listitem>
  2357. <listitem>
  2358. <para>
  2359. This Adapter uses the
  2360. <acronym>PHP</acronym>
  2361. extension oci8.
  2362. </para>
  2363. </listitem>
  2364. <listitem>
  2365. <para>
  2366. Oracle does not support auto-incrementing keys, so you
  2367. should specify the
  2368. name of a sequence to
  2369. <methodname>lastInsertId()</methodname>
  2370. or
  2371. <methodname>lastSequenceId()</methodname>
  2372. .
  2373. </para>
  2374. </listitem>
  2375. <listitem>
  2376. <para>
  2377. The Oracle extension does not support positional
  2378. parameters. You must use
  2379. named parameters.
  2380. </para>
  2381. </listitem>
  2382. <listitem>
  2383. <para>
  2384. Currently the
  2385. <constant>Zend_Db::CASE_FOLDING</constant>
  2386. option
  2387. is not supported by the Oracle adapter. To use this
  2388. option with Oracle,
  2389. you must use the
  2390. <acronym>PDO</acronym>
  2391. OCI adapter.
  2392. </para>
  2393. </listitem>
  2394. <listitem>
  2395. <para>
  2396. By default, LOB fields are returned as OCI-Lob objects. You could
  2397. retrieve
  2398. them as string for all requests by using driver options
  2399. <code>'lob_as_string'</code>
  2400. or for particular request by using
  2401. <methodname>setLobAsString(boolean)</methodname>
  2402. on adapter or on
  2403. statement.
  2404. </para>
  2405. </listitem>
  2406. </itemizedlist>
  2407. </sect3>
  2408. <sect3 id="zend.db.adapter.adapter-notes.sqlsrv">
  2409. <title>Microsoft SQL Server</title>
  2410. <itemizedlist>
  2411. <listitem>
  2412. <para>
  2413. Specify this Adapter to the
  2414. <methodname>factory()</methodname>
  2415. method with
  2416. the name 'Sqlsrv'.
  2417. </para>
  2418. </listitem>
  2419. <listitem>
  2420. <para>
  2421. This Adapter uses the
  2422. <acronym>PHP</acronym>
  2423. extension sqlsrv
  2424. </para>
  2425. </listitem>
  2426. <listitem>
  2427. <para>
  2428. Microsoft
  2429. <acronym>SQL</acronym>
  2430. Server does not support sequences, so
  2431. <methodname>lastInsertId()</methodname>
  2432. ignores primary key argument and
  2433. returns the last value generated for an
  2434. auto-increment key if a table name
  2435. is specified or a last insert query
  2436. returned id. The
  2437. <methodname>lastSequenceId()</methodname>
  2438. method returns
  2439. <constant>NULL</constant>
  2440. .
  2441. </para>
  2442. </listitem>
  2443. <listitem>
  2444. <para>
  2445. <classname>Zend_Db_Adapter_Sqlsrv</classname>
  2446. sets
  2447. <constant>QUOTED_IDENTIFIER ON</constant>
  2448. immediately after connecting to
  2449. a
  2450. <acronym>SQL</acronym>
  2451. Server database. This makes the driver use the
  2452. standard
  2453. <acronym>SQL</acronym>
  2454. identifier delimiter symbol
  2455. (
  2456. <emphasis>"</emphasis>
  2457. ) instead of the proprietary square-brackets
  2458. syntax
  2459. <acronym>SQL</acronym>
  2460. Server uses for delimiting identifiers.
  2461. </para>
  2462. </listitem>
  2463. <listitem>
  2464. <para>
  2465. You can specify
  2466. <property>driver_options</property>
  2467. as a key in the options
  2468. array. The value can be a anything from here
  2469. <ulink url="http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx">http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx</ulink>
  2470. .
  2471. </para>
  2472. </listitem>
  2473. <listitem>
  2474. <para>
  2475. You can use
  2476. <methodname>setTransactionIsolationLevel()</methodname>
  2477. to set
  2478. isolation level for current connection. The value can be
  2479. <constant>SQLSRV_TXN_READ_UNCOMMITTED</constant>
  2480. ,
  2481. <constant>SQLSRV_TXN_READ_COMMITTED</constant>
  2482. ,
  2483. <constant>SQLSRV_TXN_REPEATABLE_READ</constant>
  2484. ,
  2485. <constant>SQLSRV_TXN_SNAPSHOT</constant>
  2486. or
  2487. <constant>SQLSRV_TXN_SERIALIZABLE</constant>
  2488. .
  2489. </para>
  2490. </listitem>
  2491. <listitem>
  2492. <para>
  2493. As of Zend Framework 1.9, the minimal supported build of the
  2494. <acronym>PHP</acronym>
  2495. <acronym>SQL</acronym>
  2496. Server extension from
  2497. Microsoft is 1.0.1924.0. and the
  2498. <acronym>MSSQL</acronym>
  2499. Server Native
  2500. Client version 9.00.3042.00.
  2501. </para>
  2502. </listitem>
  2503. </itemizedlist>
  2504. </sect3>
  2505. <sect3 id="zend.db.adapter.adapter-notes.pdo-ibm">
  2506. <title>PDO for IBM DB2 and Informix Dynamic Server (IDS)</title>
  2507. <itemizedlist>
  2508. <listitem>
  2509. <para>
  2510. Specify this Adapter to the
  2511. <methodname>factory()</methodname>
  2512. method with the name 'Pdo_Ibm'.
  2513. </para>
  2514. </listitem>
  2515. <listitem>
  2516. <para>
  2517. This Adapter uses the
  2518. <acronym>PHP</acronym>
  2519. extensions pdo and pdo_ibm.
  2520. </para>
  2521. </listitem>
  2522. <listitem>
  2523. <para>
  2524. You must use at least
  2525. <acronym>PDO</acronym>
  2526. _IBM extension version 1.2.2.
  2527. If you have an earlier version of this
  2528. extension, you
  2529. must upgrade the
  2530. <acronym>PDO</acronym>
  2531. _IBM extension from
  2532. <acronym>PECL</acronym>
  2533. .
  2534. </para>
  2535. </listitem>
  2536. </itemizedlist>
  2537. </sect3>
  2538. <sect3 id="zend.db.adapter.adapter-notes.pdo-mssql">
  2539. <title>PDO Microsoft SQL Server</title>
  2540. <itemizedlist>
  2541. <listitem>
  2542. <para>
  2543. Specify this Adapter to the
  2544. <methodname>factory()</methodname>
  2545. method with the name 'Pdo_Mssql'.
  2546. </para>
  2547. </listitem>
  2548. <listitem>
  2549. <para>
  2550. This Adapter uses the
  2551. <acronym>PHP</acronym>
  2552. extensions pdo and pdo_mssql.
  2553. </para>
  2554. </listitem>
  2555. <listitem>
  2556. <para>
  2557. Microsoft
  2558. <acronym>SQL</acronym>
  2559. Server does not support sequences, so
  2560. <methodname>lastInsertId()</methodname>
  2561. ignores its arguments and
  2562. always returns the last value generated for an
  2563. auto-increment key. The
  2564. <methodname>lastSequenceId()</methodname>
  2565. method returns
  2566. <constant>NULL</constant>
  2567. .
  2568. </para>
  2569. </listitem>
  2570. <listitem>
  2571. <para>
  2572. If you are working with unicode strings in an encoding other than UCS-2
  2573. (such as UTF-8), you may have to perform a conversion in your application
  2574. code or store the data in a binary column. Please refer to
  2575. <ulink url="http://support.microsoft.com/kb/232580">Microsoft's Knowledge
  2576. Base</ulink>
  2577. for more information.
  2578. </para>
  2579. </listitem>
  2580. <listitem>
  2581. <para>
  2582. <classname>Zend_Db_Adapter_Pdo_Mssql</classname>
  2583. sets
  2584. <constant>QUOTED_IDENTIFIER ON</constant>
  2585. immediately after connecting to a
  2586. <acronym>SQL</acronym>
  2587. Server database. This makes the driver use the standard
  2588. <acronym>SQL</acronym>
  2589. identifier
  2590. delimiter symbol (
  2591. <code>"</code>
  2592. ) instead of the
  2593. proprietary square-brackets syntax
  2594. <acronym>SQL</acronym>
  2595. Server uses for
  2596. delimiting identifiers.
  2597. </para>
  2598. </listitem>
  2599. <listitem>
  2600. <para>
  2601. You can specify
  2602. <code>pdoType</code>
  2603. as a key in the
  2604. options array. The value can be "mssql" (the default),
  2605. "dblib", "freetds", or "sybase". This option affects
  2606. the DSN prefix the
  2607. adapter uses when constructing the
  2608. DSN string. Both "freetds" and "sybase"
  2609. imply a prefix
  2610. of "sybase:", which is used for the
  2611. <ulink url="http://www.freetds.org/">FreeTDS</ulink>
  2612. set
  2613. of libraries.
  2614. See also
  2615. <ulink url="http://www.php.net/manual/en/ref.pdo-dblib.connection.php">
  2616. http://www.php.net/manual/en/ref.pdo-dblib.connection.php</ulink>
  2617. for more information on the DSN prefixes used in this driver.
  2618. </para>
  2619. </listitem>
  2620. </itemizedlist>
  2621. </sect3>
  2622. <sect3 id="zend.db.adapter.adapter-notes.pdo-mysql">
  2623. <title>PDO MySQL</title>
  2624. <itemizedlist>
  2625. <listitem>
  2626. <para>
  2627. Specify this Adapter to the
  2628. <methodname>factory()</methodname>
  2629. method with the name 'Pdo_Mysql'.
  2630. </para>
  2631. </listitem>
  2632. <listitem>
  2633. <para>
  2634. This Adapter uses the
  2635. <acronym>PHP</acronym>
  2636. extensions pdo and pdo_mysql.
  2637. </para>
  2638. </listitem>
  2639. <listitem>
  2640. <para>
  2641. MySQL does not support sequences, so
  2642. <methodname>lastInsertId()</methodname>
  2643. ignores its arguments and
  2644. always returns the last value generated for an
  2645. auto-increment key. The
  2646. <methodname>lastSequenceId()</methodname>
  2647. method returns
  2648. <constant>NULL</constant>
  2649. .
  2650. </para>
  2651. </listitem>
  2652. </itemizedlist>
  2653. </sect3>
  2654. <sect3 id="zend.db.adapter.adapter-notes.pdo-oci">
  2655. <title>PDO Oracle</title>
  2656. <itemizedlist>
  2657. <listitem>
  2658. <para>
  2659. Specify this Adapter to the
  2660. <methodname>factory()</methodname>
  2661. method with the name 'Pdo_Oci'.
  2662. </para>
  2663. </listitem>
  2664. <listitem>
  2665. <para>
  2666. This Adapter uses the
  2667. <acronym>PHP</acronym>
  2668. extensions pdo and pdo_oci.
  2669. </para>
  2670. </listitem>
  2671. <listitem>
  2672. <para>
  2673. Oracle does not support auto-incrementing keys, so you
  2674. should specify the
  2675. name of a sequence to
  2676. <methodname>lastInsertId()</methodname>
  2677. or
  2678. <methodname>lastSequenceId()</methodname>
  2679. .
  2680. </para>
  2681. </listitem>
  2682. </itemizedlist>
  2683. </sect3>
  2684. <sect3 id="zend.db.adapter.adapter-notes.pdo-pgsql">
  2685. <title>PDO PostgreSQL</title>
  2686. <itemizedlist>
  2687. <listitem>
  2688. <para>
  2689. Specify this Adapter to the
  2690. <methodname>factory()</methodname>
  2691. method with the name 'Pdo_Pgsql'.
  2692. </para>
  2693. </listitem>
  2694. <listitem>
  2695. <para>
  2696. This Adapter uses the
  2697. <acronym>PHP</acronym>
  2698. extensions pdo and pdo_pgsql.
  2699. </para>
  2700. </listitem>
  2701. <listitem>
  2702. <para>
  2703. PostgreSQL supports both sequences and auto-incrementing
  2704. keys. Therefore the
  2705. arguments to
  2706. <methodname>lastInsertId()</methodname>
  2707. are optional. If you give
  2708. no arguments, the Adapter returns the last value
  2709. generated for an auto-increment key. If you give
  2710. arguments, the Adapter
  2711. returns the last value generated
  2712. by the sequence named according to the
  2713. convention
  2714. '
  2715. <emphasis>table</emphasis>
  2716. _
  2717. <emphasis>column</emphasis>
  2718. _seq'.
  2719. </para>
  2720. </listitem>
  2721. </itemizedlist>
  2722. </sect3>
  2723. <sect3 id="zend.db.adapter.adapter-notes.pdo-sqlite">
  2724. <title>PDO SQLite</title>
  2725. <itemizedlist>
  2726. <listitem>
  2727. <para>
  2728. Specify this Adapter to the
  2729. <methodname>factory()</methodname>
  2730. method with the name 'Pdo_Sqlite'.
  2731. </para>
  2732. </listitem>
  2733. <listitem>
  2734. <para>
  2735. This Adapter uses the
  2736. <acronym>PHP</acronym>
  2737. extensions pdo and pdo_sqlite.
  2738. </para>
  2739. </listitem>
  2740. <listitem>
  2741. <para>
  2742. SQLite does not support sequences, so
  2743. <methodname>lastInsertId()</methodname>
  2744. ignores its arguments and
  2745. always returns the last value generated for an
  2746. auto-increment key. The
  2747. <methodname>lastSequenceId()</methodname>
  2748. method returns
  2749. <constant>NULL</constant>
  2750. .
  2751. </para>
  2752. </listitem>
  2753. <listitem>
  2754. <para>
  2755. To connect to an SQLite2 database, specify
  2756. <code>'sqlite2'=>true</code>
  2757. in the array of
  2758. parameters when creating an instance of the
  2759. <classname>Pdo_Sqlite</classname>
  2760. Adapter.
  2761. </para>
  2762. </listitem>
  2763. <listitem>
  2764. <para>
  2765. To connect to an in-memory SQLite database,
  2766. specify
  2767. <code>'dbname'=>':memory:'</code>
  2768. in the
  2769. array of parameters when creating an instance of
  2770. the
  2771. <classname>Pdo_Sqlite</classname>
  2772. Adapter.
  2773. </para>
  2774. </listitem>
  2775. <listitem>
  2776. <para>
  2777. Older versions of the SQLite driver for
  2778. <acronym>PHP</acronym>
  2779. do not seem
  2780. to support the PRAGMA commands necessary to ensure that
  2781. short
  2782. column names are used in result sets. If you
  2783. have problems that your result
  2784. sets are returned with
  2785. keys of the form "tablename.columnname" when you do a
  2786. join query, then you should upgrade to the current
  2787. version of
  2788. <acronym>PHP</acronym>
  2789. .
  2790. </para>
  2791. </listitem>
  2792. </itemizedlist>
  2793. </sect3>
  2794. <sect3 id="zend.db.adapter.adapter-notes.firebird">
  2795. <title>Firebird/Interbase</title>
  2796. <itemizedlist>
  2797. <listitem>
  2798. <para>
  2799. This Adapter uses the
  2800. <acronym>PHP</acronym>
  2801. extension php_interbase.
  2802. </para>
  2803. </listitem>
  2804. <listitem>
  2805. <para>
  2806. Firebird/interbase does not support auto-incrementing keys,
  2807. so you should
  2808. specify the name of a sequence to
  2809. <methodname>lastInsertId()</methodname>
  2810. or
  2811. <methodname>lastSequenceId()</methodname>
  2812. .
  2813. </para>
  2814. </listitem>
  2815. <listitem>
  2816. <para>
  2817. Currently the
  2818. <constant>Zend_Db::CASE_FOLDING</constant>
  2819. option
  2820. is not supported by the Firebird/interbase adapter.
  2821. Unquoted
  2822. identifiers are automatically returned in
  2823. upper case.
  2824. </para>
  2825. </listitem>
  2826. <listitem>
  2827. <para>
  2828. Adapter name is
  2829. <classname>ZendX_Db_Adapter_Firebird</classname>
  2830. .
  2831. </para>
  2832. <para>
  2833. Remember to use the param adapterNamespace with value
  2834. <classname>ZendX_Db_Adapter</classname>
  2835. .
  2836. </para>
  2837. <para>
  2838. We recommend to update the gds32.dll (or linux equivalent) bundled with
  2839. php,
  2840. to the same version of the server. For Firebird the equivalent gds32.dll
  2841. is
  2842. fbclient.dll.
  2843. </para>
  2844. <para>
  2845. By default all identifiers (tables names, fields) are returned in upper
  2846. case.
  2847. </para>
  2848. </listitem>
  2849. </itemizedlist>
  2850. </sect3>
  2851. </sect2>
  2852. </sect1>
  2853. <!--
  2854. vim:se ts=4 sw=4 et:
  2855. -->