Relation.php 6.81 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
doctrine's avatar
doctrine committed
21 22
/**
 * Doctrine_Relation
23
 * This class represents a relation between components
doctrine's avatar
doctrine committed
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
 *
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 */
class Doctrine_Relation {
    /**
     * RELATION CONSTANTS
     */

    /**
     * constant for ONE_TO_ONE and MANY_TO_ONE aggregate relationships
     */
    const ONE_AGGREGATE         = 0;
    /**
     * constant for ONE_TO_ONE and MANY_TO_ONE composite relationships
     */
    const ONE_COMPOSITE         = 1;
    /**
     * constant for MANY_TO_MANY and ONE_TO_MANY aggregate relationships
     */
    const MANY_AGGREGATE        = 2;
    /**
     * constant for MANY_TO_MANY and ONE_TO_MANY composite relationships
     */
    const MANY_COMPOSITE        = 3;
    

    /**
doctrine's avatar
doctrine committed
53
     * @var Doctrine_Table $table   foreign factory
doctrine's avatar
doctrine committed
54
     */
pookey's avatar
pookey committed
55
    protected $table;
doctrine's avatar
doctrine committed
56 57 58
    /**
     * @var string $local           local field
     */
pookey's avatar
pookey committed
59
    protected $local;
doctrine's avatar
doctrine committed
60 61 62
    /**
     * @var string $foreign         foreign field
     */
pookey's avatar
pookey committed
63
    protected $foreign;
doctrine's avatar
doctrine committed
64 65 66
    /**
     * @var integer $type           bind type
     */
pookey's avatar
pookey committed
67
    protected $type;
68 69 70
    /**
     * @var string $alias           relation alias
     */
pookey's avatar
pookey committed
71
    protected $alias;
doctrine's avatar
doctrine committed
72

doctrine's avatar
doctrine committed
73 74 75 76 77
    /**
     * @param Doctrine_Table $table
     * @param string $local
     * @param string $foreign
     * @param integer $type
doctrine's avatar
doctrine committed
78
     * @param string $alias
doctrine's avatar
doctrine committed
79
     */
doctrine's avatar
doctrine committed
80
    public function __construct(Doctrine_Table $table, $local, $foreign, $type, $alias) {
doctrine's avatar
doctrine committed
81 82 83 84
        $this->table    = $table;
        $this->local    = $local;
        $this->foreign  = $foreign;
        $this->type     = $type;
doctrine's avatar
doctrine committed
85
        $this->alias    = $alias;
doctrine's avatar
doctrine committed
86
    }
87 88 89
    /**
     * @return string                   the relation alias
     */
90
    final public function getAlias() {
91 92
        return $this->alias;
    }
doctrine's avatar
doctrine committed
93
    /**
doctrine's avatar
doctrine committed
94
     * @return integer                  the relation type, either 0 or 1
doctrine's avatar
doctrine committed
95
     */
96
    final public function getType() {
doctrine's avatar
doctrine committed
97 98 99 100 101
        return $this->type;
    }
    /**
     * @return object Doctrine_Table    foreign factory object
     */
102
    final public function getTable() {
doctrine's avatar
doctrine committed
103 104 105
        return $this->table;
    }
    /**
doctrine's avatar
doctrine committed
106
     * @return string                   the name of the local column
doctrine's avatar
doctrine committed
107
     */
108
    final public function getLocal() {
doctrine's avatar
doctrine committed
109 110 111
        return $this->local;
    }
    /**
doctrine's avatar
doctrine committed
112 113
     * @return string                   the name of the foreignkey column where
     *                                  the localkey column is pointing at
doctrine's avatar
doctrine committed
114
     */
115
    final public function getForeign() {
doctrine's avatar
doctrine committed
116 117
        return $this->foreign;
    }
pookey's avatar
pookey committed
118 119 120 121 122 123 124 125 126 127 128 129 130
    /** 
     * getRelationDql
     *
     * @param integer $count
     * @return string
     */
    public function getRelationDql($count) {
        $dql  = "FROM ".$this->table->getComponentName().
                " WHERE ".$this->table->getComponentName(). '.' . $this->foreign.
                " IN (".substr(str_repeat("?, ", $count),0,-2).")";
        
        return $dql;
    }
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
    /**
     * getDeleteOperations
     *
     * get the records that need to be deleted in order to change the old collection
     * to the new one
     *
     * The algorithm here is very simple and definitely not
     * the fastest one, since we have to iterate through the collections twice.
     * the complexity of this algorithm is O(n^2)
     *
     * We iterate through the old collection and get the records
     * that do not exists in the new collection (Doctrine_Records that need to be deleted).
     */
    final public static function getDeleteOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
        $r = array();

        foreach($old as $k => $record) {
            $id = $record->getIncremented();

            if(empty($id))
                continue;

            $found = false;
            foreach($new as $k2 => $record2) {
                if($record2->getIncremented() === $record->getIncremented()) {
                    $found = true;
                    break;
                }
            }

            if( ! $found)  {
                $r[] = $record;
                unset($old[$k]);
            }
        }

        return $r;
    }
    /**
     * getInsertOperations
     *
     * get the records that need to be added in order to change the old collection
     * to the new one
     *
     * The algorithm here is very simple and definitely not
     * the fastest one, since we have to iterate through the collections twice.
     * the complexity of this algorithm is O(n^2)
     *
     * We iterate through the old collection and get the records
     * that exists only in the new collection (Doctrine_Records that need to be added).
     */
    final public static function getInsertOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
        $r = array();

        foreach($new as $k => $record) {
            $found = false;

            $id = $record->getIncremented();
            if( ! empty($id)) {
                foreach($old as $k2 => $record2) {
                    if($record2->getIncremented() === $record->getIncremented()) {
                        $found = true;
                        break;
                    }
                }
            }
            if( ! $found) {
                $old[] = $record;
                $r[] = $record;
            }
        }

        return $r;
    }
doctrine's avatar
doctrine committed
205 206
    /**
     * __toString
207 208
     *
     * @return string
doctrine's avatar
doctrine committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222
     */
    public function __toString() {
        $r[] = "<pre>";
        $r[] = "Class       : ".get_class($this);
        $r[] = "Component   : ".$this->table->getComponentName();
        $r[] = "Table       : ".$this->table->getTableName();
        $r[] = "Local key   : ".$this->local;
        $r[] = "Foreign key : ".$this->foreign;
        $r[] = "Type        : ".$this->type;
        $r[] = "</pre>";
        return implode("\n", $r);
    }
}

223