Zend_Db_Table_Row.xml 24 KB

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