Relation.php 9.41 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 26 27 28 29 30 31 32
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
lsmith's avatar
lsmith committed
33 34
abstract class Doctrine_Relation
{
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
    {
zYne's avatar
zYne committed
121 122 123
    	$def = array();
    	foreach ($this->definition as $key => $val) {
            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 129
            } else {
                $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
    /**
zYne's avatar
zYne committed
161 162 163 164 165 166 167
     * toArray
     *
     * @return array
     */
    public function toArray() 
    {
        return $this->definition;
doctrine's avatar
doctrine committed
168
    }
169
    /**
zYne's avatar
zYne committed
170 171 172 173
     * getAlias
     * returns the relation alias
     *
     * @return string
174
     */
lsmith's avatar
lsmith committed
175 176
    final public function getAlias()
    {
177
        return $this->definition['alias'];
178
    }
doctrine's avatar
doctrine committed
179
    /**
zYne's avatar
zYne committed
180 181 182 183 184
     * getType
     * returns the relation type, either 0 or 1
     *
     * @see Doctrine_Relation MANY_* and ONE_* constants
     * @return integer
doctrine's avatar
doctrine committed
185
     */
lsmith's avatar
lsmith committed
186 187
    final public function getType()
    {
188
        return $this->definition['type'];
doctrine's avatar
doctrine committed
189 190
    }
    /**
zYne's avatar
zYne committed
191 192 193 194
     * getTable
     * returns the foreign table object
     *
     * @return object Doctrine_Table
doctrine's avatar
doctrine committed
195
     */
lsmith's avatar
lsmith committed
196 197
    final public function getTable()
    {
zYne's avatar
zYne committed
198 199 200
        return Doctrine_Manager::getInstance()
               ->getConnectionForComponent($this->definition['class'])
               ->getTable($this->definition['class']);
doctrine's avatar
doctrine committed
201 202
    }
    /**
zYne's avatar
zYne committed
203 204 205 206
     * getLocal
     * returns the name of the local column
     *
     * @return string
doctrine's avatar
doctrine committed
207
     */
lsmith's avatar
lsmith committed
208 209
    final public function getLocal()
    {
210
        return $this->definition['local'];
doctrine's avatar
doctrine committed
211 212
    }
    /**
zYne's avatar
zYne committed
213 214 215 216 217
     * getForeign
     * returns the name of the foreignkey column where
     * the localkey column is pointing at
     *
     * @return string
doctrine's avatar
doctrine committed
218
     */
lsmith's avatar
lsmith committed
219 220
    final public function getForeign()
    {
221
        return $this->definition['foreign'];
doctrine's avatar
doctrine committed
222
    }
223 224 225 226 227 228
    /**
     * isComposite
     * returns whether or not this relation is a composite relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
229 230
    final public function isComposite()
    {
231 232
        return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
                $this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
233
    }
234 235 236 237 238 239
    /**
     * isOneToOne
     * returns whether or not this relation is a one-to-one relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
240 241
    final public function isOneToOne()
    {
242 243
        return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
                $this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
244
    }
lsmith's avatar
lsmith committed
245
    /**
pookey's avatar
pookey committed
246 247 248 249 250
     * getRelationDql
     *
     * @param integer $count
     * @return string
     */
lsmith's avatar
lsmith committed
251 252
    public function getRelationDql($count)
    {
zYne's avatar
zYne committed
253
    	$component = $this->getTable()->getComponentName();
254 255 256

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

pookey's avatar
pookey committed
259 260
        return $dql;
    }
zYne's avatar
zYne committed
261 262 263 264 265 266 267 268 269
    /**
     * 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
270 271
    /**
     * __toString
272 273
     *
     * @return string
doctrine's avatar
doctrine committed
274
     */
lsmith's avatar
lsmith committed
275 276
    public function __toString()
    {
doctrine's avatar
doctrine committed
277
        $r[] = "<pre>";
278 279 280 281 282 283
        foreach ($this->definition as $k => $v) {
            if(is_object($v)) {
                $v = 'Object(' . get_class($v) . ')';
            }
            $r[] = $k . ' : ' . $v;
        }
doctrine's avatar
doctrine committed
284 285 286 287
        $r[] = "</pre>";
        return implode("\n", $r);
    }
}