Relation.php 8.07 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
 *
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 */
zYne's avatar
zYne committed
29
abstract class Doctrine_Relation {
doctrine's avatar
doctrine committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    /**
     * 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
    /**
zYne's avatar
zYne committed
88 89 90 91
     * getAlias
     * returns the relation alias
     *
     * @return string
92
     */
93
    final public function getAlias() {
94 95
        return $this->alias;
    }
doctrine's avatar
doctrine committed
96
    /**
zYne's avatar
zYne committed
97 98 99 100 101
     * getType
     * returns the relation type, either 0 or 1
     *
     * @see Doctrine_Relation MANY_* and ONE_* constants
     * @return integer
doctrine's avatar
doctrine committed
102
     */
103
    final public function getType() {
doctrine's avatar
doctrine committed
104 105 106
        return $this->type;
    }
    /**
zYne's avatar
zYne committed
107 108 109 110
     * getTable
     * returns the foreign table object
     *
     * @return object Doctrine_Table
doctrine's avatar
doctrine committed
111
     */
112
    final public function getTable() {
doctrine's avatar
doctrine committed
113 114 115
        return $this->table;
    }
    /**
zYne's avatar
zYne committed
116 117 118 119
     * getLocal
     * returns the name of the local column
     *
     * @return string
doctrine's avatar
doctrine committed
120
     */
121
    final public function getLocal() {
doctrine's avatar
doctrine committed
122 123 124
        return $this->local;
    }
    /**
zYne's avatar
zYne committed
125 126 127 128 129
     * getForeign
     * returns the name of the foreignkey column where
     * the localkey column is pointing at
     *
     * @return string
doctrine's avatar
doctrine committed
130
     */
131
    final public function getForeign() {
doctrine's avatar
doctrine committed
132 133
        return $this->foreign;
    }
134 135 136 137 138 139 140 141 142 143
    /**
     * isComposite
     * returns whether or not this relation is a composite relation
     *
     * @return boolean
     */
    final public function isComposite() {
        return ($this->type == Doctrine_Relation::ONE_COMPOSITE ||
                $this->type == Doctrine_Relation::MANY_COMPOSITE);
    }
144 145 146 147 148 149 150 151 152 153
    /**
     * isOneToOne
     * returns whether or not this relation is a one-to-one relation
     *
     * @return boolean
     */
    final public function isOneToOne() {
        return ($this->type == Doctrine_Relation::ONE_AGGREGATE ||
                $this->type == Doctrine_Relation::ONE_COMPOSITE);
    }
pookey's avatar
pookey committed
154 155 156 157 158 159 160 161 162 163 164 165 166
    /** 
     * 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;
    }
167 168 169 170 171 172 173 174 175 176 177 178
    /**
     * 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).
zYne's avatar
zYne committed
179 180 181 182
     *
     * @param Doctrine_Collection $old
     * @param Doctrine_Collection $new
     * @return array
183
     */
zYne's avatar
zYne committed
184
    public static function getDeleteOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
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
        $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).
zYne's avatar
zYne committed
221 222 223 224
     *
     * @param Doctrine_Collection $old
     * @param Doctrine_Collection $new
     * @return array
225
     */
zYne's avatar
zYne committed
226
    public static function getInsertOperations(Doctrine_Collection $old, Doctrine_Collection $new) {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        $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;
    }
zYne's avatar
zYne committed
249 250 251 252 253 254 255 256 257
    /**
     * fetchRelatedFor
     *
     * fetches a component related to given record
     *
     * @param Doctrine_Record $record
     * @return Doctrine_Record|Doctrine_Collection
     */
    abstract public function fetchRelatedFor(Doctrine_Record $record);
doctrine's avatar
doctrine committed
258 259
    /**
     * __toString
260 261
     *
     * @return string
doctrine's avatar
doctrine committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275
     */
    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);
    }
}

276