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

zYne's avatar
zYne committed
131 132
        $this->definition = $def;
    }
133
    /**
zYne's avatar
zYne committed
134 135 136 137 138 139 140
     * toArray
     *
     * @return array
     */
    public function toArray() 
    {
        return $this->definition;
doctrine's avatar
doctrine committed
141
    }
zYne's avatar
zYne committed
142 143 144 145 146 147 148 149 150
    /**
     * hasConstraint
     *
     * @return boolean
     */
    public function hasConstraint()
    {
        return $this->definition['constraint'];
    }
151
    /**
zYne's avatar
zYne committed
152 153 154 155
     * getAlias
     * returns the relation alias
     *
     * @return string
156
     */
lsmith's avatar
lsmith committed
157 158
    final public function getAlias()
    {
159
        return $this->definition['alias'];
160
    }
doctrine's avatar
doctrine committed
161
    /**
zYne's avatar
zYne committed
162 163 164 165 166
     * getType
     * returns the relation type, either 0 or 1
     *
     * @see Doctrine_Relation MANY_* and ONE_* constants
     * @return integer
doctrine's avatar
doctrine committed
167
     */
lsmith's avatar
lsmith committed
168 169
    final public function getType()
    {
170
        return $this->definition['type'];
doctrine's avatar
doctrine committed
171 172
    }
    /**
zYne's avatar
zYne committed
173 174 175 176
     * getTable
     * returns the foreign table object
     *
     * @return object Doctrine_Table
doctrine's avatar
doctrine committed
177
     */
lsmith's avatar
lsmith committed
178 179
    final public function getTable()
    {
zYne's avatar
zYne committed
180 181 182
        return Doctrine_Manager::getInstance()
               ->getConnectionForComponent($this->definition['class'])
               ->getTable($this->definition['class']);
doctrine's avatar
doctrine committed
183 184
    }
    /**
zYne's avatar
zYne committed
185 186 187 188
     * getLocal
     * returns the name of the local column
     *
     * @return string
doctrine's avatar
doctrine committed
189
     */
lsmith's avatar
lsmith committed
190 191
    final public function getLocal()
    {
192
        return $this->definition['local'];
doctrine's avatar
doctrine committed
193 194
    }
    /**
zYne's avatar
zYne committed
195 196 197 198 199
     * getForeign
     * returns the name of the foreignkey column where
     * the localkey column is pointing at
     *
     * @return string
doctrine's avatar
doctrine committed
200
     */
lsmith's avatar
lsmith committed
201 202
    final public function getForeign()
    {
203
        return $this->definition['foreign'];
doctrine's avatar
doctrine committed
204
    }
205 206 207 208 209 210
    /**
     * isComposite
     * returns whether or not this relation is a composite relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
211 212
    final public function isComposite()
    {
213 214
        return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
                $this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
215
    }
216 217 218 219 220 221
    /**
     * isOneToOne
     * returns whether or not this relation is a one-to-one relation
     *
     * @return boolean
     */
lsmith's avatar
lsmith committed
222 223
    final public function isOneToOne()
    {
224 225
        return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
                $this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
226
    }
lsmith's avatar
lsmith committed
227
    /**
pookey's avatar
pookey committed
228 229 230 231 232
     * getRelationDql
     *
     * @param integer $count
     * @return string
     */
lsmith's avatar
lsmith committed
233 234
    public function getRelationDql($count)
    {
zYne's avatar
zYne committed
235
    	$component = $this->getTable()->getComponentName();
236 237 238

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

pookey's avatar
pookey committed
241 242
        return $dql;
    }
zYne's avatar
zYne committed
243 244 245 246 247 248 249 250 251
    /**
     * 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
252 253
    /**
     * __toString
254 255
     *
     * @return string
doctrine's avatar
doctrine committed
256
     */
lsmith's avatar
lsmith committed
257 258
    public function __toString()
    {
doctrine's avatar
doctrine committed
259
        $r[] = "<pre>";
260 261 262 263 264 265
        foreach ($this->definition as $k => $v) {
            if(is_object($v)) {
                $v = 'Object(' . get_class($v) . ')';
            }
            $r[] = $k . ' : ' . $v;
        }
doctrine's avatar
doctrine committed
266 267 268 269
        $r[] = "</pre>";
        return implode("\n", $r);
    }
}