Zend_Validate-Db.xml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.set.db.options">
  12. <title>Supported options for Zend_Validate_Db_*</title>
  13. <para>
  14. The following options are supported for
  15. <classname>Zend_Validate_Db_NoRecordExists</classname> and
  16. <classname>Zend_Validate_Db_RecordExists</classname>:
  17. </para>
  18. <itemizedlist>
  19. <listitem>
  20. <para>
  21. <emphasis><property>adapter</property></emphasis>: The database adapter which
  22. will be used for the search.
  23. </para>
  24. </listitem>
  25. <listitem>
  26. <para>
  27. <emphasis><property>exclude</property></emphasis>: Sets records which will be
  28. excluded from the search.
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. <emphasis><property>field</property></emphasis>: The database field within this
  34. table which will be searched for the record.
  35. </para>
  36. </listitem>
  37. <listitem>
  38. <para>
  39. <emphasis><property>schema</property></emphasis>: Sets the schema which will be
  40. used for the search.
  41. </para>
  42. </listitem>
  43. <listitem>
  44. <para>
  45. <emphasis><property>table</property></emphasis>: The table which will be
  46. searched for the record.
  47. </para>
  48. </listitem>
  49. </itemizedlist>
  50. </sect3>
  51. <sect3 id="zend.validate.db.basic-usage">
  52. <title>Basic usage</title>
  53. <para>
  54. An example of basic usage of the validators:
  55. </para>
  56. <programlisting language="php"><![CDATA[
  57. //Check that the email address exists in the database
  58. $validator = new Zend_Validate_Db_RecordExists(
  59. array(
  60. 'table' => 'users',
  61. 'field' => 'emailaddress'
  62. )
  63. );
  64. if ($validator->isValid($emailaddress)) {
  65. // email address appears to be valid
  66. } else {
  67. // email address is invalid; print the reasons
  68. foreach ($validator->getMessages() as $message) {
  69. echo "$message\n";
  70. }
  71. }
  72. ]]></programlisting>
  73. <para>
  74. The above will test that a given email address is in the database
  75. table. If no record is found containing the value of
  76. <varname>$emailaddress</varname> in the specified column, then an error
  77. message is displayed.
  78. </para>
  79. <programlisting language="php"><![CDATA[
  80. //Check that the username is not present in the database
  81. $validator = new Zend_Validate_Db_NoRecordExists(
  82. array(
  83. 'table' => 'users',
  84. 'field' => 'username'
  85. )
  86. );
  87. if ($validator->isValid($username)) {
  88. // username appears to be valid
  89. } else {
  90. // username is invalid; print the reason
  91. $messages = $validator->getMessages();
  92. foreach ($messages as $message) {
  93. echo "$message\n";
  94. }
  95. }
  96. ]]></programlisting>
  97. <para>
  98. The above will test that a given username is not in the database
  99. table. If a record is found containing the value of
  100. <varname>$username</varname> in the specified column, then an error
  101. message is displayed.
  102. </para>
  103. </sect3>
  104. <sect3 id="zend.validate.db.excluding-records">
  105. <title>Excluding records</title>
  106. <para>
  107. <classname>Zend_Validate_Db_RecordExists</classname> and
  108. <classname>Zend_Validate_Db_NoRecordExists</classname> also provide a means
  109. to test the database, excluding a part of the table, either by
  110. providing a where clause as a string, or an array with the keys
  111. "field" and "value".
  112. </para>
  113. <para>
  114. When providing an array for the exclude clause, the <emphasis>!=</emphasis>
  115. operator is used, so you can check the rest of a table for a value
  116. before altering a record (for example on a user profile form)
  117. </para>
  118. <programlisting language="php"><![CDATA[
  119. //Check no other users have the username
  120. $user_id = $user->getId();
  121. $validator = new Zend_Validate_Db_NoRecordExists(
  122. array(
  123. 'table' => 'users',
  124. 'field' => 'username',
  125. 'exclude' => array(
  126. 'field' => 'id',
  127. 'value' => $user_id
  128. )
  129. )
  130. );
  131. if ($validator->isValid($username)) {
  132. // username appears to be valid
  133. } else {
  134. // username is invalid; print the reason
  135. $messages = $validator->getMessages();
  136. foreach ($messages as $message) {
  137. echo "$message\n";
  138. }
  139. }
  140. ]]></programlisting>
  141. <para>
  142. The above example will check the table to ensure no records other
  143. than the one where <command>id = $user_id</command> contains the value
  144. $username.
  145. </para>
  146. <para>
  147. You can also provide a string to the exclude clause so you can use
  148. an operator other than <emphasis>!=</emphasis>. This can be useful for
  149. testing against composite keys.
  150. </para>
  151. <programlisting language="php"><![CDATA[
  152. $email = 'user@example.com';
  153. $clause = $db->quoteInto('email = ?', $email);
  154. $validator = new Zend_Validate_Db_RecordExists(
  155. array(
  156. 'table' => 'users',
  157. 'field' => 'username',
  158. 'exclude' => $clause
  159. )
  160. );
  161. if ($validator->isValid($username)) {
  162. // username appears to be valid
  163. } else {
  164. // username is invalid; print the reason
  165. $messages = $validator->getMessages();
  166. foreach ($messages as $message) {
  167. echo "$message\n";
  168. }
  169. }
  170. ]]></programlisting>
  171. <para>
  172. The above example will check the 'users' table
  173. to ensure that only a record with both the username
  174. <varname>$username</varname> and with the email
  175. <varname>$email</varname> is valid.
  176. </para>
  177. </sect3>
  178. <sect3 id="zend.validate.db.database-adapters">
  179. <title>Database Adapters</title>
  180. <para>
  181. You can also specify an adapter. This will allow you to work with
  182. applications using multiple database adapters, or where you have not
  183. set a default adapter. As in the example below:
  184. </para>
  185. <programlisting language="php"><![CDATA[
  186. $validator = new Zend_Validate_Db_RecordExists(
  187. array(
  188. 'table' => 'users',
  189. 'field' => 'id',
  190. 'adapter' => $dbAdapter
  191. )
  192. );
  193. ]]></programlisting>
  194. </sect3>
  195. <sect3 id="zend.validate.db.database-schemas">
  196. <title>Database Schemas</title>
  197. <para>
  198. You can specify a schema within your database for adapters such as
  199. PostgreSQL and DB/2 by simply supplying an array with
  200. <property>table</property> and <property>schema</property> keys. As in the example
  201. below:
  202. </para>
  203. <programlisting language="php"><![CDATA[
  204. $validator = new Zend_Validate_Db_RecordExists(
  205. array(
  206. 'table' => 'users',
  207. 'schema' => 'my',
  208. 'field' => 'id'
  209. )
  210. );
  211. ]]></programlisting>
  212. </sect3>
  213. </sect2>