Zend_Db_Table.xml 61 KB

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