Relation.php 10 KB
Newer Older
doctrine's avatar
doctrine committed
1
<?php
lsmith's avatar
lsmith committed
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
 *
lsmith's avatar
lsmith committed
25
 * @package     Doctrine
26
 * @subpackage  Relation
lsmith's avatar
lsmith committed
27 28 29 30 31 32
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
33
abstract class Doctrine_Relation implements ArrayAccess
lsmith's avatar
lsmith committed
34
{
doctrine's avatar
doctrine committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    /**
     * 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;

56
    const ONE   = 0;
zYne's avatar
zYne committed
57
    const MANY  = 2;
58
    
zYne's avatar
zYne committed
59 60 61 62 63
    protected $definition = array('alias'       => true,
                                  'foreign'     => true,
                                  'local'       => true,
                                  'class'       => true,
                                  'type'        => true,
64
                                  'table'       => true,
zYne's avatar
zYne committed
65
                                  'name'        => false,
66
                                  'refTable'    => false,
zYne's avatar
zYne committed
67 68 69
                                  'onDelete'    => false,
                                  'onUpdate'    => false,
                                  'deferred'    => false,
zYne's avatar
zYne committed
70
                                  'deferrable'  => false,
zYne's avatar
zYne committed
71
                                  'constraint'  => false,
72
                                  'equal'       => false,
73
                                  );
doctrine's avatar
doctrine committed
74
    /**
75 76 77
     * constructor
     *
     * @param array $definition         an associative array with the following structure:
zYne's avatar
zYne committed
78
     *          name                    foreign key constraint name
79 80 81 82 83 84 85
     *
     *          local                   the local field(s)
     *
     *          foreign                 the foreign reference field(s)
     *
     *          table                   the foreign table object
     *
86
     *          refTable                the reference table object (if any)
87 88 89 90 91 92 93 94 95 96 97
     *
     *          onDelete                referential delete action
     *  
     *          onUpdate                referential update action
     *
     *          deferred                deferred constraint checking 
     *
     *          alias                   relation alias
     *
     *          type                    the relation type, either Doctrine_Relation::ONE or Doctrine_Relation::MANY
     *
zYne's avatar
zYne committed
98
     *          constraint              boolean value, true if the relation has an explicit referential integrity constraint
zYne's avatar
zYne committed
99
     *
100 101
     * The onDelete and onUpdate keys accept the following values:
     *
zYne's avatar
zYne committed
102
     * CASCADE: Delete or update the row from the parent table and automatically delete or
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
     *          update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
     *          Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
     *          in the parent table or in the child table.
     *
     * SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
     *          child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier 
     *          specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
     *
     * NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary 
     *           key value is not allowed to proceed if there is a related foreign key value in the referenced table.
     *
     * RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
     *           omitting the ON DELETE or ON UPDATE clause.
     *
     * SET DEFAULT
doctrine's avatar
doctrine committed
118
     */
119
    public function __construct(array $definition)
lsmith's avatar
lsmith committed
120
    {
121 122
        $def = array();
        foreach ($this->definition as $key => $val) {
zYne's avatar
zYne committed
123
            if ( ! isset($definition[$key]) && $val) {
124 125
                throw new Doctrine_Exception($key . ' is required!');
            }
zYne's avatar
zYne committed
126 127
            if (isset($definition[$key])) {
                $def[$key] = $definition[$key];
zYne's avatar
zYne committed
128
            } else {
129
                $def[$key] = null;          
zYne's avatar
zYne committed
130
            }
131 132
        }

zYne's avatar
zYne committed
133 134
        $this->definition = $def;
    }
zYne's avatar
zYne committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    /**
     * hasConstraint
     * whether or not this relation has an explicit constraint
     *
     * @return boolean
     */
    public function hasConstraint()
    {
        return ($this->definition['constraint'] ||
                ($this->definition['onUpdate']) ||
                ($this->definition['onDelete']));
    }
    public function isDeferred()
    {
        return $this->definition['deferred'];
    }

    public function isDeferrable()
    {
        return $this->definition['deferrable'];
    }
156 157
    public function isEqual()
    {
zYne's avatar
zYne committed
158
        return $this->definition['equal'];
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

    public function offsetExists($offset)
    {
        return isset($this->definition[$offset]);
    }

    public function offsetGet($offset)
    {
        if (isset($this->definition[$offset])) {
            return $this->definition[$offset];
        }
        
        return null;
    }

    public function offsetSet($offset, $value)
    {
        if (isset($this->definition[$offset])) {
            $this->definition[$offset] = $value;
        }
    }

    public function offsetUnset($offset)
    {
        $this->definition[$offset] = false;
    }
186
    /**
zYne's avatar
zYne committed
187 188 189 190 191 192 193
     * toArray
     *
     * @return array
     */
    public function toArray() 
    {
        return $this->definition;
doctrine's avatar
doctrine committed
194
    }
195
    /**
zYne's avatar
zYne committed
196 197 198 199
     * getAlias
     * returns the relation alias
     *
     * @return string
200
     */
lsmith's avatar
lsmith committed
201 202
    final public function getAlias()
    {
203
        return $this->definition['alias'];
204
    }
doctrine's avatar
doctrine committed
205
    /**
zYne's avatar
zYne committed
206 207 208 209 210
     * getType
     * returns the relation type, either 0 or 1
     *
     * @see Doctrine_Relation MANY_* and ONE_* constants
     * @return integer
doctrine's avatar
doctrine committed
211
     */
lsmith's avatar
lsmith committed
212 213
    final public function getType()
    {
214
        return $this->definition['type'];
doctrine's avatar
doctrine committed
215 216
    }
    /**
zYne's avatar
zYne committed
217 218 219 220
     * getTable
     * returns the foreign table object
     *
     * @return object Doctrine_Table
doctrine's avatar
doctrine committed
221
     */
lsmith's avatar
lsmith committed
222 223
    final public function getTable()
    {
zYne's avatar
zYne committed
224 225 226
        return Doctrine_Manager::getInstance()
               ->getConnectionForComponent($this->definition['class'])
               ->getTable($this->definition['class']);
doctrine's avatar
doctrine committed
227 228
    }
    /**
zYne's avatar
zYne committed
229 230 231 232
     * getLocal
     * returns the name of the local column
     *
     * @return string
doctrine's avatar
doctrine committed
233
     */
lsmith's avatar
lsmith committed
234 235
    final public function getLocal()
    {
236
        return $this->definition['local'];
doctrine's avatar
doctrine committed
237 238
    }
    /**
zYne's avatar
zYne committed
239 240 241 242 243
     * getForeign
     * returns the name of the foreignkey column where
     * the localkey column is pointing at
     *
     * @return string
doctrine's avatar
doctrine committed
244
     */
lsmith's avatar
lsmith committed
245 246
    final public function getForeign()
    {
247
        return $this->definition['foreign'];
doctrine's avatar
doctrine committed
248
    }
249 250 251 252 253 254
    /**
     * isComposite
     * returns whether or not this relation is a composite relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
255 256
    final public function isComposite()
    {
257 258
        return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
                $this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
259
    }
260 261 262 263 264 265
    /**
     * isOneToOne
     * returns whether or not this relation is a one-to-one relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
266 267
    final public function isOneToOne()
    {
268 269
        return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
                $this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
270
    }
lsmith's avatar
lsmith committed
271
    /**
pookey's avatar
pookey committed
272 273 274 275 276
     * getRelationDql
     *
     * @param integer $count
     * @return string
     */
lsmith's avatar
lsmith committed
277 278
    public function getRelationDql($count)
    {
279
        $component = $this->getTable()->getComponentName();
280 281 282

        $dql  = 'FROM ' . $component
              . ' WHERE ' . $component . '.' . $this->definition['foreign']
zYne's avatar
zYne committed
283
              . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
lsmith's avatar
lsmith committed
284

pookey's avatar
pookey committed
285 286
        return $dql;
    }
zYne's avatar
zYne committed
287 288 289 290 291 292 293 294 295
    /**
     * 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
296 297
    /**
     * __toString
298 299
     *
     * @return string
doctrine's avatar
doctrine committed
300
     */
lsmith's avatar
lsmith committed
301 302
    public function __toString()
    {
doctrine's avatar
doctrine committed
303
        $r[] = "<pre>";
304
        foreach ($this->definition as $k => $v) {
305
            if (is_object($v)) {
306 307 308 309
                $v = 'Object(' . get_class($v) . ')';
            }
            $r[] = $k . ' : ' . $v;
        }
doctrine's avatar
doctrine committed
310 311 312
        $r[] = "</pre>";
        return implode("\n", $r);
    }
313
}