3
* $Id: Relation.php 5095 2008-10-15 21:45:14Z jwage $
5
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
* This software consists of voluntary contributions made by many individuals
18
* and is licensed under the LGPL. For more information, see
19
* <http://www.phpdoctrine.org>.
24
* This class represents a relation between components
27
* @subpackage Relation
28
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29
* @link www.phpdoctrine.org
31
* @version $Revision: 5095 $
32
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
34
abstract class Doctrine_Relation implements ArrayAccess
41
* constant for ONE_TO_ONE and MANY_TO_ONE relationships
46
* constant for MANY_TO_MANY and ONE_TO_MANY relationships
50
// TRUE => mandatory, everything else is just a default value. this should be refactored
51
// since TRUE can bot be used as a default value this way. All values should be default values.
52
protected $definition = array('alias' => true,
67
'cascade' => array(), // application-level cascades
68
'owningSide' => false, // whether this is the owning side
69
'refClassRelationAlias' => null,
75
* @param array $definition an associative array with the following structure:
76
* name foreign key constraint name
78
* local the local field(s)
80
* foreign the foreign reference field(s)
82
* table the foreign table object
84
* localTable the local table object
86
* refTable the reference table object (if any)
88
* onDelete referential delete action
90
* onUpdate referential update action
92
* deferred deferred constraint checking
94
* alias relation alias
96
* type the relation type, either Doctrine_Relation::ONE or Doctrine_Relation::MANY
98
* constraint boolean value, true if the relation has an explicit referential integrity constraint
100
* The onDelete and onUpdate keys accept the following values:
102
* CASCADE: Delete or update the row from the parent table and automatically delete or
103
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
104
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
105
* in the parent table or in the child table.
107
* SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
108
* child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier
109
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
111
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
112
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
114
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
115
* omitting the ON DELETE or ON UPDATE clause.
119
public function __construct(array $definition)
122
foreach ($this->definition as $key => $val) {
123
if ( ! isset($definition[$key]) && $val) {
124
throw new Doctrine_Exception($key . ' is required!');
126
if (isset($definition[$key])) {
127
$def[$key] = $definition[$key];
129
$def[$key] = $this->definition[$key];
132
$this->definition = $def;
137
* whether or not this relation has an explicit constraint
141
public function hasConstraint()
143
return ($this->definition['constraint'] ||
144
($this->definition['onUpdate']) ||
145
($this->definition['onDelete']));
147
public function isDeferred()
149
return $this->definition['deferred'];
152
public function isDeferrable()
154
return $this->definition['deferrable'];
156
public function isEqual()
158
return $this->definition['equal'];
161
public function offsetExists($offset)
163
return isset($this->definition[$offset]);
166
public function offsetGet($offset)
168
if (isset($this->definition[$offset])) {
169
return $this->definition[$offset];
175
public function offsetSet($offset, $value)
177
if (isset($this->definition[$offset])) {
178
$this->definition[$offset] = $value;
182
public function offsetUnset($offset)
184
$this->definition[$offset] = false;
192
public function toArray()
194
return $this->definition;
199
* returns the relation alias
203
final public function getAlias()
205
return $this->definition['alias'];
210
* returns the relation type, either 0 or 1
212
* @see Doctrine_Relation MANY_* and ONE_* constants
215
final public function getType()
217
return $this->definition['type'];
221
* Checks whether this relation cascades deletions to the related objects
222
* on the application level.
226
public function isCascadeDelete()
228
return in_array('delete', $this->definition['cascade']);
233
* returns the foreign table object
235
* @return object Doctrine_Table
237
final public function getTable()
239
return Doctrine_Manager::getInstance()
240
->getConnectionForComponent($this->definition['class'])
241
->getTable($this->definition['class']);
246
* returns the name of the related class
248
* @return object Doctrine_Record
250
final public function getClass()
252
return $this->definition['class'];
257
* returns the name of the local column
261
final public function getLocal()
263
return $this->definition['local'];
268
* returns the field name of the local column
270
final public function getLocalFieldName()
272
return $this->definition['localTable']->getFieldName($this->definition['local']);
277
* returns the column name of the local column
279
* @return string $columnName
281
final public function getLocalColumnName()
283
return $this->definition['localTable']->getColumnName($this->definition['local']);
288
* returns the name of the foreignkey column where
289
* the localkey column is pointing at
293
final public function getForeign()
295
return $this->definition['foreign'];
300
* returns the field name of the foreign column
302
final public function getForeignFieldName()
304
return $this->definition['table']->getFieldName($this->definition['foreign']);
308
* getForeignColumnName
309
* returns the column name of the foreign column
311
* @return string $columnName
313
final public function getForeignColumnName()
315
return $this->definition['table']->getColumnName($this->definition['foreign']);
320
* returns whether or not this relation is a one-to-one relation
324
final public function isOneToOne()
326
return ($this->definition['type'] == Doctrine_Relation::ONE);
332
* @param integer $count
335
public function getRelationDql($count)
337
$component = $this->getTable()->getComponentName();
339
$dql = 'FROM ' . $component
340
. ' WHERE ' . $component . '.' . $this->definition['foreign']
341
. ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
349
* fetches a component related to given record
351
* @param Doctrine_Record $record
352
* @return Doctrine_Record|Doctrine_Collection
354
abstract public function fetchRelatedFor(Doctrine_Record $record);
361
public function __toString()
364
foreach ($this->definition as $k => $v) {
366
$v = 'Object(' . get_class($v) . ')';
368
$r[] = $k . ' : ' . $v;
371
return implode("\n", $r);
b'\\ No newline at end of file'