Zend_Db_Table_Row.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.table.row">
  4. <title>Zend_Db_Table_Row</title>
  5. <sect2 id="zend.db.table.row.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. <classname>Zend_Db_Table_Row</classname> is a class that contains an individual row of a
  9. <classname>Zend_Db_Table</classname> object. When you run a query against a Table class,
  10. the result is returned in a set of <classname>Zend_Db_Table_Row</classname> objects. You
  11. can also use this object to create new rows and add them to the database table.
  12. </para>
  13. <para>
  14. <classname>Zend_Db_Table_Row</classname> is an implementation of the <ulink
  15. url="http://www.martinfowler.com/eaaCatalog/rowDataGateway.html">Row Data
  16. Gateway</ulink> pattern.
  17. </para>
  18. </sect2>
  19. <sect2 id="zend.db.table.row.read">
  20. <title>Fetching a Row</title>
  21. <para>
  22. <classname>Zend_Db_Table_Abstract</classname> provides methods <code>find()</code> and
  23. <code>fetchAll()</code>, which each return an object of type
  24. <classname>Zend_Db_Table_Rowset</classname>, and the method <code>fetchRow()</code>,
  25. which returns an object of type <classname>Zend_Db_Table_Row</classname>.
  26. </para>
  27. <example id="zend.db.table.row.read.example">
  28. <title>Example of fetching a row</title>
  29. <programlisting language="php"><![CDATA[
  30. $bugs = new Bugs();
  31. $row = $bugs->fetchRow($bugs->select()->where('bug_id = ?', 1));
  32. ]]></programlisting>
  33. </example>
  34. <para>
  35. A <classname>Zend_Db_Table_Rowset</classname> object contains a collection of
  36. <classname>Zend_Db_Table_Row</classname> objects. See <xref
  37. linkend="zend.db.table.rowset" />.
  38. </para>
  39. <example id="zend.db.table.row.read.example-rowset">
  40. <title>Example of reading a row in a rowset</title>
  41. <programlisting language="php"><![CDATA[
  42. $bugs = new Bugs();
  43. $rowset = $bugs->fetchAll($bugs->select()->where('bug_status = ?', 1));
  44. $row = $rowset->current();
  45. ]]></programlisting>
  46. </example>
  47. <sect3 id="zend.db.table.row.read.get">
  48. <title>Reading column values from a row</title>
  49. <para>
  50. <classname>Zend_Db_Table_Row_Abstract</classname> provides accessor methods so you
  51. can reference columns in the row as object properties.
  52. </para>
  53. <example id="zend.db.table.row.read.get.example">
  54. <title>Example of reading a column in a row</title>
  55. <programlisting language="php"><![CDATA[
  56. $bugs = new Bugs();
  57. $row = $bugs->fetchRow($bugs->select()->where('bug_id = ?', 1));
  58. // Echo the value of the bug_description column
  59. echo $row->bug_description;
  60. ]]></programlisting>
  61. </example>
  62. <note>
  63. <para>
  64. Earlier versions of <classname>Zend_Db_Table_Row</classname> mapped these column
  65. accessors to the database column names using a string transformation called
  66. <emphasis>inflection</emphasis>.
  67. </para>
  68. <para>
  69. Currently, <classname>Zend_Db_Table_Row</classname> does not implement
  70. inflection. Accessed property names need to match the spelling of the column
  71. names as they appear in your database.
  72. </para>
  73. </note>
  74. </sect3>
  75. <sect3 id="zend.db.table.row.read.to-array">
  76. <title>Retrieving Row Data as an Array</title>
  77. <para>
  78. You can access the row's data as an array using the <code>toArray()</code> method
  79. of the Row object. This returns an associative array of the column names to the
  80. column values.
  81. </para>
  82. <example id="zend.db.table.row.read.to-array.example">
  83. <title>Example of using the toArray() method</title>
  84. <programlisting language="php"><![CDATA[
  85. $bugs = new Bugs();
  86. $row = $bugs->fetchRow($bugs->select()->where('bug_id = ?', 1));
  87. // Get the column/value associative array from the Row object
  88. $rowArray = $row->toArray();
  89. // Now use it as a normal array
  90. foreach ($rowArray as $column => $value) {
  91. echo "Column: $column\n";
  92. echo "Value: $value\n";
  93. }
  94. ]]></programlisting>
  95. </example>
  96. <para>
  97. The array returned from <code>toArray()</code> is not updateable. You can modify
  98. values in the array as you can with any array, but you cannot save changes to this
  99. array to the database directly.
  100. </para>
  101. </sect3>
  102. <sect3 id="zend.db.table.row.read.relationships">
  103. <title>Fetching data from related tables</title>
  104. <para>
  105. The <classname>Zend_Db_Table_Row_Abstract</classname> class provides methods for
  106. fetching rows and rowsets from related tables. See <xref
  107. linkend="zend.db.table.relationships" /> for more information on table
  108. relationships.
  109. </para>
  110. </sect3>
  111. </sect2>
  112. <sect2 id="zend.db.table.row.write">
  113. <title>Writing rows to the database</title>
  114. <sect3 id="zend.db.table.row.write.set">
  115. <title>Changing column values in a row</title>
  116. <para>
  117. You can set individual column values using column accessors, similar to how the
  118. columns are read as object properties in the example above.
  119. </para>
  120. <para>
  121. Using a column accessor to set a value changes the column value of the row object
  122. in your application, but it does not commit the change to the database yet. You can
  123. do that with the <code>save()</code> method.
  124. </para>
  125. <example id="zend.db.table.row.write.set.example">
  126. <title>Example of changing a column in a row</title>
  127. <programlisting language="php"><![CDATA[
  128. $bugs = new Bugs();
  129. $row = $bugs->fetchRow($bugs->select()->where('bug_id = ?', 1));
  130. // Change the value of one or more columns
  131. $row->bug_status = 'FIXED';
  132. // UPDATE the row in the database with new values
  133. $row->save();
  134. ]]></programlisting>
  135. </example>
  136. </sect3>
  137. <sect3 id="zend.db.table.row.write.insert">
  138. <title>Inserting a new row</title>
  139. <para>
  140. You can create a new row for a given table with the <code>createRow()</code> method
  141. of the table class. You can access fields of this row with the object-oriented
  142. interface, but the row is not stored in the database until you call the
  143. <code>save()</code> method.
  144. </para>
  145. <example id="zend.db.table.row.write.insert.example">
  146. <title>Example of creating a new row for a table</title>
  147. <programlisting language="php"><![CDATA[
  148. $bugs = new Bugs();
  149. $newRow = $bugs->createRow();
  150. // Set column values as appropriate for your application
  151. $newRow->bug_description = '...description...';
  152. $newRow->bug_status = 'NEW';
  153. // INSERT the new row to the database
  154. $newRow->save();
  155. ]]></programlisting>
  156. </example>
  157. <para>
  158. The optional argument to the createRow() method is an associative array, with which
  159. you can populate fields of the new row.
  160. </para>
  161. <example id="zend.db.table.row.write.insert.example2">
  162. <title>Example of populating a new row for a table</title>
  163. <programlisting language="php"><![CDATA[
  164. $data = array(
  165. 'bug_description' => '...description...',
  166. 'bug_status' => 'NEW'
  167. );
  168. $bugs = new Bugs();
  169. $newRow = $bugs->createRow($data);
  170. // INSERT the new row to the database
  171. $newRow->save();
  172. ]]></programlisting>
  173. </example>
  174. <note>
  175. <para>
  176. The <code>createRow()</code> method was called <code>fetchNew()</code> in
  177. earlier releases of <classname>Zend_Db_Table</classname>. You are encouraged to
  178. use the new method name, even though the old name continues to work for the sake
  179. of backward compatibility.
  180. </para>
  181. </note>
  182. </sect3>
  183. <sect3 id="zend.db.table.row.write.set-from-array">
  184. <title>Changing values in multiple columns</title>
  185. <para>
  186. <classname>Zend_Db_Table_Row_Abstract</classname> provides the
  187. <code>setFromArray()</code> method to enable you to set several columns in a single
  188. row at once, specified in an associative array that maps the column names to values.
  189. You may find this method convenient for setting values both for new rows and for
  190. rows you need to update.
  191. </para>
  192. <example id="zend.db.table.row.write.set-from-array.example">
  193. <title>Example of using setFromArray() to set values in a new Row</title>
  194. <programlisting language="php"><![CDATA[
  195. $bugs = new Bugs();
  196. $newRow = $bugs->createRow();
  197. // Data are arranged in an associative array
  198. $data = array(
  199. 'bug_description' => '...description...',
  200. 'bug_status' => 'NEW'
  201. );
  202. // Set all the column values at once
  203. $newRow->setFromArray($data);
  204. // INSERT the new row to the database
  205. $newRow->save();
  206. ]]></programlisting>
  207. </example>
  208. </sect3>
  209. <sect3 id="zend.db.table.row.write.delete">
  210. <title>Deleting a row</title>
  211. <para>
  212. You can call the <code>delete()</code> method on a Row object. This deletes rows in
  213. the database matching the primary key in the Row object.
  214. </para>
  215. <example id="zend.db.table.row.write.delete.example">
  216. <title>Example of deleting a row</title>
  217. <programlisting language="php"><![CDATA[
  218. $bugs = new Bugs();
  219. $row = $bugs->fetchRow('bug_id = 1');
  220. // DELETE this row
  221. $row->delete();
  222. ]]></programlisting>
  223. </example>
  224. <para>
  225. You do not have to call <code>save()</code> to apply the delete; it is executed
  226. against the database immediately.
  227. </para>
  228. </sect3>
  229. </sect2>
  230. <sect2 id="zend.db.table.row.serialize">
  231. <title>Serializing and unserializing rows</title>
  232. <para>
  233. It is often convenient to save the contents of a database row to be used later.
  234. <emphasis>Serialization</emphasis> is the name for the operation that converts an
  235. object into a form that is easy to save in offline storage (for example, a file).
  236. Objects of type <classname>Zend_Db_Table_Row_Abstract</classname> are serializable.
  237. </para>
  238. <sect3 id="zend.db.table.row.serialize.serializing">
  239. <title>Serializing a Row</title>
  240. <para>
  241. Simply use PHP's <code>serialize()</code> function to create a string containing a
  242. byte-stream representation of the Row object argument.
  243. </para>
  244. <example id="zend.db.table.row.serialize.serializing.example">
  245. <title>Example of serializing a row</title>
  246. <programlisting language="php"><![CDATA[
  247. $bugs = new Bugs();
  248. $row = $bugs->fetchRow('bug_id = 1');
  249. // Convert object to serialized form
  250. $serializedRow = serialize($row);
  251. // Now you can write $serializedRow to a file, etc.
  252. ]]></programlisting>
  253. </example>
  254. </sect3>
  255. <sect3 id="zend.db.table.row.serialize.unserializing">
  256. <title>Unserializing Row Data</title>
  257. <para>
  258. Use PHP's <code>unserialize()</code> function to restore a string containing a
  259. byte-stream representation of an object. The function returns the original object.
  260. </para>
  261. <para>
  262. Note that the Row object returned is in a <emphasis>disconnected</emphasis> state.
  263. You can read the Row object and its properties, but you cannot change values in the
  264. Row or execute other methods that require a database connection (for example,
  265. queries against related tables).
  266. </para>
  267. <example id="zend.db.table.row.serialize.unserializing.example">
  268. <title>Example of unserializing a serialized row</title>
  269. <programlisting language="php"><![CDATA[
  270. $rowClone = unserialize($serializedRow);
  271. // Now you can use object properties, but read-only
  272. echo $rowClone->bug_description;
  273. ]]></programlisting>
  274. </example>
  275. <note>
  276. <title>Why do Rows unserialize in a disconnected state?</title>
  277. <para>
  278. A serialized object is a string that is readable to anyone who possesses it. It
  279. could be a security risk to store parameters such as database account and
  280. password in plain, unencrypted text in the serialized string. You would not
  281. want to store such data to a text file that is not protected, or send it in an
  282. email or other medium that is easily read by potential attackers. The reader of
  283. the serialized object should not be able to use it to gain access to your
  284. database without knowing valid credentials.
  285. </para>
  286. </note>
  287. </sect3>
  288. <sect3 id="zend.db.table.row.serialize.set-table">
  289. <title>Reactivating a Row as Live Data</title>
  290. <para>
  291. You can reactivate a disconnected Row, using the <code>setTable()</code> method.
  292. The argument to this method is a valid object of type
  293. <classname>Zend_Db_Table_Abstract</classname>, which you create. Creating a Table
  294. object requires a live connection to the database, so by reassociating the Table
  295. with the Row, the Row gains access to the database. Subsequently, you can change
  296. values in the Row object and save the changes to the database.
  297. </para>
  298. <example id="zend.db.table.row.serialize.set-table.example">
  299. <title>Example of reactivating a row</title>
  300. <programlisting language="php"><![CDATA[
  301. $rowClone = unserialize($serializedRow);
  302. $bugs = new Bugs();
  303. // Reconnect the row to a table, and
  304. // thus to a live database connection
  305. $rowClone->setTable($bugs);
  306. // Now you can make changes to the row and save them
  307. $rowClone->bug_status = 'FIXED';
  308. $rowClone->save();
  309. ]]></programlisting>
  310. </example>
  311. </sect3>
  312. </sect2>
  313. <sect2 id="zend.db.table.row.extending">
  314. <title>Extending the Row class</title>
  315. <para>
  316. <classname>Zend_Db_Table_Row</classname> is the default concrete class that extends
  317. <classname>Zend_Db_Table_Row_Abstract</classname>. You can define your own concrete
  318. class for instances of Row by extending
  319. <classname>Zend_Db_Table_Row_Abstract</classname>. To use your new Row class to store
  320. results of Table queries, specify the custom Row class by name either in the
  321. <varname>$_rowClass</varname> protected member of a Table class, or in the array
  322. argument of the constructor of a Table object.
  323. </para>
  324. <example id="zend.db.table.row.extending.example">
  325. <title>Specifying a custom Row class</title>
  326. <programlisting language="php"><![CDATA[
  327. class MyRow extends Zend_Db_Table_Row_Abstract
  328. {
  329. // ...customizations
  330. }
  331. // Specify a custom Row to be used by default
  332. // in all instances of a Table class.
  333. class Products extends Zend_Db_Table_Abstract
  334. {
  335. protected $_name = 'products';
  336. protected $_rowClass = 'MyRow';
  337. }
  338. // Or specify a custom Row to be used in one
  339. // instance of a Table class.
  340. $bugs = new Bugs(array('rowClass' => 'MyRow'));
  341. ]]></programlisting>
  342. </example>
  343. <sect3 id="zend.db.table.row.extending.overriding">
  344. <title>Row initialization</title>
  345. <para>
  346. If application-specific logic needs to be initialized when a row is constructed,
  347. you can select to move your tasks to the <code>init()</code> method, which is
  348. called after all row metadata has been processed. This is recommended over the
  349. <code>__construct</code> method if you do not need to alter the metadata in any
  350. programmatic way.
  351. <example id="zend.db.table.row.init.usage.example">
  352. <title>Example usage of init() method</title>
  353. <programlisting language="php"><![CDATA[
  354. class MyApplicationRow extends Zend_Db_Table_Row_Abstract
  355. {
  356. protected $_role;
  357. public function init()
  358. {
  359. $this->_role = new MyRoleClass();
  360. }
  361. }
  362. ]]></programlisting>
  363. </example>
  364. </para>
  365. </sect3>
  366. <sect3 id="zend.db.table.row.extending.insert-update">
  367. <title>Defining Custom Logic for Insert, Update, and Delete in Zend_Db_Table_Row</title>
  368. <para>
  369. The Row class calls protected methods <code>_insert()</code>,
  370. <code>_update()</code>, and <code>_delete()</code> before performing the
  371. corresponding operations <code>INSERT</code>, <code>UPDATE</code>, and
  372. <code>DELETE</code>. You can add logic to these methods in your custom Row
  373. subclass.
  374. </para>
  375. <para>
  376. If you need to do custom logic in a specific table, and the custom logic must occur
  377. for every operation on that table, it may make more sense to implement your custom
  378. code in the <code>insert()</code>, <code>update()</code> and <code>delete()</code>
  379. methods of your Table class. However, sometimes it may be necessary to do custom
  380. logic in the Row class.
  381. </para>
  382. <para>
  383. Below are some example cases where it might make sense to implement custom logic in
  384. a Row class instead of in the Table class:
  385. </para>
  386. <example id="zend.db.table.row.extending.overriding-example1">
  387. <title>Example of custom logic in a Row class</title>
  388. <para>
  389. The custom logic may not apply in all cases of operations on the respective
  390. Table. You can provide custom logic on demand by implementing it in a Row class
  391. and creating an instance of the Table class with that custom Row class
  392. specified. Otherwise, the Table uses the default Row class.
  393. </para>
  394. <para>
  395. You need data operations on this table to record the operation to a
  396. <classname>Zend_Log</classname> object, but only if the application
  397. configuration has enabled this behavior.
  398. </para>
  399. <programlisting language="php"><![CDATA[
  400. class MyLoggingRow extends Zend_Db_Table_Row_Abstract
  401. {
  402. protected function _insert()
  403. {
  404. $log = Zend_Registry::get('database_log');
  405. $log->info(Zend_Debug::dump($this->_data,
  406. "INSERT: $this->_tableClass",
  407. false)
  408. );
  409. }
  410. }
  411. // $loggingEnabled is an example property that depends
  412. // on your application configuration
  413. if ($loggingEnabled) {
  414. $bugs = new Bugs(array('rowClass' => 'MyLoggingRow'));
  415. } else {
  416. $bugs = new Bugs();
  417. }
  418. ]]></programlisting>
  419. </example>
  420. <example id="zend.db.table.row.extending.overriding-example2">
  421. <title>Example of a Row class that logs insert data for multiple tables</title>
  422. <para>
  423. The custom logic may be common to multiple tables. Instead of implementing the
  424. same custom logic in every one of your Table classes, you can implement the
  425. code for such actions in the definition of a Row class, and use this Row in
  426. each of your Table classes.
  427. </para>
  428. <para>
  429. In this example, the logging code is identical in all table classes.
  430. </para>
  431. <programlisting language="php"><![CDATA[
  432. class MyLoggingRow extends Zend_Db_Table_Row_Abstract
  433. {
  434. protected function _insert()
  435. {
  436. $log = Zend_Registry::get('database_log');
  437. $log->info(Zend_Debug::dump($this->_data,
  438. "INSERT: $this->_tableClass",
  439. false)
  440. );
  441. }
  442. }
  443. class Bugs extends Zend_Db_Table_Abstract
  444. {
  445. protected $_name = 'bugs';
  446. protected $_rowClass = 'MyLoggingRow';
  447. }
  448. class Products extends Zend_Db_Table_Abstract
  449. {
  450. protected $_name = 'products';
  451. protected $_rowClass = 'MyLoggingRow';
  452. }
  453. ]]></programlisting>
  454. </example>
  455. </sect3>
  456. <sect3 id="zend.db.table.row.extending.inflection">
  457. <title>Define Inflection in Zend_Db_Table_Row</title>
  458. <para>
  459. Some people prefer that the table class name match a table name in the RDBMS by
  460. using a string transformation called <emphasis>inflection</emphasis>.
  461. </para>
  462. <para>
  463. <classname>Zend_Db</classname> classes do not implement inflection by default. See
  464. <xref linkend="zend.db.table.extending.inflection" /> for an explanation of this
  465. policy.
  466. </para>
  467. <para>
  468. If you prefer to use inflection, then you must implement the transformation
  469. yourself, by overriding the <code>_transformColumn()</code> method in a custom Row
  470. class, and using that custom Row class when you perform queries against your Table
  471. class.
  472. </para>
  473. <example id="zend.db.table.row.extending.inflection.example">
  474. <title>Example of defining an inflection transformation</title>
  475. <para>
  476. This allows you to use an inflected version of the column name in the
  477. accessors. The Row class uses the <code>_transformColumn()</code> method to
  478. change the name you use to the native column name in the database table.
  479. </para>
  480. <programlisting language="php"><![CDATA[
  481. class MyInflectedRow extends Zend_Db_Table_Row_Abstract
  482. {
  483. protected function _transformColumn($columnName)
  484. {
  485. $nativeColumnName = myCustomInflector($columnName);
  486. return $nativeColumnName;
  487. }
  488. }
  489. class Bugs extends Zend_Db_Table_Abstract
  490. {
  491. protected $_name = 'bugs';
  492. protected $_rowClass = 'MyInflectedRow';
  493. }
  494. $bugs = new Bugs();
  495. $row = $bugs->fetchNew();
  496. // Use camelcase column names, and rely on the
  497. // transformation function to change it into the
  498. // native representation.
  499. $row->bugDescription = 'New description';
  500. ]]></programlisting>
  501. </example>
  502. <para>
  503. You are responsible for writing the functions to perform inflection transformation.
  504. Zend Framework does not provide such a function.
  505. </para>
  506. </sect3>
  507. </sect2>
  508. </sect1>
  509. <!--
  510. vim:se ts=4 sw=4 et:
  511. -->