Zend_Validate-Db.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.validate.Db">
  4. <title>Db_RecordExists and Db_NoRecordExists</title>
  5. <para>
  6. <classname>Zend_Validate_Db_RecordExists</classname> and
  7. <classname>Zend_Validate_Db_NoRecordExists</classname> provide a means to test
  8. whether a record exists in a given table of a database, with a given
  9. value.
  10. </para>
  11. <sect3 id="zend.validate.db.basic-usage">
  12. <title>Basic usage</title>
  13. <para>
  14. An example of basic usage of the validators:
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. //Check that the email address exists in the database
  18. $validator = new Zend_Validate_Db_RecordExists(
  19. array(
  20. 'table' => 'users',
  21. 'field' => 'emailaddress'
  22. )
  23. );
  24. if ($validator->isValid($emailaddress)) {
  25. // email address appears to be valid
  26. } else {
  27. // email address is invalid; print the reasons
  28. foreach ($validator->getMessages() as $message) {
  29. echo "$message\n";
  30. }
  31. }
  32. ]]></programlisting>
  33. <para>
  34. The above will test that a given email address is in the database
  35. table. If no record is found containing the value of
  36. <varname>$emailaddress</varname> in the specified column, then an error
  37. message is displayed.
  38. </para>
  39. <programlisting language="php"><![CDATA[
  40. //Check that the username is not present in the database
  41. $validator = new Zend_Validate_Db_NoRecordExists(
  42. array(
  43. 'table' => 'users',
  44. 'field' => 'username'
  45. )
  46. );
  47. if ($validator->isValid($username)) {
  48. // username appears to be valid
  49. } else {
  50. // username is invalid; print the reason
  51. $messages = $validator->getMessages();
  52. foreach ($messages as $message) {
  53. echo "$message\n";
  54. }
  55. }
  56. ]]></programlisting>
  57. <para>
  58. The above will test that a given username is not in the database
  59. table. If a record is found containing the value of
  60. <varname>$username</varname> in the specified column, then an error
  61. message is displayed.
  62. </para>
  63. </sect3>
  64. <sect3 id="zend.validate.db.excluding-records">
  65. <title>Excluding records</title>
  66. <para>
  67. <classname>Zend_Validate_Db_RecordExists</classname> and
  68. <classname>Zend_Validate_Db_NoRecordExists</classname> also provide a means
  69. to test the database, excluding a part of the table, either by
  70. providing a where clause as a string, or an array with the keys
  71. "field" and "value".
  72. </para>
  73. <para>
  74. When providing an array for the exclude clause, the <code>!=</code>
  75. operator is used, so you can check the rest of a table for a value
  76. before altering a record (for example on a user profile form)
  77. </para>
  78. <programlisting language="php"><![CDATA[
  79. //Check no other users have the username
  80. $user_id = $user->getId();
  81. $validator = new Zend_Validate_Db_NoRecordExists(
  82. array(
  83. 'table' => 'users',
  84. 'field' => 'username',
  85. 'exclude' => array(
  86. 'field' => 'id',
  87. 'value' => $user_id
  88. )
  89. )
  90. );
  91. if ($validator->isValid($username)) {
  92. // username appears to be valid
  93. } else {
  94. // username is invalid; print the reason
  95. $messages = $validator->getMessages();
  96. foreach ($messages as $message) {
  97. echo "$message\n";
  98. }
  99. }
  100. ]]></programlisting>
  101. <para>
  102. The above example will check the table to ensure no records other
  103. than the one where <code>id = $user_id</code> contains the value
  104. $username.
  105. </para>
  106. <para>
  107. You can also provide a string to the exclude clause so you can use
  108. an operator other than <code>!=</code>. This can be useful for
  109. testing against composite keys.
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. $post_id = $post->getId();
  113. $clause = $db->quoteInto('post_id = ?', $category_id);
  114. $validator = new Zend_Validate_Db_RecordExists(
  115. array(
  116. 'table' => 'posts_categories',
  117. 'field' => 'post_id',
  118. 'exclude' => $clause
  119. )
  120. );
  121. if ($validator->isValid($username)) {
  122. // username appears to be valid
  123. } else {
  124. // username is invalid; print the reason
  125. $messages = $validator->getMessages();
  126. foreach ($messages as $message) {
  127. echo "$message\n";
  128. }
  129. }
  130. ]]></programlisting>
  131. <para>
  132. The above example will check the <code>posts_categories</code> table
  133. to ensure that a record with the <code>post_id</code> has a value
  134. matching <varname>$category_id</varname>
  135. </para>
  136. </sect3>
  137. <sect3 id="zend.validate.db.database-adapters">
  138. <title>Database Adapters</title>
  139. <para>
  140. You can also specify an adapter, as the fourth parameter when
  141. instantiating your validator, this will allow you to work with
  142. applications using multiple database adapters, or where you have not
  143. set a default adapter. As in the example below:
  144. </para>
  145. <programlisting language="php"><![CDATA[
  146. $validator = new Zend_Validate_Db_RecordExists(
  147. array(
  148. 'table' => 'users',
  149. 'field' => 'id',
  150. 'adapter' => $dbAdapter
  151. )
  152. );
  153. ]]></programlisting>
  154. </sect3>
  155. <sect3 id="zend.validate.db.database-schemas">
  156. <title>Database Schemas</title>
  157. <para>
  158. You can specify a schema within your database for adapters such as
  159. PostgreSQL and DB/2 by simply supplying an array with
  160. <code>table</code> and <code>schema</code> keys. As in the example
  161. below:
  162. </para>
  163. <programlisting language="php"><![CDATA[
  164. $validator = new Zend_Validate_Db_RecordExists(
  165. array(
  166. 'table' => 'users',
  167. 'schema' => 'my',
  168. 'field' => 'id'
  169. )
  170. );
  171. ]]></programlisting>
  172. </sect3>
  173. </sect2>