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>.
 */
21

doctrine's avatar
doctrine committed
22 23
/**
 * Doctrine_Relation
24
 * This class represents a relation between components
doctrine's avatar
doctrine committed
25
 *
lsmith's avatar
lsmith committed
26
 * @package     Doctrine
27
 * @subpackage  Relation
lsmith's avatar
lsmith committed
28 29 30 31 32 33
 * @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>
 */
34
abstract class Doctrine_Relation implements ArrayAccess
lsmith's avatar
lsmith committed
35
{
doctrine's avatar
doctrine committed
36 37 38 39 40 41 42 43
    /**
     * RELATION CONSTANTS
     */

    /**
     * constant for ONE_TO_ONE and MANY_TO_ONE aggregate relationships
     */
    const ONE_AGGREGATE         = 0;
44

doctrine's avatar
doctrine committed
45 46 47 48
    /**
     * constant for ONE_TO_ONE and MANY_TO_ONE composite relationships
     */
    const ONE_COMPOSITE         = 1;
49

doctrine's avatar
doctrine committed
50 51 52 53
    /**
     * constant for MANY_TO_MANY and ONE_TO_MANY aggregate relationships
     */
    const MANY_AGGREGATE        = 2;
54

doctrine's avatar
doctrine committed
55 56 57 58 59
    /**
     * constant for MANY_TO_MANY and ONE_TO_MANY composite relationships
     */
    const MANY_COMPOSITE        = 3;

60
    const ONE   = 0;
zYne's avatar
zYne committed
61
    const MANY  = 2;
62
    
zYne's avatar
zYne committed
63 64 65 66 67
    protected $definition = array('alias'       => true,
                                  'foreign'     => true,
                                  'local'       => true,
                                  'class'       => true,
                                  'type'        => true,
68
                                  'table'       => true,
zYne's avatar
zYne committed
69
                                  'name'        => false,
70
                                  'refTable'    => false,
zYne's avatar
zYne committed
71 72 73
                                  'onDelete'    => false,
                                  'onUpdate'    => false,
                                  'deferred'    => false,
zYne's avatar
zYne committed
74
                                  'deferrable'  => false,
zYne's avatar
zYne committed
75
                                  'constraint'  => false,
76
                                  'equal'       => false,
77
                                  );
78

doctrine's avatar
doctrine committed
79
    /**
80 81 82
     * constructor
     *
     * @param array $definition         an associative array with the following structure:
zYne's avatar
zYne committed
83
     *          name                    foreign key constraint name
84 85 86 87 88 89 90
     *
     *          local                   the local field(s)
     *
     *          foreign                 the foreign reference field(s)
     *
     *          table                   the foreign table object
     *
91
     *          refTable                the reference table object (if any)
92 93 94 95 96 97 98 99 100 101 102
     *
     *          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
103
     *          constraint              boolean value, true if the relation has an explicit referential integrity constraint
zYne's avatar
zYne committed
104
     *
105 106
     * The onDelete and onUpdate keys accept the following values:
     *
zYne's avatar
zYne committed
107
     * CASCADE: Delete or update the row from the parent table and automatically delete or
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
     *          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
123
     */
124
    public function __construct(array $definition)
lsmith's avatar
lsmith committed
125
    {
126 127
        $def = array();
        foreach ($this->definition as $key => $val) {
zYne's avatar
zYne committed
128
            if ( ! isset($definition[$key]) && $val) {
129 130
                throw new Doctrine_Exception($key . ' is required!');
            }
zYne's avatar
zYne committed
131 132
            if (isset($definition[$key])) {
                $def[$key] = $definition[$key];
zYne's avatar
zYne committed
133
            } else {
134
                $def[$key] = null;          
zYne's avatar
zYne committed
135
            }
136 137
        }

zYne's avatar
zYne committed
138 139
        $this->definition = $def;
    }
140

zYne's avatar
zYne committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    /**
     * 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'];
    }
162 163
    public function isEqual()
    {
zYne's avatar
zYne committed
164
        return $this->definition['equal'];
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

    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;
    }
192

193
    /**
zYne's avatar
zYne committed
194 195 196 197 198 199 200
     * toArray
     *
     * @return array
     */
    public function toArray() 
    {
        return $this->definition;
doctrine's avatar
doctrine committed
201
    }
202

203
    /**
zYne's avatar
zYne committed
204 205 206 207
     * getAlias
     * returns the relation alias
     *
     * @return string
208
     */
lsmith's avatar
lsmith committed
209 210
    final public function getAlias()
    {
211
        return $this->definition['alias'];
212
    }
213

doctrine's avatar
doctrine committed
214
    /**
zYne's avatar
zYne committed
215 216 217 218 219
     * getType
     * returns the relation type, either 0 or 1
     *
     * @see Doctrine_Relation MANY_* and ONE_* constants
     * @return integer
doctrine's avatar
doctrine committed
220
     */
lsmith's avatar
lsmith committed
221 222
    final public function getType()
    {
223
        return $this->definition['type'];
doctrine's avatar
doctrine committed
224
    }
225

doctrine's avatar
doctrine committed
226
    /**
zYne's avatar
zYne committed
227 228 229 230
     * getTable
     * returns the foreign table object
     *
     * @return object Doctrine_Table
doctrine's avatar
doctrine committed
231
     */
lsmith's avatar
lsmith committed
232 233
    final public function getTable()
    {
zYne's avatar
zYne committed
234 235 236
        return Doctrine_Manager::getInstance()
               ->getConnectionForComponent($this->definition['class'])
               ->getTable($this->definition['class']);
doctrine's avatar
doctrine committed
237
    }
238

doctrine's avatar
doctrine committed
239
    /**
zYne's avatar
zYne committed
240 241 242 243
     * getLocal
     * returns the name of the local column
     *
     * @return string
doctrine's avatar
doctrine committed
244
     */
lsmith's avatar
lsmith committed
245 246
    final public function getLocal()
    {
247
        return $this->definition['local'];
doctrine's avatar
doctrine committed
248
    }
249

doctrine's avatar
doctrine committed
250
    /**
zYne's avatar
zYne committed
251 252 253 254 255
     * getForeign
     * returns the name of the foreignkey column where
     * the localkey column is pointing at
     *
     * @return string
doctrine's avatar
doctrine committed
256
     */
lsmith's avatar
lsmith committed
257 258
    final public function getForeign()
    {
259
        return $this->definition['foreign'];
doctrine's avatar
doctrine committed
260
    }
261

262 263 264 265 266 267
    /**
     * isComposite
     * returns whether or not this relation is a composite relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
268 269
    final public function isComposite()
    {
270 271
        return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
                $this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
272
    }
273

274 275 276 277 278 279
    /**
     * isOneToOne
     * returns whether or not this relation is a one-to-one relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
280 281
    final public function isOneToOne()
    {
282 283
        return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
                $this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
284
    }
285

lsmith's avatar
lsmith committed
286
    /**
pookey's avatar
pookey committed
287 288 289 290 291
     * getRelationDql
     *
     * @param integer $count
     * @return string
     */
lsmith's avatar
lsmith committed
292 293
    public function getRelationDql($count)
    {
294
        $component = $this->getTable()->getComponentName();
295 296 297

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

pookey's avatar
pookey committed
300 301
        return $dql;
    }
302

zYne's avatar
zYne committed
303 304 305 306 307 308 309 310 311
    /**
     * fetchRelatedFor
     *
     * fetches a component related to given record
     *
     * @param Doctrine_Record $record
     * @return Doctrine_Record|Doctrine_Collection
     */
    abstract public function fetchRelatedFor(Doctrine_Record $record);
312

doctrine's avatar
doctrine committed
313 314
    /**
     * __toString
315 316
     *
     * @return string
doctrine's avatar
doctrine committed
317
     */
lsmith's avatar
lsmith committed
318 319
    public function __toString()
    {
doctrine's avatar
doctrine committed
320
        $r[] = "<pre>";
321
        foreach ($this->definition as $k => $v) {
322
            if (is_object($v)) {
323 324 325 326
                $v = 'Object(' . get_class($v) . ')';
            }
            $r[] = $k . ' : ' . $v;
        }
doctrine's avatar
doctrine committed
327 328 329
        $r[] = "</pre>";
        return implode("\n", $r);
    }
330
}