File: Zend_Validate-Db.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (244 lines) | stat: -rw-r--r-- 7,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect2 id="zend.validate.Db">
    <title>Db_RecordExists and Db_NoRecordExists</title>

    <para>
        <classname>Zend_Validate_Db_RecordExists</classname> and
        <classname>Zend_Validate_Db_NoRecordExists</classname> provide a means to test
        whether a record exists in a given table of a database, with a given
        value.
    </para>

    <sect3 id="zend.validate.set.db.options">
        <title>Supported options for Zend_Validate_Db_*</title>

        <para>
            The following options are supported for
            <classname>Zend_Validate_Db_NoRecordExists</classname> and
            <classname>Zend_Validate_Db_RecordExists</classname>:
        </para>

        <itemizedlist>
            <listitem>
                <para>
                    <emphasis><property>adapter</property></emphasis>: The database adapter which
                    will be used for the search.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><property>exclude</property></emphasis>: Sets records which will be
                    excluded from the search.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><property>field</property></emphasis>: The database field within this
                    table which will be searched for the record.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><property>schema</property></emphasis>: Sets the schema which will be
                    used for the search.
                </para>
            </listitem>

            <listitem>
                <para>
                    <emphasis><property>table</property></emphasis>: The table which will be
                    searched for the record.
                </para>
            </listitem>
        </itemizedlist>
    </sect3>

    <sect3 id="zend.validate.db.basic-usage">
        <title>Basic usage</title>

        <para>
            An example of basic usage of the validators:
        </para>

        <programlisting language="php"><![CDATA[
//Check that the email address exists in the database
$validator = new Zend_Validate_Db_RecordExists(
    array(
        'table' => 'users',
        'field' => 'emailaddress'
    )
);

if ($validator->isValid($emailaddress)) {
    // email address appears to be valid
} else {
    // email address is invalid; print the reasons
    foreach ($validator->getMessages() as $message) {
        echo "$message\n";
    }
}
]]></programlisting>

        <para>
            The above will test that a given email address is in the database
            table. If no record is found containing the value of
            <varname>$emailaddress</varname> in the specified column, then an error
            message is displayed.
        </para>

        <programlisting language="php"><![CDATA[
//Check that the username is not present in the database
$validator = new Zend_Validate_Db_NoRecordExists(
    array(
        'table' => 'users',
        'field' => 'username'
    )
);
if ($validator->isValid($username)) {
    // username appears to be valid
} else {
    // username is invalid; print the reason
    $messages = $validator->getMessages();
    foreach ($messages as $message) {
        echo "$message\n";
    }
}
]]></programlisting>

        <para>
            The above will test that a given username is not in the database
            table. If a record is found containing the value of
            <varname>$username</varname> in the specified column, then an error
            message is displayed.
        </para>
    </sect3>

    <sect3 id="zend.validate.db.excluding-records">
        <title>Excluding records</title>

        <para>
            <classname>Zend_Validate_Db_RecordExists</classname> and
            <classname>Zend_Validate_Db_NoRecordExists</classname> also provide a means
            to test the database, excluding a part of the table, either by
            providing a where clause as a string, or an array with the keys
            "field" and "value".
        </para>

        <para>
            When providing an array for the exclude clause, the <emphasis>!=</emphasis>
            operator is used, so you can check the rest of a table for a value
            before altering a record (for example on a user profile form)
        </para>

        <programlisting language="php"><![CDATA[
//Check no other users have the username
$user_id   = $user->getId();
$validator = new Zend_Validate_Db_NoRecordExists(
    array(
        'table' => 'users',
        'field' => 'username',
        'exclude' => array(
            'field' => 'id',
            'value' => $user_id
        )
    )
);

if ($validator->isValid($username)) {
    // username appears to be valid
} else {
    // username is invalid; print the reason
    $messages = $validator->getMessages();
    foreach ($messages as $message) {
        echo "$message\n";
    }
}
]]></programlisting>

        <para>
            The above example will check the table to ensure no records other
            than the one where <command>id = $user_id</command> contains the value
            $username.
        </para>

        <para>
            You can also provide a string to the exclude clause so you can use
            an operator other than <emphasis>!=</emphasis>. This can be useful for
            testing against composite keys.
        </para>

        <programlisting language="php"><![CDATA[
$email     = 'user@example.com';
$clause    = $db->quoteInto('email = ?', $email);
$validator = new Zend_Validate_Db_RecordExists(
    array(
        'table'   => 'users',
        'field'   => 'username',
        'exclude' => $clause
    )
);

if ($validator->isValid($username)) {
    // username appears to be valid
} else {
    // username is invalid; print the reason
    $messages = $validator->getMessages();
    foreach ($messages as $message) {
        echo "$message\n";
    }
}
]]></programlisting>

        <para>
            The above example will check the 'users' table
            to ensure that only a record with both the username
            <varname>$username</varname> and with the email
            <varname>$email</varname> is valid.
        </para>
    </sect3>

    <sect3 id="zend.validate.db.database-adapters">
        <title>Database Adapters</title>

        <para>
            You can also specify an adapter. This will allow you to work with
            applications using multiple database adapters, or where you have not
            set a default adapter. As in the example below:
        </para>

        <programlisting language="php"><![CDATA[
$validator = new Zend_Validate_Db_RecordExists(
    array(
        'table' => 'users',
        'field' => 'id',
        'adapter' => $dbAdapter
    )
);
]]></programlisting>
    </sect3>

    <sect3 id="zend.validate.db.database-schemas">
        <title>Database Schemas</title>

        <para>
            You can specify a schema within your database for adapters such as
            PostgreSQL and DB/2 by simply supplying an array with
            <property>table</property> and <property>schema</property> keys. As in the example
            below:
        </para>

        <programlisting language="php"><![CDATA[
$validator = new Zend_Validate_Db_RecordExists(
    array(
        'table'  => 'users',
        'schema' => 'my',
        'field'  => 'id'
    )
);
]]></programlisting>
    </sect3>
</sect2>