Zend_Db_Table.xml 60 KB

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