1
0
mirror of synced 2024-12-14 07:06:04 +03:00
doctrine2/lib/Doctrine/Relation.php

269 lines
8.8 KiB
PHP
Raw Normal View History

2006-05-30 12:42:10 +04:00
<?php
2006-12-29 17:01:31 +03:00
/*
* $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>.
*/
2006-05-30 12:42:10 +04:00
/**
* Doctrine_Relation
* This class represents a relation between components
2006-05-30 12:42:10 +04:00
*
2006-12-29 17:01:31 +03:00
* @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>
*/
2006-12-29 17:40:47 +03:00
abstract class Doctrine_Relation
{
2006-05-30 12:42:10 +04:00
/**
* 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;
2007-02-17 01:54:59 +03:00
const ONE = 0;
2007-05-23 01:05:52 +04:00
const MANY = 2;
2007-02-17 01:54:59 +03:00
2007-02-17 15:38:02 +03:00
protected $definition = array('alias' => true,
'foreign' => true,
'local' => true,
'class' => true,
'type' => true,
'name' => false,
'refTable' => false,
2007-02-17 15:38:02 +03:00
'onDelete' => false,
'onUpdate' => false,
'deferred' => false,
2007-03-22 01:11:18 +03:00
'constraint' => false,
2007-02-17 01:54:59 +03:00
);
2006-05-30 12:42:10 +04:00
/**
2007-02-17 01:54:59 +03:00
* constructor
*
* @param array $definition an associative array with the following structure:
2007-02-17 15:38:02 +03:00
* name foreign key constraint name
2007-02-17 01:54:59 +03:00
*
* local the local field(s)
*
* foreign the foreign reference field(s)
*
* table the foreign table object
*
* refTable the reference table object (if any)
2007-02-17 01:54:59 +03:00
*
* 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
*
2007-03-22 01:11:18 +03:00
* constraint boolean value, true if the relation needs referential integrity constraint
*
2007-02-17 01:54:59 +03:00
* The onDelete and onUpdate keys accept the following values:
*
2007-02-28 01:54:27 +03:00
* CASCADE: Delete or update the row from the parent table and automatically delete or
2007-02-17 01:54:59 +03:00
* 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
2006-05-30 12:42:10 +04:00
*/
2007-02-17 01:54:59 +03:00
public function __construct(array $definition)
2006-12-29 17:40:47 +03:00
{
2007-02-17 15:38:02 +03:00
$def = array();
foreach ($this->definition as $key => $val) {
if ( ! isset($definition[$key]) && $val) {
2007-02-17 01:54:59 +03:00
throw new Doctrine_Exception($key . ' is required!');
}
2007-02-17 15:38:02 +03:00
if (isset($definition[$key])) {
$def[$key] = $definition[$key];
2007-03-22 01:11:18 +03:00
} else {
$def[$key] = null;
2007-02-17 15:38:02 +03:00
}
2007-02-17 01:54:59 +03:00
}
2007-02-17 15:38:02 +03:00
$this->definition = $def;
}
/**
* toArray
*
* @return array
*/
public function toArray()
{
return $this->definition;
2006-05-30 12:42:10 +04:00
}
2007-03-22 01:11:18 +03:00
/**
* hasConstraint
*
* @return boolean
*/
public function hasConstraint()
{
return $this->definition['constraint'];
}
2006-06-25 22:34:53 +04:00
/**
2006-09-21 01:43:40 +04:00
* getAlias
* returns the relation alias
*
* @return string
2006-06-25 22:34:53 +04:00
*/
2006-12-29 17:40:47 +03:00
final public function getAlias()
{
2007-02-17 01:54:59 +03:00
return $this->definition['alias'];
2006-06-25 22:34:53 +04:00
}
2006-05-30 12:42:10 +04:00
/**
2006-09-21 01:43:40 +04:00
* getType
* returns the relation type, either 0 or 1
*
* @see Doctrine_Relation MANY_* and ONE_* constants
* @return integer
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
final public function getType()
{
2007-02-17 01:54:59 +03:00
return $this->definition['type'];
2006-05-30 12:42:10 +04:00
}
/**
2006-09-21 01:43:40 +04:00
* getTable
* returns the foreign table object
*
* @return object Doctrine_Table
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
final public function getTable()
{
2007-02-28 01:54:27 +03:00
return Doctrine_Manager::getInstance()
->getConnectionForComponent($this->definition['class'])
->getTable($this->definition['class']);
2006-05-30 12:42:10 +04:00
}
/**
2006-09-21 01:43:40 +04:00
* getLocal
* returns the name of the local column
*
* @return string
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
final public function getLocal()
{
2007-02-17 01:54:59 +03:00
return $this->definition['local'];
2006-05-30 12:42:10 +04:00
}
/**
2006-09-21 01:43:40 +04:00
* getForeign
* returns the name of the foreignkey column where
* the localkey column is pointing at
*
* @return string
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
final public function getForeign()
{
2007-02-17 01:54:59 +03:00
return $this->definition['foreign'];
2006-05-30 12:42:10 +04:00
}
/**
* isComposite
* returns whether or not this relation is a composite relation
*
* @return boolean
*/
2006-12-29 17:40:47 +03:00
final public function isComposite()
{
2007-02-17 01:54:59 +03:00
return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
$this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
}
/**
* isOneToOne
* returns whether or not this relation is a one-to-one relation
*
* @return boolean
*/
2006-12-29 17:40:47 +03:00
final public function isOneToOne()
{
2007-02-17 01:54:59 +03:00
return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
$this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
}
2006-12-29 17:01:31 +03:00
/**
2006-08-16 01:33:18 +04:00
* getRelationDql
*
* @param integer $count
* @return string
*/
2006-12-29 17:40:47 +03:00
public function getRelationDql($count)
{
2007-02-17 15:38:02 +03:00
$component = $this->getTable()->getComponentName();
2007-02-17 01:54:59 +03:00
$dql = 'FROM ' . $component
. ' WHERE ' . $component . '.' . $this->definition['foreign']
2007-01-05 00:08:56 +03:00
. ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
2006-12-29 17:01:31 +03:00
2006-08-16 01:33:18 +04:00
return $dql;
}
2006-09-29 00:03:29 +04:00
/**
* fetchRelatedFor
*
* fetches a component related to given record
*
* @param Doctrine_Record $record
* @return Doctrine_Record|Doctrine_Collection
*/
abstract public function fetchRelatedFor(Doctrine_Record $record);
2006-05-30 12:42:10 +04:00
/**
* __toString
2006-07-22 03:22:15 +04:00
*
* @return string
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
public function __toString()
{
2006-05-30 12:42:10 +04:00
$r[] = "<pre>";
2007-02-17 01:54:59 +03:00
foreach ($this->definition as $k => $v) {
if(is_object($v)) {
$v = 'Object(' . get_class($v) . ')';
}
$r[] = $k . ' : ' . $v;
}
2006-05-30 12:42:10 +04:00
$r[] = "</pre>";
return implode("\n", $r);
}
}