Zend_Db_Table.xml 60 KB

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