1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/lib/Doctrine/Relation.php

291 lines
8.3 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;
/**
2006-06-22 00:02:40 +04:00
* @var Doctrine_Table $table foreign factory
2006-05-30 12:42:10 +04:00
*/
2006-08-16 01:33:18 +04:00
protected $table;
2006-05-30 12:42:10 +04:00
/**
* @var string $local local field
*/
2006-08-16 01:33:18 +04:00
protected $local;
2006-05-30 12:42:10 +04:00
/**
* @var string $foreign foreign field
*/
2006-08-16 01:33:18 +04:00
protected $foreign;
2006-05-30 12:42:10 +04:00
/**
* @var integer $type bind type
*/
2006-08-16 01:33:18 +04:00
protected $type;
2006-06-25 22:34:53 +04:00
/**
* @var string $alias relation alias
*/
2006-08-16 01:33:18 +04:00
protected $alias;
2006-06-22 00:02:40 +04:00
2006-05-30 12:42:10 +04:00
/**
* @param Doctrine_Table $table
* @param string $local
* @param string $foreign
* @param integer $type
2006-06-22 00:02:40 +04:00
* @param string $alias
2006-05-30 12:42:10 +04:00
*/
2006-12-29 17:40:47 +03:00
public function __construct(Doctrine_Table $table, $local, $foreign, $type, $alias)
{
2006-05-30 12:42:10 +04:00
$this->table = $table;
$this->local = $local;
$this->foreign = $foreign;
$this->type = $type;
2006-06-26 22:55:42 +04:00
$this->alias = $alias;
2006-05-30 12:42:10 +04:00
}
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()
{
2006-06-25 22:34:53 +04:00
return $this->alias;
}
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()
{
2006-05-30 12:42:10 +04:00
return $this->type;
}
/**
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()
{
2006-05-30 12:42:10 +04:00
return $this->table;
}
/**
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()
{
2006-05-30 12:42:10 +04:00
return $this->local;
}
/**
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()
{
2006-05-30 12:42:10 +04:00
return $this->foreign;
}
/**
* 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()
{
return ($this->type == Doctrine_Relation::ONE_COMPOSITE ||
$this->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()
{
return ($this->type == Doctrine_Relation::ONE_AGGREGATE ||
$this->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)
{
2006-08-16 01:33:18 +04:00
$dql = "FROM ".$this->table->getComponentName().
" WHERE ".$this->table->getComponentName(). '.' . $this->foreign.
" 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;
}
/**
* getDeleteOperations
*
* get the records that need to be deleted in order to change the old collection
* to the new one
*
* The algorithm here is very simple and definitely not
* the fastest one, since we have to iterate through the collections twice.
* the complexity of this algorithm is O(n^2)
*
* We iterate through the old collection and get the records
* that do not exists in the new collection (Doctrine_Records that need to be deleted).
2006-09-29 00:03:29 +04:00
*
* @param Doctrine_Collection $old
* @param Doctrine_Collection $new
* @return array
*/
2006-12-29 17:40:47 +03:00
public static function getDeleteOperations(Doctrine_Collection $old, Doctrine_Collection $new)
{
$r = array();
2006-12-29 17:01:31 +03:00
foreach ($old as $k => $record) {
$id = $record->getIncremented();
2006-12-29 17:01:31 +03:00
if (empty($id)) {
continue;
2006-12-29 17:01:31 +03:00
}
$found = false;
2006-12-29 17:01:31 +03:00
foreach ($new as $k2 => $record2) {
if ($record2->getIncremented() === $record->getIncremented()) {
$found = true;
break;
}
}
2006-12-29 17:01:31 +03:00
if ( ! $found) {
$r[] = $record;
unset($old[$k]);
}
}
return $r;
}
/**
* getInsertOperations
*
* get the records that need to be added in order to change the old collection
* to the new one
*
* The algorithm here is very simple and definitely not
* the fastest one, since we have to iterate through the collections twice.
* the complexity of this algorithm is O(n^2)
*
* We iterate through the old collection and get the records
* that exists only in the new collection (Doctrine_Records that need to be added).
2006-09-29 00:03:29 +04:00
*
* @param Doctrine_Collection $old
* @param Doctrine_Collection $new
* @return array
*/
2006-12-29 17:40:47 +03:00
public static function getInsertOperations(Doctrine_Collection $old, Doctrine_Collection $new)
{
$r = array();
2006-12-29 17:01:31 +03:00
foreach ($new as $k => $record) {
$found = false;
$id = $record->getIncremented();
2006-12-29 17:01:31 +03:00
if ( ! empty($id)) {
foreach ($old as $k2 => $record2) {
if ($record2->getIncremented() === $record->getIncremented()) {
$found = true;
break;
}
}
}
2006-12-29 17:01:31 +03:00
if ( ! $found) {
$old[] = $record;
$r[] = $record;
}
}
return $r;
}
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>";
$r[] = "Class : ".get_class($this);
$r[] = "Component : ".$this->table->getComponentName();
$r[] = "Table : ".$this->table->getTableName();
$r[] = "Local key : ".$this->local;
$r[] = "Foreign key : ".$this->foreign;
$r[] = "Type : ".$this->type;
$r[] = "</pre>";
return implode("\n", $r);
}
}