Zend_Validate-Db.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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('users', 'emailaddress');
  19. if ($validator->isValid($emailaddress)) {
  20. // email address appears to be valid
  21. } else {
  22. // email address is invalid; print the reasons
  23. foreach ($validator->getMessages() as $message) {
  24. echo "$message\n";
  25. }
  26. }
  27. ]]></programlisting>
  28. <para>
  29. The above will test that a given email address is in the database
  30. table. If no record is found containing the value of
  31. <code>$emailaddress</code> in the specified column, then an error
  32. message is displayed.
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. //Check that the username is not present in the database
  36. $validator = new Zend_Validate_Db_NoRecordExists('users', 'username');
  37. if ($validator->isValid($username)) {
  38. // username appears to be valid
  39. } else {
  40. // username is invalid; print the reason
  41. $messages = $validator->getMessages();
  42. foreach ($messages as $message) {
  43. echo "$message\n";
  44. }
  45. }
  46. ]]></programlisting>
  47. <para>
  48. The above will test that a given username is not in the database
  49. table. If a record is found containing the value of
  50. <code>$username</code> in the specified column, then an error
  51. message is displayed.
  52. </para>
  53. </sect3>
  54. <sect3 id="zend.validate.db.excluding-records">
  55. <title>Excluding records</title>
  56. <para>
  57. <classname>Zend_Validate_Db_RecordExists</classname> and
  58. <classname>Zend_Validate_Db_NoRecordExists</classname> also provide a means
  59. to test the database, excluding a part of the table, either by
  60. providing a where clause as a string, or an array with the keys
  61. "field" and "value".
  62. </para>
  63. <para>
  64. When providing an array for the exclude clause, the <code>!=</code>
  65. operator is used, so you can check the rest of a table for a value
  66. before altering a record (for example on a user profile form)
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. //Check no other users have the username
  70. $user_id = $user->getId();
  71. $validator = new Zend_Validate_Db_NoRecordExists(
  72. 'users',
  73. 'username',
  74. array(
  75. 'field' => 'id',
  76. 'value' => $user_id
  77. )
  78. );
  79. if ($validator->isValid($username)) {
  80. // username appears to be valid
  81. } else {
  82. // username is invalid; print the reason
  83. $messages = $validator->getMessages();
  84. foreach ($messages as $message) {
  85. echo "$message\n";
  86. }
  87. }
  88. ]]></programlisting>
  89. <para>
  90. The above example will check the table to ensure no records other
  91. than the one where <code>id = $user_id</code> contains the value
  92. $username.
  93. </para>
  94. <para>
  95. You can also provide a string to the exclude clause so you can use
  96. an operator other than <code>!=</code>. This can be useful for
  97. testing against composite keys.
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. $post_id = $post->getId();
  101. $clause = $db->quoteInto('post_id = ?', $category_id);
  102. $validator = new Zend_Validate_Db_RecordExists(
  103. 'posts_categories',
  104. 'post_id',
  105. $clause
  106. );
  107. if ($validator->isValid($username)) {
  108. // username appears to be valid
  109. } else {
  110. // username is invalid; print the reason
  111. $messages = $validator->getMessages();
  112. foreach ($messages as $message) {
  113. echo "$message\n";
  114. }
  115. }
  116. ]]></programlisting>
  117. <para>
  118. The above example will check the <code>posts_categories</code> table
  119. to ensure that a record with the <code>post_id</code> has a value
  120. matching <code>$category_id</code>
  121. </para>
  122. </sect3>
  123. <sect3 id="zend.validate.db.database-adapters">
  124. <title>Database Adapters</title>
  125. <para>
  126. You can also specify an adapter, as the fourth parameter when
  127. instantiating your validator, this will allow you to work with
  128. applications using multiple database adapters, or where you have not
  129. set a default adapter. As in the example below:
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. $validator = new Zend_Validate_Db_RecordExists('users', 'id', null, $dbAdapter);
  133. ]]></programlisting>
  134. </sect3>
  135. </sect2>