Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Unique.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine_Validator_Unique
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Validator
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
32
 */
33
class Doctrine_Validator_Unique extends Doctrine_Validator_Driver
34
{
35
    /**
36
     * checks if given value is unique
37
     *
38
     * @param mixed $value
39
     * @return boolean
40
     */
41
    public function validate($value)
42
    {
43
        if (is_null($value)) {
44
            return true;
45
        }
46
 
47
        $table = $this->invoker->getTable();
48
        $conn = $table->getConnection();
49
        $pks = $table->getIdentifierColumnNames();
50
 
51
        if (is_array($pks)) {
52
            for ($i = 0, $l = count($pks); $i < $l; $i++) {
53
                $pks[$i] = $conn->quoteIdentifier($pks[$i]);
54
            }
55
 
56
            $pks = implode(', ', $pks);
57
        }
58
 
59
        $sql = 'SELECT ' . $pks . ' FROM ' . $conn->quoteIdentifier($table->getTableName()) . ' WHERE ';
60
 
61
        if (is_array($this->field)) {
62
            foreach ($this->field as $k => $v) {
63
                $this->field[$k] = $conn->quoteIdentifier($table->getColumnName($v));
64
            }
65
 
66
            $sql .= implode(' = ? AND ', $this->field) . ' = ?';
67
            $values = $value;
68
        } else {
69
            $sql .= $conn->quoteIdentifier($table->getColumnName($this->field)) . ' = ?';
70
            $values = array();
71
            $values[] = $value;
72
        }
73
 
74
        // If the record is not new we need to add primary key checks because its ok if the
75
        // unique value already exists in the database IF the record in the database is the same
76
        // as the one that is validated here.
77
        $state = $this->invoker->state();
78
        if ( ! ($state == Doctrine_Record::STATE_TDIRTY || $state == Doctrine_Record::STATE_TCLEAN)) {
79
            foreach ((array) $table->getIdentifierColumnNames() as $pk) {
80
                $sql .= ' AND ' . $conn->quoteIdentifier($pk) . ' != ?';
81
                $pkFieldName = $table->getFieldName($pk);
82
                $values[] = $this->invoker->$pkFieldName;
83
            }
84
        }
85
 
86
        if (isset($this->args) && is_array($this->args) && isset($this->args['where'])) {
87
            $sql .= ' AND ' . $this->args['where'];
88
        }
89
 
90
        $stmt  = $table->getConnection()->getDbh()->prepare($sql);
91
        $stmt->execute($values);
92
 
93
        return ( ! is_array($stmt->fetch()));
94
    }
95
}