Zend_Db_Table.xml 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.table">
  4. <title>Zend_Db_Table</title>
  5. <sect2 id="zend.db.table.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <classname>Zend_Db_Table</classname> class is an object-oriented interface to database tables. It provides
  9. methods for many common operations on tables. The base class is extensible, so you can
  10. add custom logic.
  11. </para>
  12. <para>
  13. The <classname>Zend_Db_Table</classname> solution is an implementation of the
  14. <ulink url="http://www.martinfowler.com/eaaCatalog/tableDataGateway.html">Table Data
  15. Gateway</ulink> pattern. The solution also includes a class that implements the
  16. <ulink url="http://www.martinfowler.com/eaaCatalog/rowDataGateway.html">Row Data
  17. Gateway</ulink> pattern.
  18. </para>
  19. </sect2>
  20. <sect2 id="zend.db.table.defining">
  21. <title>Defining a Table Class</title>
  22. <para>
  23. For each table in your database that you want to access, define a class that extends
  24. <classname>Zend_Db_Table_Abstract</classname>.
  25. </para>
  26. <sect3 id="zend.db.table.defining.table-schema">
  27. <title>Defining the Table Name and Schema</title>
  28. <para>
  29. Declare the database table for which this class is defined, using the protected
  30. variable <code>$_name</code>. This is a string, and must contain the name of the
  31. table spelled as it appears in the database.
  32. </para>
  33. <example id="zend.db.table.defining.table-schema.example1">
  34. <title>Declaring a table class with explicit table name</title>
  35. <programlisting role="php"><![CDATA[
  36. class Bugs extends Zend_Db_Table_Abstract
  37. {
  38. protected $_name = 'bugs';
  39. }
  40. ]]></programlisting>
  41. </example>
  42. <para>
  43. If you don't specify the table name, it defaults to the name of the class. If you
  44. rely on this default, the class name must match the spelling of the table name as
  45. it appears in the database.
  46. </para>
  47. <example id="zend.db.table.defining.table-schema.example">
  48. <title>Declaring a table class with implicit table name</title>
  49. <programlisting role="php"><![CDATA[
  50. class bugs extends Zend_Db_Table_Abstract
  51. {
  52. // table name matches class name
  53. }
  54. ]]></programlisting>
  55. </example>
  56. <para>
  57. You can also declare the schema for the table, either with the protected variable
  58. <code>$_schema</code>, or with the schema prepended to the table name in the
  59. <code>$_name</code> property. Any schema specified with the <code>$_name</code>
  60. property takes precedence over a schema specified with the <code>$_schema</code>
  61. property. In some RDBMS brands, the term for schema is "database" or "tablespace,"
  62. but it is used similarly.
  63. </para>
  64. <example id="zend.db.table.defining.table-schema.example3">
  65. <title>Declaring a table class with schema</title>
  66. <programlisting role="php"><![CDATA[
  67. // First alternative:
  68. class Bugs extends Zend_Db_Table_Abstract
  69. {
  70. protected $_schema = 'bug_db';
  71. protected $_name = 'bugs';
  72. }
  73. // Second alternative:
  74. class Bugs extends Zend_Db_Table_Abstract
  75. {
  76. protected $_name = 'bug_db.bugs';
  77. }
  78. // If schemas are specified in both $_name and $_schema, the one
  79. // specified in $_name takes precedence:
  80. class Bugs extends Zend_Db_Table_Abstract
  81. {
  82. protected $_name = 'bug_db.bugs';
  83. protected $_schema = 'ignored';
  84. }
  85. ]]></programlisting>
  86. </example>
  87. <para>
  88. The schema and table names may also be specified via constructor configuration
  89. directives, which override any default values specified with the
  90. <code>$_name</code> and <code>$_schema</code> properties. A schema specification
  91. given with the <code>name</code> directive overrides any value provided with the
  92. <code>schema</code> option.
  93. </para>
  94. <example id="zend.db.table.defining.table-schema.example.constructor">
  95. <title>Declaring table and schema names upon instantiation</title>
  96. <programlisting role="php"><![CDATA[
  97. class Bugs extends Zend_Db_Table_Abstract
  98. {
  99. }
  100. // First alternative:
  101. $tableBugs = new Bugs(array('name' => 'bugs', 'schema' => 'bug_db'));
  102. // Second alternative:
  103. $tableBugs = new Bugs(array('name' => 'bug_db.bugs'));
  104. // If schemas are specified in both 'name' and 'schema', the one
  105. // specified in 'name' takes precedence:
  106. $tableBugs = new Bugs(array('name' => 'bug_db.bugs',
  107. 'schema' => 'ignored'));
  108. ]]></programlisting>
  109. </example>
  110. <para>
  111. If you don't specify the schema name, it defaults to the schema to which your
  112. database adapter instance is connected.
  113. </para>
  114. </sect3>
  115. <sect3 id="zend.db.table.defining.primary-key">
  116. <title>Defining the Table Primary Key</title>
  117. <para>
  118. Every table must have a primary key. You can declare the column for the primary key
  119. using the protected variable <code>$_primary</code>. This is either a string that
  120. names the single column for the primary key, or else it is an array of column names
  121. if your primary key is a compound key.
  122. </para>
  123. <example id="zend.db.table.defining.primary-key.example">
  124. <title>Example of specifying the primary key</title>
  125. <programlisting role="php"><![CDATA[
  126. class Bugs extends Zend_Db_Table_Abstract
  127. {
  128. protected $_name = 'bugs';
  129. protected $_primary = 'bug_id';
  130. }
  131. ]]></programlisting>
  132. </example>
  133. <para>
  134. If you don't specify the primary key, <classname>Zend_Db_Table_Abstract</classname> tries to discover the
  135. primary key based on the information provided by the <code>describeTable()</code>´
  136. method.
  137. </para>
  138. <note>
  139. <para>
  140. Every table class must know which column(s) can be used to address rows
  141. uniquely. If no primary key column(s) are specified in the table class
  142. definition or the table constructor arguments, or discovered in the table
  143. metadata provided by <code>describeTable()</code>, then the table cannot be
  144. used with <classname>Zend_Db_Table</classname>.
  145. </para>
  146. </note>
  147. </sect3>
  148. <sect3 id="zend.db.table.defining.setup">
  149. <title>Overriding Table Setup Methods</title>
  150. <para>
  151. When you create an instance of a Table class, the constructor calls a set of
  152. protected methods that initialize metadata for the table. You can extend any of
  153. these methods to define metadata explicitly. Remember to call the method of the
  154. same name in the parent class at the end of your method.
  155. </para>
  156. <example id="zend.db.table.defining.setup.example">
  157. <title>Example of overriding the _setupTableName() method</title>
  158. <programlisting role="php"><![CDATA[
  159. class Bugs extends Zend_Db_Table_Abstract
  160. {
  161. protected function _setupTableName()
  162. {
  163. $this->_name = 'bugs';
  164. parent::_setupTableName();
  165. }
  166. }
  167. ]]></programlisting>
  168. </example>
  169. <para>
  170. The setup methods you can override are the following:
  171. </para>
  172. <itemizedlist>
  173. <listitem>
  174. <para>
  175. <code>_setupDatabaseAdapter()</code> checks that an adapter has been
  176. provided; gets a default adapter from the registry if needed. By overriding
  177. this method, you can set a database adapter from some other source.
  178. </para>
  179. </listitem>
  180. <listitem>
  181. <para>
  182. <code>_setupTableName()</code> defaults the table name to the name of the
  183. class. By overriding this method, you can set the table name before this
  184. default behavior runs.
  185. </para>
  186. </listitem>
  187. <listitem>
  188. <para>
  189. <code>_setupMetadata()</code> sets the schema if the table name contains
  190. the pattern "schema.table"; calls <code>describeTable()</code> to get
  191. metadata information; defaults the <code>$_cols</code> array to the columns
  192. reported by <code>describeTable()</code>. By overriding this method, you
  193. can specify the columns.
  194. </para>
  195. </listitem>
  196. <listitem>
  197. <para>
  198. <code>_setupPrimaryKey()</code> defaults the primary key columns to those
  199. reported by <code>describeTable()</code>; checks that the primary key
  200. columns are included in the <code>$_cols</code> array. By overriding this
  201. method, you can specify the primary key columns.
  202. </para>
  203. </listitem>
  204. </itemizedlist>
  205. </sect3>
  206. <sect3 id="zend.db.table.initialization">
  207. <title>Table initialization</title>
  208. <para>
  209. If application-specific logic needs to be initialized when a Table class is
  210. constructed, you can select to move your tasks to the <code>init()</code> method,
  211. which is called after all Table metadata has been processed. This is recommended
  212. over the <code>__construct</code> method if you do not need to alter the metadata
  213. in any programmatic way.
  214. <example id="zend.db.table.defining.init.usage.example">
  215. <title>Example usage of init() method</title>
  216. <programlisting role="php"><![CDATA[
  217. class Bugs extends Zend_Db_Table_Abstract
  218. {
  219. protected $_observer;
  220. public function init()
  221. {
  222. $this->_observer = new MyObserverClass();
  223. }
  224. }
  225. ]]></programlisting>
  226. </example>
  227. </para>
  228. </sect3>
  229. </sect2>
  230. <sect2 id="zend.db.table.constructing">
  231. <title>Creating an Instance of a Table</title>
  232. <para>
  233. Before you use a Table class, create an instance using its constructor. The
  234. constructor's argument is an array of options. The most important option to a Table
  235. constructor is the database adapter instance, representing a live connection to an
  236. RDBMS. There are three ways of specifying the database adapter to a Table class, and
  237. these three ways are described below:
  238. </para>
  239. <sect3 id="zend.db.table.constructing.adapter">
  240. <title>Specifying a Database Adapter</title>
  241. <para>
  242. The first way to provide a database adapter to a Table class is by passing it as an
  243. object of type <classname>Zend_Db_Adapter_Abstract</classname> in the options array, identified by the key
  244. <code>'db'</code>.
  245. </para>
  246. <example id="zend.db.table.constructing.adapter.example">
  247. <title>Example of constructing a Table using an Adapter object</title>
  248. <programlisting role="php"><![CDATA[
  249. $db = Zend_Db::factory('PDO_MYSQL', $options);
  250. $table = new Bugs(array('db' => $db));
  251. ]]></programlisting>
  252. </example>
  253. </sect3>
  254. <sect3 id="zend.db.table.constructing.default-adapter">
  255. <title>Setting a Default Database Adapter</title>
  256. <para>
  257. The second way to provide a database adapter to a Table class is by declaring an
  258. object of type <classname>Zend_Db_Adapter_Abstract</classname> to be a default database adapter for all
  259. subsequent instances of Tables in your application. You can do this with the static
  260. method <classname>Zend_Db_Table_Abstract::setDefaultAdapter()</classname>. The argument is an
  261. object of type <classname>Zend_Db_Adapter_Abstract</classname>.
  262. </para>
  263. <example id="zend.db.table.constructing.default-adapter.example">
  264. <title>Example of constructing a Table using a the Default Adapter</title>
  265. <programlisting role="php"><![CDATA[
  266. $db = Zend_Db::factory('PDO_MYSQL', $options);
  267. Zend_Db_Table_Abstract::setDefaultAdapter($db);
  268. // Later...
  269. $table = new Bugs();
  270. ]]></programlisting>
  271. </example>
  272. <para>
  273. It can be convenient to create the database adapter object in a central place of
  274. your application, such as the bootstrap, and then store it as the default adapter.
  275. This gives you a means to ensure that the adapter instance is the same throughout
  276. your application. However, setting a default adapter is limited to a single adapter
  277. instance.
  278. </para>
  279. </sect3>
  280. <sect3 id="zend.db.table.constructing.registry">
  281. <title>Storing a Database Adapter in the Registry</title>
  282. <para>
  283. The third way to provide a database adapter to a Table class is by passing a string
  284. in the options array, also identified by the <code>'db'</code> key. The string is
  285. used as a key to the static <classname>Zend_Registry</classname> instance, where the entry at that key is
  286. an object of type <classname>Zend_Db_Adapter_Abstract</classname>.
  287. </para>
  288. <example id="zend.db.table.constructing.registry.example">
  289. <title>Example of constructing a Table using a Registry key</title>
  290. <programlisting role="php"><![CDATA[
  291. $db = Zend_Db::factory('PDO_MYSQL', $options);
  292. Zend_Registry::set('my_db', $db);
  293. // Later...
  294. $table = new Bugs(array('db' => 'my_db'));
  295. ]]></programlisting>
  296. </example>
  297. <para>
  298. Like setting the default adapter, this gives you the means to ensure that the same
  299. adapter instance is used throughout your application. Using the registry is more
  300. flexible, because you can store more than one adapter instance. A given adapter
  301. instance is specific to a certain RDBMS brand and database instance. If your
  302. application needs access to multiple databases or even multiple database brands,
  303. then you need to use multiple adapters.
  304. </para>
  305. </sect3>
  306. </sect2>
  307. <sect2 id="zend.db.table.insert">
  308. <title>Inserting Rows to a Table</title>
  309. <para>
  310. You can use the Table object to insert rows into the database table on which the Table
  311. object is based. Use the <code>insert()</code> method of your Table object. The
  312. argument is an associative array, mapping column names to values.
  313. </para>
  314. <example id="zend.db.table.insert.example">
  315. <title>Example of inserting to a Table</title>
  316. <programlisting role="php"><![CDATA[
  317. $table = new Bugs();
  318. $data = array(
  319. 'created_on' => '2007-03-22',
  320. 'bug_description' => 'Something wrong',
  321. 'bug_status' => 'NEW'
  322. );
  323. $table->insert($data);
  324. ]]></programlisting>
  325. </example>
  326. <para>
  327. By default, the values in your data array are inserted as literal values, using
  328. parameters. If you need them to be treated as SQL expressions, you must make sure they
  329. are distinct from plain strings. Use an object of type <classname>Zend_Db_Expr</classname> to do this.
  330. </para>
  331. <example id="zend.db.table.insert.example-expr">
  332. <title>Example of inserting expressions to a Table</title>
  333. <programlisting role="php"><![CDATA[
  334. $table = new Bugs();
  335. $data = array(
  336. 'created_on' => new Zend_Db_Expr('CURDATE()'),
  337. 'bug_description' => 'Something wrong',
  338. 'bug_status' => 'NEW'
  339. );
  340. ]]></programlisting>
  341. </example>
  342. <para>
  343. In the examples of inserting rows above, it is assumed that the table has an
  344. auto-incrementing primary key. This is the default behavior of <classname>Zend_Db_Table_Abstract</classname>,
  345. but there are other types of primary keys as well. The following sections describe how
  346. to support different types of primary keys.
  347. </para>
  348. <sect3 id="zend.db.table.insert.key-auto">
  349. <title>Using a Table with an Auto-incrementing Key</title>
  350. <para>
  351. An auto-incrementing primary key generates a unique integer value for you if you
  352. omit the primary key column from your SQL <code>INSERT</code> statement.
  353. </para>
  354. <para>
  355. In <classname>Zend_Db_Table_Abstract</classname>, if you define the protected variable
  356. <code>$_sequence</code> to be the Boolean value <code>true</code>, then the class
  357. assumes that the table has an auto-incrementing primary key.
  358. </para>
  359. <example id="zend.db.table.insert.key-auto.example">
  360. <title>Example of declaring a Table with auto-incrementing primary key</title>
  361. <programlisting role="php"><![CDATA[
  362. class Bugs extends Zend_Db_Table_Abstract
  363. {
  364. protected $_name = 'bugs';
  365. // This is the default in the Zend_Db_Table_Abstract class;
  366. // you do not need to define this.
  367. protected $_sequence = true;
  368. }
  369. ]]></programlisting>
  370. </example>
  371. <para>
  372. MySQL, Microsoft SQL Server, and SQLite are examples of RDBMS brands that support
  373. auto-incrementing primary keys.
  374. </para>
  375. <para>
  376. PostgreSQL has a <code>SERIAL</code> notation that implicitly defines a sequence
  377. based on the table and column name, and uses the sequence to generate key values
  378. for new rows. IBM DB2 has an <code>IDENTITY</code> notation that works similarly.
  379. If you use either of these notations, treat your <classname>Zend_Db_Table</classname> class as having an
  380. auto-incrementing column with respect to declaring the <code>$_sequence</code>
  381. member as <code>true</code>.
  382. </para>
  383. </sect3>
  384. <sect3 id="zend.db.table.insert.key-sequence">
  385. <title>Using a Table with a Sequence</title>
  386. <para>
  387. A sequence is a database object that generates a unique value, which can be used
  388. as a primary key value in one or more tables of the database.
  389. </para>
  390. <para>
  391. If you define <code>$_sequence</code> to be a string, then <classname>Zend_Db_Table_Abstract</classname>
  392. assumes the string to name a sequence object in the database. The sequence is
  393. invoked to generate a new value, and this value is used in the <code>INSERT</code>
  394. operation.
  395. </para>
  396. <example id="zend.db.table.insert.key-sequence.example">
  397. <title>Example of declaring a Table with a sequence</title>
  398. <programlisting role="php"><![CDATA[
  399. class Bugs extends Zend_Db_Table_Abstract
  400. {
  401. protected $_name = 'bugs';
  402. protected $_sequence = 'bug_sequence';
  403. }
  404. ]]></programlisting>
  405. </example>
  406. <para>
  407. Oracle, PostgreSQL, and IBM DB2 are examples of RDBMS brands that support sequence
  408. objects in the database.
  409. </para>
  410. <para>
  411. PostgreSQL and IBM DB2 also have syntax that defines sequences implicitly and
  412. associated with columns. If you use this notation, treat the table as having an
  413. auto-incrementing key column. Define the sequence name as a string only in cases
  414. where you would invoke the sequence explicitly to get the next key value.
  415. </para>
  416. </sect3>
  417. <sect3 id="zend.db.table.insert.key-natural">
  418. <title>Using a Table with a Natural Key</title>
  419. <para>
  420. Some tables have a natural key. This means that the key is not automatically
  421. generated by the table or by a sequence. You must specify the value for the primary
  422. key in this case.
  423. </para>
  424. <para>
  425. If you define the <code>$_sequence</code> to be the Boolean value
  426. <code>false</code>, then <classname>Zend_Db_Table_Abstract</classname> assumes that the table has a
  427. natural primary key. You must provide values for the primary key columns in the
  428. array of data to the <code>insert()</code> method, or else this method throws a
  429. <classname>Zend_Db_Table_Exception</classname>.
  430. </para>
  431. <example id="zend.db.table.insert.key-natural.example">
  432. <title>Example of declaring a Table with a natural key</title>
  433. <programlisting role="php"><![CDATA[
  434. class BugStatus extends Zend_Db_Table_Abstract
  435. {
  436. protected $_name = 'bug_status';
  437. protected $_sequence = false;
  438. }
  439. ]]></programlisting>
  440. </example>
  441. <note>
  442. <para>
  443. All RDBMS brands support tables with natural keys. Examples of tables that are
  444. often declared as having natural keys are lookup tables, intersection tables in
  445. many-to-many relationships, or most tables with compound primary keys.
  446. </para>
  447. </note>
  448. </sect3>
  449. </sect2>
  450. <sect2 id="zend.db.table.update">
  451. <title>Updating Rows in a Table</title>
  452. <para>
  453. You can update rows in a database table using the <code>update</code> method of a Table
  454. class. This method takes two arguments: an associative array of columns to change and
  455. new values to assign to these columns; and an SQL expression that is used in a
  456. <code>WHERE</code> clause, as criteria for the rows to change in the
  457. <code>UPDATE</code> operation.
  458. </para>
  459. <example id="zend.db.table.update.example">
  460. <title>Example of updating rows in a Table</title>
  461. <programlisting role="php"><![CDATA[
  462. $table = new Bugs();
  463. $data = array(
  464. 'updated_on' => '2007-03-23',
  465. 'bug_status' => 'FIXED'
  466. );
  467. $where = $table->getAdapter()->quoteInto('bug_id = ?', 1234);
  468. $table->update($data, $where);
  469. ]]></programlisting>
  470. </example>
  471. <para>
  472. Since the table <code>update()</code> method proxies to the database adapter
  473. <link linkend="zend.db.adapter.write.update"><code>update()</code></link> method, the
  474. second argument can be an array of SQL expressions. The expressions are combined as
  475. Boolean terms using an <code>AND</code> operator.
  476. </para>
  477. <note>
  478. <para>
  479. The values and identifiers in the SQL expression are not quoted for you. If you
  480. have values or identifiers that require quoting, you are responsible for doing
  481. this. Use the <code>quote()</code>, <code>quoteInto()</code>, and
  482. <code>quoteIdentifier()</code> methods of the database adapter.
  483. </para>
  484. </note>
  485. </sect2>
  486. <sect2 id="zend.db.table.delete">
  487. <title>Deleting Rows from a Table</title>
  488. <para>
  489. You can delete rows from a database table using the <code>delete()</code> method. This
  490. method takes one argument, which is an SQL expression that is used in a
  491. <code>WHERE</code> clause, as criteria for the rows to delete.
  492. </para>
  493. <example id="zend.db.table.delete.example">
  494. <title>Example of deleting rows from a Table</title>
  495. <programlisting role="php"><![CDATA[
  496. $table = new Bugs();
  497. $where = $table->getAdapter()->quoteInto('bug_id = ?', 1235);
  498. $table->delete($where);
  499. ]]></programlisting>
  500. </example>
  501. <para>
  502. Since the table <code>delete()</code> method proxies to the database adapter
  503. <link linkend="zend.db.adapter.write.delete"><code>delete()</code></link> method, the
  504. argument can also be an array of SQL expressions. The expressions are combined as
  505. Boolean terms using an <code>AND</code> operator.
  506. </para>
  507. <note>
  508. <para>
  509. The values and identifiers in the SQL expression are not quoted for you. If you
  510. have values or identifiers that require quoting, you are responsible for doing
  511. this. Use the <code>quote()</code>, <code>quoteInto()</code>, and
  512. <code>quoteIdentifier()</code> methods of the database adapter.
  513. </para>
  514. </note>
  515. </sect2>
  516. <sect2 id="zend.db.table.find">
  517. <title>Finding Rows by Primary Key</title>
  518. <para>
  519. You can query the database table for rows matching specific values in the primary key,
  520. using the <code>find()</code> method. The first argument of this method is either a
  521. single value or an array of values to match against the primary key of the table.
  522. </para>
  523. <example id="zend.db.table.find.example">
  524. <title>Example of finding rows by primary key values</title>
  525. <programlisting role="php"><![CDATA[
  526. $table = new Bugs();
  527. // Find a single row
  528. // Returns a Rowset
  529. $rows = $table->find(1234);
  530. // Find multiple rows
  531. // Also returns a Rowset
  532. $rows = $table->find(array(1234, 5678));
  533. ]]></programlisting>
  534. </example>
  535. <para>
  536. If you specify a single value, the method returns at most one row, because a primary
  537. key cannot have duplicate values and there is at most one row in the database table
  538. matching the value you specify. If you specify multiple values in an array, the method
  539. returns at most as many rows as the number of distinct values you specify.
  540. </para>
  541. <para>
  542. The <code>find()</code> method might return fewer rows than the number of values you
  543. specify for the primary key, if some of the values don't match any rows in the database
  544. table. The method even may return zero rows. Because the number of rows returned is
  545. variable, the <code>find()</code> method returns an object of type
  546. <classname>Zend_Db_Table_Rowset_Abstract</classname>.
  547. </para>
  548. <para>
  549. If the primary key is a compound key, that is, it consists of multiple columns, you can
  550. specify the additional columns as additional arguments to the <code>find()</code>
  551. method. You must provide as many arguments as the number of columns in the table's
  552. primary key.
  553. </para>
  554. <para>
  555. To find multiple rows from a table with a compound primary key, provide an array for
  556. each of the arguments. All of these arrays must have the same number of elements. The
  557. values in each array are formed into tuples in order; for example, the first element
  558. in all the array arguments define the first compound primary key value, then the second
  559. elements of all the arrays define the second compound primary key value, and so on.
  560. </para>
  561. <example id="zend.db.table.find.example-compound">
  562. <title>Example of finding rows by compound primary key values</title>
  563. <para>
  564. The call to <code>find()</code> below to match multiple rows can match two rows in
  565. the database. The first row must have primary key value (1234, 'ABC'), and the
  566. second row must have primary key value (5678, 'DEF').
  567. </para>
  568. <programlisting role="php"><![CDATA[
  569. class BugsProducts extends Zend_Db_Table_Abstract
  570. {
  571. protected $_name = 'bugs_products';
  572. protected $_primary = array('bug_id', 'product_id');
  573. }
  574. $table = new BugsProducts();
  575. // Find a single row with a compound primary key
  576. // Returns a Rowset
  577. $rows = $table->find(1234, 'ABC');
  578. // Find multiple rows with compound primary keys
  579. // Also returns a Rowset
  580. $rows = $table->find(array(1234, 5678), array('ABC', 'DEF'));
  581. ]]></programlisting>
  582. </example>
  583. </sect2>
  584. <sect2 id="zend.db.table.fetch-all">
  585. <title>Querying for a Set of Rows</title>
  586. <sect3 id="zend.db.table.fetch-all.select">
  587. <title>Select API</title>
  588. <para>
  589. <warning>
  590. <para>
  591. The API for fetch operations has been superseded to allow a
  592. <classname>Zend_Db_Table_Select</classname> object to modify the query. However, the
  593. deprecated usage of the <code>fetchRow()</code> and <code>fetchAll()</code>
  594. methods will continue to work without modification.
  595. </para>
  596. <para>
  597. The following statements are all legal and functionally identical, however
  598. it is recommended to update your code to take advantage of the new usage
  599. where possible.
  600. </para>
  601. <para>
  602. <programlisting role="php"><![CDATA[
  603. // Fetching a rowset
  604. $rows = $table->fetchAll('bug_status = "NEW"', 'bug_id ASC', 10, 0);
  605. $rows = $table->fetchAll($table->select()->where('bug_status = ?', 'NEW')
  606. ->order('bug_id ASC')
  607. ->limit(10, 0));
  608. // Fetching a single row
  609. $row = $table->fetchRow('bug_status = "NEW"', 'bug_id ASC');
  610. $row = $table->fetchRow($table->select()->where('bug_status = ?', 'NEW')
  611. ->order('bug_id ASC'));
  612. ]]></programlisting>
  613. </para>
  614. </warning>
  615. </para>
  616. <para>
  617. The <classname>Zend_Db_Table_Select</classname> object is an extension of the
  618. <classname>Zend_Db_Select</classname> object that applies specific restrictions to
  619. a query. The enhancements and restrictions are:
  620. </para>
  621. <itemizedlist>
  622. <listitem>
  623. <para>
  624. You <emphasis>can</emphasis> elect to return a subset of columns within a
  625. fetchRow or fetchAll query. This can provide optimization benefits where
  626. returning a large set of results for all columns is not desirable.
  627. </para>
  628. </listitem>
  629. <listitem>
  630. <para>
  631. You <emphasis>can</emphasis> specify columns that evaluate expressions from
  632. within the selected table. However this will mean that the returned row or
  633. rowset will be <property>readOnly</property> and cannot be used for save()
  634. operations. A <classname>Zend_Db_Table_Row</classname> with
  635. <property>readOnly</property> status will throw an exception if a
  636. <code>save()</code> operation is attempted.
  637. </para>
  638. </listitem>
  639. <listitem>
  640. <para>
  641. You <emphasis>can</emphasis> allow JOIN clauses on a select to allow
  642. multi-table lookups.
  643. </para>
  644. </listitem>
  645. <listitem>
  646. <para>
  647. You <emphasis>can not</emphasis> specify columns from a JOINed tabled to be
  648. returned in a row/rowset. Doing so will trigger a PHP error. This was done
  649. to ensure the integrity of the <classname>Zend_Db_Table</classname> is retained. i.e.
  650. A <classname>Zend_Db_Table_Row</classname> should only reference columns derived from
  651. its parent table.
  652. </para>
  653. </listitem>
  654. </itemizedlist>
  655. <para>
  656. <example id="zend.db.table.qry.rows.set.simple.usage.example">
  657. <title>Simple usage</title>
  658. <programlisting role="php"><![CDATA[
  659. $table = new Bugs();
  660. $select = $table->select();
  661. $select->where('bug_status = ?', 'NEW');
  662. $rows = $table->fetchAll($select);
  663. ]]></programlisting>
  664. </example>
  665. </para>
  666. <para>
  667. Fluent interfaces are implemented across the component, so this can be rewritten
  668. this in a more abbreviated form.
  669. </para>
  670. <para>
  671. <example id="zend.db.table.qry.rows.set.fluent.interface.example">
  672. <title>Example of fluent interface</title>
  673. <programlisting role="php"><![CDATA[
  674. $table = new Bugs();
  675. $rows =
  676. $table->fetchAll($table->select()->where('bug_status = ?', 'NEW'));
  677. ]]></programlisting>
  678. </example>
  679. </para>
  680. </sect3>
  681. <sect3 id="zend.db.table.fetch-all.usage">
  682. <title>Fetching a rowset</title>
  683. <para>
  684. You can query for a set of rows using any criteria other than the primary key
  685. values, using the <code>fetchAll()</code> method of the Table class. This method
  686. returns an object of type <classname>Zend_Db_Table_Rowset_Abstract</classname>.
  687. </para>
  688. <example id="zend.db.table.qry.rows.set.finding.row.example">
  689. <title>Example of finding rows by an expression</title>
  690. <programlisting role="php"><![CDATA[
  691. $table = new Bugs();
  692. $select = $table->select()->where('bug_status = ?', 'NEW');
  693. $rows = $table->fetchAll($select);
  694. ]]></programlisting>
  695. </example>
  696. <para>
  697. You may also pass sorting criteria in an <code>ORDER BY</code> clause, as well as
  698. count and offset integer values, used to make the query return a specific subset of
  699. rows. These values are used in a <code>LIMIT</code> clause, or in equivalent logic
  700. for RDBMS brands that do not support the <code>LIMIT</code> syntax.
  701. </para>
  702. <example id="zend.db.table.fetch-all.example2">
  703. <title>Example of finding rows by an expression</title>
  704. <programlisting role="php"><![CDATA[
  705. $table = new Bugs();
  706. $order = 'bug_id';
  707. // Return the 21st through 30th rows
  708. $count = 10;
  709. $offset = 20;
  710. $select = $table->select()->where(array('bug_status = ?' => 'NEW'))
  711. ->order($order)
  712. ->limit($count, $offset);
  713. $rows = $table->fetchAll($select);
  714. ]]></programlisting>
  715. </example>
  716. <para>
  717. All of the arguments above are optional. If you omit the ORDER clause, the result
  718. set includes rows from the table in an unpredictable order. If no LIMIT clause is
  719. set, you retrieve every row in the table that matches the WHERE clause.
  720. </para>
  721. </sect3>
  722. <sect3 id="zend.db.table.advanced.usage">
  723. <title>Advanced usage</title>
  724. <para>
  725. For more specific and optimized requests, you may wish to limit the number of
  726. columns returned in a row/rowset. This can be achieved by passing a FROM clause to
  727. the select object. The first argument in the FROM clause is identical to that of a
  728. <classname>Zend_Db_Select</classname> object with the addition of being able to pass an instance of
  729. <classname>Zend_Db_Table_Abstract</classname> and have it automatically determine the table name.
  730. </para>
  731. <para>
  732. <example id="zend.db.table.qry.rows.set.retrieving.a.example">
  733. <title>Retrieving specific columns</title>
  734. <programlisting role="php"><![CDATA[
  735. $table = new Bugs();
  736. $select = $table->select();
  737. $select->from($table, array('bug_id', 'bug_description'))
  738. ->where('bug_status = ?', 'NEW');
  739. $rows = $table->fetchAll($select);
  740. ]]></programlisting>
  741. </example>
  742. </para>
  743. <para>
  744. <important>
  745. <para>
  746. The rowset contains rows that are still 'valid' - they simply contain a
  747. subset of the columns of a table. If a save() method is called on a partial
  748. row then only the fields available will be modified.
  749. </para>
  750. </important>
  751. You can also specify expressions within a FROM clause and have these returned as a
  752. readOnly row/rowset. In this example we will return a rows from the bugs table that
  753. show an aggregate of the number of new bugs reported by individuals. Note the GROUP
  754. clause. The 'count' column will be made available to the row for evaluation and can
  755. be accessed as if it were part of the schema.
  756. </para>
  757. <para>
  758. <example id="zend.db.table.qry.rows.set.retrieving.b.example">
  759. <title>Retrieving expressions as columns</title>
  760. <programlisting role="php"><![CDATA[
  761. $table = new Bugs();
  762. $select = $table->select();
  763. $select->from($table,
  764. array('COUNT(reported_by) as `count`', 'reported_by'))
  765. ->where('bug_status = ?', 'NEW')
  766. ->group('reported_by');
  767. $rows = $table->fetchAll($select);
  768. ]]></programlisting>
  769. </example>
  770. You can also use a lookup as part of your query to further refine your fetch
  771. operations. In this example the accounts table is queried as part of a search for
  772. all new bugs reported by 'Bob'.
  773. </para>
  774. <para>
  775. <example id="zend.db.table.qry.rows.set.refine.example">
  776. <title>Using a lookup table to refine the results of fetchAll()</title>
  777. <programlisting role="php"><![CDATA[
  778. $table = new Bugs();
  779. $select = $table->select();
  780. $select->where('bug_status = ?', 'NEW')
  781. ->join('accounts', 'accounts.account_name = bugs.reported_by')
  782. ->where('accounts.account_name = ?', 'Bob');
  783. $rows = $table->fetchAll($select);
  784. ]]></programlisting>
  785. </example>
  786. </para>
  787. <para>
  788. The <classname>Zend_Db_Table_Select</classname> is primarily used to constrain and
  789. validate so that it may enforce the criteria for a legal SELECT query. However
  790. there may be certain cases where you require the flexibility of the
  791. <classname>Zend_Db_Table_Row</classname> component and do not require a writable or deletable row. For
  792. this specific user case, it is possible to retrieve a row/rowset by passing a false
  793. value to setIntegrityCheck. The resulting row/rowset will be returned as a 'locked'
  794. row (meaning the save(), delete() and any field-setting methods will throw an
  795. exception).
  796. </para>
  797. <example id="zend.db.table.qry.rows.set.integrity.example">
  798. <title>Removing the integrity check on Zend_Db_Table_Select to allow JOINed rows</title>
  799. <programlisting><![CDATA[
  800. $table = new Bugs();
  801. $select = $table->select()->setIntegrityCheck(false);
  802. $select->where('bug_status = ?', 'NEW')
  803. ->join('accounts',
  804. 'accounts.account_name = bugs.reported_by',
  805. 'account_name')
  806. ->where('accounts.account_name = ?', 'Bob');
  807. $rows = $table->fetchAll($select);
  808. ]]></programlisting>
  809. </example>
  810. </sect3>
  811. </sect2>
  812. <sect2 id="zend.db.table.fetch-row">
  813. <title>Querying for a Single Row</title>
  814. <para>
  815. You can query for a single row using criteria similar to that of the
  816. <code>fetchAll()</code> method.
  817. </para>
  818. <example id="zend.db.table.fetch-row.example1">
  819. <title>Example of finding a single row by an expression</title>
  820. <programlisting role="php"><![CDATA[
  821. $table = new Bugs();
  822. $select = $table->select()->where('bug_status = ?', 'NEW')
  823. ->order('bug_id');
  824. $row = $table->fetchRow($select);
  825. ]]></programlisting>
  826. </example>
  827. <para>
  828. This method returns an object of type <classname>Zend_Db_Table_Row_Abstract</classname>. If the search
  829. criteria you specified match no rows in the database table, then
  830. <code>fetchRow()</code> returns PHP's <code>null</code> value.
  831. </para>
  832. </sect2>
  833. <sect2 id="zend.db.table.info">
  834. <title>Retrieving Table Metadata Information</title>
  835. <para>
  836. The <classname>Zend_Db_Table_Abstract</classname> class provides some information about its metadata. The
  837. <code>info()</code> method returns an array structure with information about the table,
  838. its columns and primary key, and other metadata.
  839. </para>
  840. <example id="zend.db.table.info.example">
  841. <title>Example of getting the table name</title>
  842. <programlisting role="php"><![CDATA[
  843. $table = new Bugs();
  844. $info = $table->info();
  845. echo "The table name is " . $info['name'] . "\n";
  846. ]]></programlisting>
  847. </example>
  848. <para>
  849. The keys of the array returned by the <code>info()</code> method are described below:
  850. </para>
  851. <itemizedlist>
  852. <listitem>
  853. <para>
  854. <emphasis role="strong">name</emphasis> => the name of the table.
  855. </para>
  856. </listitem>
  857. <listitem>
  858. <para>
  859. <emphasis role="strong">cols</emphasis> => an array, naming the column(s) of
  860. the table.
  861. </para>
  862. </listitem>
  863. <listitem>
  864. <para>
  865. <emphasis role="strong">primary</emphasis> => an array, naming the column(s) in
  866. the primary key.
  867. </para>
  868. </listitem>
  869. <listitem>
  870. <para>
  871. <emphasis role="strong">metadata</emphasis> => an associative array, mapping
  872. column names to information about the columns. This is the information returned
  873. by the <code>describeTable()</code> method.
  874. </para>
  875. </listitem>
  876. <listitem>
  877. <para>
  878. <emphasis role="strong">rowClass</emphasis> => the name of the concrete class
  879. used for Row objects returned by methods of this table instance. This defaults
  880. to <classname>Zend_Db_Table_Row</classname>.
  881. </para>
  882. </listitem>
  883. <listitem>
  884. <para>
  885. <emphasis role="strong">rowsetClass</emphasis> => the name of the concrete
  886. class used for Rowset objects returned by methods of this table instance. This
  887. defaults to <classname>Zend_Db_Table_Rowset</classname>.
  888. </para>
  889. </listitem>
  890. <listitem>
  891. <para>
  892. <emphasis role="strong">referenceMap</emphasis> => an associative array, with
  893. information about references from this table to any parent tables. See
  894. <xref linkend="zend.db.table.relationships.defining" />.
  895. </para>
  896. </listitem>
  897. <listitem>
  898. <para>
  899. <emphasis role="strong">dependentTables</emphasis> => an array of class names
  900. of tables that reference this table. See
  901. <xref linkend="zend.db.table.relationships.defining" />.
  902. </para>
  903. </listitem>
  904. <listitem>
  905. <para>
  906. <emphasis role="strong">schema</emphasis> => the name of the schema (or
  907. database or tablespace) for this table.
  908. </para>
  909. </listitem>
  910. </itemizedlist>
  911. </sect2>
  912. <sect2 id="zend.db.table.metadata.caching">
  913. <title>Caching Table Metadata</title>
  914. <para>
  915. By default, <classname>Zend_Db_Table_Abstract</classname> queries the
  916. underlying database for <link linkend="zend.db.table.info">table
  917. metadata</link> whenever that data is needed to perform table
  918. operations. The table object fetches the table metadata from the
  919. database using the adapter's <code>describeTable()</code> method.
  920. Operations requiring this introspection include:
  921. </para>
  922. <itemizedlist>
  923. <listitem><para><code>insert()</code></para></listitem>
  924. <listitem><para><code>find()</code></para></listitem>
  925. <listitem><para><code>info()</code></para></listitem>
  926. </itemizedlist>
  927. <para>
  928. In some circumstances, particularly when many table objects are instantiated against
  929. the same database table, querying the database for the table metadata for each instance
  930. may be undesirable from a performance standpoint. In such cases, users may benefit by
  931. caching the table metadata retrieved from the database.
  932. </para>
  933. <para>
  934. There are two primary ways in which a user may take advantage of table metadata
  935. caching:
  936. <itemizedlist>
  937. <listitem>
  938. <para>
  939. <emphasis role="strong">Call
  940. <classname>Zend_Db_Table_Abstract::setDefaultMetadataCache()</classname></emphasis> - This allows
  941. a developer to once set the default cache object to be used for all table
  942. classes.
  943. </para>
  944. </listitem>
  945. <listitem>
  946. <para>
  947. <emphasis role="strong">Configure
  948. <classname>Zend_Db_Table_Abstract::__construct()</classname></emphasis> - This allows a developer
  949. to set the cache object to be used for a particular table class instance.
  950. </para>
  951. </listitem>
  952. </itemizedlist>
  953. In both cases, the cache specification must be either <code>null</code> (i.e., no cache
  954. used) or an instance of
  955. <link linkend="zend.cache.frontends.core"><classname>Zend_Cache_Core</classname></link>. The
  956. methods may be used in conjunction when it is desirable to have both a default metadata
  957. cache and the ability to change the cache for individual table objects.
  958. </para>
  959. <example id="zend.db.table.metadata.caching-default">
  960. <title>Using a Default Metadata Cache for all Table Objects</title>
  961. <para>
  962. The following code demonstrates how to set a default metadata cache to be used for
  963. all table objects:
  964. </para>
  965. <programlisting role="php"><![CDATA[<
  966. // First, set up the Cache
  967. $frontendOptions = array(
  968. 'automatic_serialization' => true
  969. );
  970. $backendOptions = array(
  971. 'cache_dir' => 'cacheDir'
  972. );
  973. $cache = Zend_Cache::factory('Core',
  974. 'File',
  975. $frontendOptions,
  976. $backendOptions);
  977. // Next, set the cache to be used with all table objects
  978. Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
  979. // A table class is also needed
  980. class Bugs extends Zend_Db_Table_Abstract
  981. {
  982. // ...
  983. }
  984. // Each instance of Bugs now uses the default metadata cache
  985. $bugs = new Bugs();
  986. ]]></programlisting>
  987. </example>
  988. <example id="zend.db.table.metadata.caching-instance">
  989. <title>Using a Metadata Cache for a Specific Table Object</title>
  990. <para>
  991. The following code demonstrates how to set a metadata cache for a specific table
  992. object instance:
  993. </para>
  994. <programlisting role="php"><![CDATA[
  995. // First, set up the Cache
  996. $frontendOptions = array(
  997. 'automatic_serialization' => true
  998. );
  999. $backendOptions = array(
  1000. 'cache_dir' => 'cacheDir'
  1001. );
  1002. $cache = Zend_Cache::factory('Core',
  1003. 'File',
  1004. $frontendOptions,
  1005. $backendOptions);
  1006. // A table class is also needed
  1007. class Bugs extends Zend_Db_Table_Abstract
  1008. {
  1009. // ...
  1010. }
  1011. // Configure an instance upon instantiation
  1012. $bugs = new Bugs(array('metadataCache' => $cache));
  1013. ]]></programlisting>
  1014. </example>
  1015. <note>
  1016. <title>Automatic Serialization with the Cache Frontend</title>
  1017. <para>
  1018. Since the information returned from the adapter's describeTable() method is an
  1019. array, ensure that the <code>automatic_serialization</code> option is set to
  1020. <code>true</code> for the <classname>Zend_Cache_Core</classname> frontend.
  1021. </para>
  1022. </note>
  1023. <para>
  1024. Though the above examples use <classname>Zend_Cache_Backend_File</classname>, developers may use
  1025. whatever cache backend is appropriate for the situation. Please see
  1026. <link linkend="zend.cache">Zend_Cache</link> for more information.
  1027. </para>
  1028. <sect3 id="zend.db.table.metadata.caching.hardcoding">
  1029. <title>Hardcoding Table Metadata</title>
  1030. <para>
  1031. To take metadata caching a step further, you can also choose to
  1032. hardcode metadata. In this particular case, however, any changes
  1033. to the table schema will require a change in your code. As such,
  1034. it is only recommended for those who are optimizing for
  1035. production usage.
  1036. </para>
  1037. <para>
  1038. The metadata structure is as follows:
  1039. </para>
  1040. <programlisting role="php"><![CDATA[
  1041. protected $_metadata = array(
  1042. '<column_name>' => array(
  1043. 'SCHEMA_NAME' => <string>,
  1044. 'TABLE_NAME' => <string>,
  1045. 'COLUMN_NAME' => <string>,
  1046. 'COLUMN_POSITION' => <int>,
  1047. 'DATA_TYPE' => <string>,
  1048. 'DEFAULT' => NULL|<value>,
  1049. 'NULLABLE' => <bool>,
  1050. 'LENGTH' => <string - length>,
  1051. 'SCALE' => NULL|<value>,
  1052. 'PRECISION' => NULL|<value>,
  1053. 'UNSIGNED' => NULL|<bool>,
  1054. 'PRIMARY' => <bool>,
  1055. 'PRIMARY_POSITION' => <int>,
  1056. 'IDENTITY' => <bool>,
  1057. ),
  1058. // additional columns...
  1059. );
  1060. ]]></programlisting>
  1061. <para>
  1062. An easy way to get the appropriate values is to use the metadata
  1063. cache, and then to deserialize values stored in the cache.
  1064. </para>
  1065. <para>
  1066. You can disable this optimization by turning of the
  1067. <code>metadataCacheInClass</code> flag:
  1068. </para>
  1069. <programlisting role="php"><![CDATA[
  1070. // At instantiation:
  1071. $bugs = new Bugs(array('metadataCacheInClass' => false));
  1072. // Or later:
  1073. $bugs->setMetadataCacheInClass(false);
  1074. ]]></programlisting>
  1075. <para>
  1076. The flag is enabled by default, which ensures that the
  1077. <code>$_metadata</code> array is only populated once per
  1078. instance.
  1079. </para>
  1080. </sect3>
  1081. </sect2>
  1082. <sect2 id="zend.db.table.extending">
  1083. <title>Customizing and Extending a Table Class</title>
  1084. <sect3 id="zend.db.table.extending.row-rowset">
  1085. <title>Using Custom Row or Rowset Classes</title>
  1086. <para>
  1087. By default, methods of the Table class return a Rowset in instances of the concrete
  1088. class <classname>Zend_Db_Table_Rowset</classname>, and Rowsets contain a collection of instances of the
  1089. concrete class <classname>Zend_Db_Table_Row</classname> You can specify an alternative class to use for
  1090. either of these, but they must be classes that extend <classname>Zend_Db_Table_Rowset_Abstract</classname>
  1091. and <classname>Zend_Db_Table_Row_Abstract</classname>, respectively.
  1092. </para>
  1093. <para>
  1094. You can specify Row and Rowset classes using the Table constructor's options array,
  1095. in keys <code>'rowClass'</code> and <code>'rowsetClass'</code> respectively.
  1096. Specify the names of the classes using strings.
  1097. </para>
  1098. <example id="zend.db.table.extending.row-rowset.example">
  1099. <title>Example of specifying the Row and Rowset classes</title>
  1100. <programlisting role="php"><![CDATA[
  1101. class My_Row extends Zend_Db_Table_Row_Abstract
  1102. {
  1103. ...
  1104. }
  1105. class My_Rowset extends Zend_Db_Table_Rowset_Abstract
  1106. {
  1107. ...
  1108. }
  1109. $table = new Bugs(
  1110. array(
  1111. 'rowClass' => 'My_Row',
  1112. 'rowsetClass' => 'My_Rowset'
  1113. )
  1114. );
  1115. $where = $table->getAdapter()->quoteInto('bug_status = ?', 'NEW')
  1116. // Returns an object of type My_Rowset,
  1117. // containing an array of objects of type My_Row.
  1118. $rows = $table->fetchAll($where);
  1119. ]]></programlisting>
  1120. </example>
  1121. <para>
  1122. You can change the classes by specifying them with the <code>setRowClass()</code>
  1123. and <code>setRowsetClass()</code> methods. This applies to rows and rowsets created
  1124. subsequently; it does not change the class of any row or rowset objects you have
  1125. created previously.
  1126. </para>
  1127. <example id="zend.db.table.extending.row-rowset.example2">
  1128. <title>Example of changing the Row and Rowset classes</title>
  1129. <programlisting role="php"><![CDATA[
  1130. $table = new Bugs();
  1131. $where = $table->getAdapter()->quoteInto('bug_status = ?', 'NEW')
  1132. // Returns an object of type Zend_Db_Table_Rowset
  1133. // containing an array of objects of type Zend_Db_Table_Row.
  1134. $rowsStandard = $table->fetchAll($where);
  1135. $table->setRowClass('My_Row');
  1136. $table->setRowsetClass('My_Rowset');
  1137. // Returns an object of type My_Rowset,
  1138. // containing an array of objects of type My_Row.
  1139. $rowsCustom = $table->fetchAll($where);
  1140. // The $rowsStandard object still exists, and it is unchanged.
  1141. ]]></programlisting>
  1142. </example>
  1143. <para>
  1144. For more information on the Row and Rowset classes, see
  1145. <xref linkend="zend.db.table.row" /> and <xref linkend="zend.db.table.rowset" />.
  1146. </para>
  1147. </sect3>
  1148. <sect3 id="zend.db.table.extending.insert-update">
  1149. <title>Defining Custom Logic for Insert, Update, and Delete</title>
  1150. <para>
  1151. You can override the <code>insert()</code> and <code>update()</code> methods in
  1152. your Table class. This gives you the opportunity to implement custom code that is
  1153. executed before performing the database operation. Be sure to call the parent class
  1154. method when you are done.
  1155. </para>
  1156. <example id="zend.db.table.extending.insert-update.example">
  1157. <title>Custom logic to manage timestamps</title>
  1158. <programlisting role="php"><![CDATA[
  1159. class Bugs extends Zend_Db_Table_Abstract
  1160. {
  1161. protected $_name = 'bugs';
  1162. public function insert(array $data)
  1163. {
  1164. // add a timestamp
  1165. if (empty($data['created_on'])) {
  1166. $data['created_on'] = time();
  1167. }
  1168. return parent::insert($data);
  1169. }
  1170. public function update(array $data, $where)
  1171. {
  1172. // add a timestamp
  1173. if (empty($data['updated_on'])) {
  1174. $data['updated_on'] = time();
  1175. }
  1176. return parent::update($data, $where);
  1177. }
  1178. }
  1179. ]]></programlisting>
  1180. </example>
  1181. <para>
  1182. You can also override the <code>delete()</code> method.
  1183. </para>
  1184. </sect3>
  1185. <sect3 id="zend.db.table.extending.finders">
  1186. <title>Define Custom Search Methods in Zend_Db_Table</title>
  1187. <para>
  1188. You can implement custom query methods in your Table class, if you have frequent
  1189. need to do queries against this table with specific criteria. Most queries can be
  1190. written using <code>fetchAll()</code>, but this requires that you duplicate code to
  1191. form the query conditions if you need to run the query in several places in your
  1192. application. Therefore it can be convenient to implement a method in the Table
  1193. class to perform frequently-used queries against this table.
  1194. </para>
  1195. <example id="zend.db.table.extending.finders.example">
  1196. <title>Custom method to find bugs by status</title>
  1197. <programlisting role="php"><![CDATA[
  1198. class Bugs extends Zend_Db_Table_Abstract
  1199. {
  1200. protected $_name = 'bugs';
  1201. public function findByStatus($status)
  1202. {
  1203. $where = $this->getAdapter()->quoteInto('bug_status = ?', $status);
  1204. return $this->fetchAll($where, 'bug_id');
  1205. }
  1206. }
  1207. ]]></programlisting>
  1208. </example>
  1209. </sect3>
  1210. <sect3 id="zend.db.table.extending.inflection">
  1211. <title>Define Inflection in Zend_Db_Table</title>
  1212. <para>
  1213. Some people prefer that the table class name match a table name in the RDBMS by
  1214. using a string transformation called <emphasis>inflection</emphasis>.
  1215. </para>
  1216. <para>
  1217. For example, if your table class name is "<code>BugsProducts</code>", it would
  1218. match the physical table in the database called "<code>bugs_products</code>," if
  1219. you omit the explicit declaration of the <code>$_name</code> class property. In
  1220. this inflection mapping, the class name spelled in "CamelCase" format would be
  1221. transformed to lower case, and words are separated with an underscore.
  1222. </para>
  1223. <para>
  1224. You can specify the database table name independently from the class name by
  1225. declaring the table name with the <code>$_name</code> class property in each of
  1226. your table classes.
  1227. </para>
  1228. <para>
  1229. <classname>Zend_Db_Table_Abstract</classname> performs no inflection to map the class name to the table
  1230. name. If you omit the declaration of <code>$_name</code> in your table class, the
  1231. class maps to a database table that matches the spelling of the class name exactly.
  1232. </para>
  1233. <para>
  1234. It is inappropriate to transform identifiers from the database, because this can
  1235. lead to ambiguity or make some identifiers inaccessible. Using the SQL identifiers
  1236. exactly as they appear in the database makes <classname>Zend_Db_Table_Abstract</classname> both simpler
  1237. and more flexible.
  1238. </para>
  1239. <para>
  1240. If you prefer to use inflection, then you must implement the transformation
  1241. yourself, by overriding the <code>_setupTableName()</code> method in your Table
  1242. classes. One way to do this is to define an abstract class that extends
  1243. <classname>Zend_Db_Table_Abstract</classname>, and then the rest of your tables extend your new abstract
  1244. class.
  1245. </para>
  1246. <example id="zend.db.table.extending.inflection.example">
  1247. <title>Example of an abstract table class that implements inflection</title>
  1248. <programlisting role="php"><![CDATA[
  1249. abstract class MyAbstractTable extends Zend_Db_Table_Abstract
  1250. {
  1251. protected function _setupTableName()
  1252. {
  1253. if (!$this->_name) {
  1254. $this->_name = myCustomInflector(get_class($this));
  1255. }
  1256. parent::_setupTableName();
  1257. }
  1258. }
  1259. class BugsProducts extends MyAbstractTable
  1260. {
  1261. }
  1262. ]]></programlisting>
  1263. </example>
  1264. <para>
  1265. You are responsible for writing the functions to perform inflection transformation.
  1266. Zend Framework does not provide such a function.
  1267. </para>
  1268. </sect3>
  1269. </sect2>
  1270. </sect1>
  1271. <!--
  1272. vim:se ts=4 sw=4 et:
  1273. -->