Commit ed62d636 authored by zYne's avatar zYne

Moved relation classes under Doctrine_Relation namespace, fixes #134

Ticket: 134
parent c8c1397f
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
/** /**
* Doctrine_Association this class takes care of association mapping * Doctrine_Relation_Association this class takes care of association mapping
* (= many-to-many relationships, where the relationship is handled with an additional relational table * (= many-to-many relationships, where the relationship is handled with an additional relational table
* which holds 2 foreign keys) * which holds 2 foreign keys)
* *
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
* @url www.phpdoctrine.com * @url www.phpdoctrine.com
* @license LGPL * @license LGPL
*/ */
class Doctrine_Association extends Doctrine_Relation { class Doctrine_Relation_Association extends Doctrine_Relation {
/** /**
* @var Doctrine_Table $associationTable * @var Doctrine_Table $associationTable
*/ */
......
...@@ -243,8 +243,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -243,8 +243,8 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$this->reference = $record; $this->reference = $record;
$this->relation = $relation; $this->relation = $relation;
if($relation instanceof Doctrine_ForeignKey || if($relation instanceof Doctrine_Relation_ForeignKey ||
$relation instanceof Doctrine_LocalKey) { $relation instanceof Doctrine_Relation_LocalKey) {
$this->reference_field = $relation->getForeign(); $this->reference_field = $relation->getForeign();
...@@ -257,7 +257,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -257,7 +257,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$record->set($this->reference_field, $this->reference, false); $record->set($this->reference_field, $this->reference, false);
} }
} }
} elseif($relation instanceof Doctrine_Association) { } elseif($relation instanceof Doctrine_Relation_Association) {
} }
} }
...@@ -315,7 +315,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -315,7 +315,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
endswitch; endswitch;
if(isset($this->relation)) { if(isset($this->relation)) {
if($this->relation instanceof Doctrine_ForeignKey) { if($this->relation instanceof Doctrine_Relation_ForeignKey) {
$params[] = $this->reference->getIncremented(); $params[] = $this->reference->getIncremented();
$where[] = $this->reference_field." = ?"; $where[] = $this->reference_field." = ?";
...@@ -331,7 +331,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -331,7 +331,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
} }
} elseif($this->relation instanceof Doctrine_Association) { } elseif($this->relation instanceof Doctrine_Relation_Association) {
$asf = $this->relation->getAssociationFactory(); $asf = $this->relation->getAssociationFactory();
$query = 'SELECT '.$foreign." FROM ".$asf->getTableName()." WHERE ".$local."=".$this->getIncremented(); $query = 'SELECT '.$foreign." FROM ".$asf->getTableName()." WHERE ".$local."=".$this->getIncremented();
...@@ -582,7 +582,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -582,7 +582,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$local = $rel->getLocal(); $local = $rel->getLocal();
$list = array(); $list = array();
if($rel instanceof Doctrine_LocalKey || $rel instanceof Doctrine_ForeignKey) { if($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) {
foreach($this->data as $record): foreach($this->data as $record):
$list[] = $record[$local]; $list[] = $record[$local];
endforeach; endforeach;
...@@ -613,7 +613,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -613,7 +613,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$foreign = $rel->getForeign(); $foreign = $rel->getForeign();
$local = $rel->getLocal(); $local = $rel->getLocal();
if($rel instanceof Doctrine_LocalKey) { if($rel instanceof Doctrine_Relation_LocalKey) {
foreach($this->data as $key => $record) { foreach($this->data as $key => $record) {
foreach($coll as $k => $related) { foreach($coll as $k => $related) {
if($related[$foreign] == $record[$local]) { if($related[$foreign] == $record[$local]) {
...@@ -621,7 +621,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -621,7 +621,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
} }
} }
} }
} elseif($rel instanceof Doctrine_ForeignKey) { } elseif($rel instanceof Doctrine_Relation_ForeignKey) {
foreach($this->data as $key => $record) { foreach($this->data as $key => $record) {
if($record->getState() == Doctrine_Record::STATE_TCLEAN || if($record->getState() == Doctrine_Record::STATE_TCLEAN ||
$record->getState() == Doctrine_Record::STATE_TDIRTY) $record->getState() == Doctrine_Record::STATE_TDIRTY)
...@@ -638,7 +638,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -638,7 +638,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$this->data[$key]->setRelated($name, $sub); $this->data[$key]->setRelated($name, $sub);
} }
} elseif($rel instanceof Doctrine_Association) { } elseif($rel instanceof Doctrine_Relation_Association) {
$identifier = $this->table->getIdentifier(); $identifier = $this->table->getIdentifier();
$asf = $rel->getAssociationFactory(); $asf = $rel->getAssociationFactory();
$name = $table->getComponentName(); $name = $table->getComponentName();
......
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
*/ */
/** /**
* Doctrine * Doctrine_Compiler
* the base class of Doctrine framework * This class can be used for compiling the entire Doctrine framework into a single file
* *
* @package Doctrine * @package Doctrine
* @author Konsta Vesterinen * @author Konsta Vesterinen
...@@ -104,7 +104,7 @@ class Doctrine_Compiler { ...@@ -104,7 +104,7 @@ class Doctrine_Compiler {
echo "Adding $file" . PHP_EOL; echo "Adding $file" . PHP_EOL;
if( ! file_exists($file)) if( ! file_exists($file))
throw new Doctrine_Exception("Couldn't compile $file. File $file does not exists."); throw new Doctrine_Compiler_Exception("Couldn't compile $file. File $file does not exists.");
Doctrine::autoload($class); Doctrine::autoload($class);
$refl = new ReflectionClass ( $class ); $refl = new ReflectionClass ( $class );
...@@ -129,11 +129,10 @@ class Doctrine_Compiler { ...@@ -129,11 +129,10 @@ class Doctrine_Compiler {
$fp = @fopen($target, 'w'); $fp = @fopen($target, 'w');
if ($fp === false) if ($fp === false)
throw new Doctrine_Exception("Couldn't write compiled data. Failed to open $target"); throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $target");
fwrite($fp, "<?php". fwrite($fp, "<?php".
" class InvalidKeyException extends Exception { }". " class InvalidKeyException extends Exception { }".
" class DQLException extends Exception { }".
implode('', $ret) implode('', $ret)
); );
fclose($fp); fclose($fp);
...@@ -141,7 +140,7 @@ class Doctrine_Compiler { ...@@ -141,7 +140,7 @@ class Doctrine_Compiler {
$stripped = php_strip_whitespace($target); $stripped = php_strip_whitespace($target);
$fp = @fopen($target, 'w'); $fp = @fopen($target, 'w');
if ($fp === false) if ($fp === false)
throw new Doctrine_Exception("Couldn't write compiled data. Failed to open $file"); throw new Doctrine_Compiler_Exception("Couldn't write compiled data. Failed to open $file");
fwrite($fp, $stripped); fwrite($fp, $stripped);
fclose($fp); fclose($fp);
} }
......
<?php
/*
* $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_Compiler_Exception
*
* @package Doctrine
* @author Konsta Vesterinen
* @license LGPL
*/
class Doctrine_Compiler_Exception extends Doctrine_Exception { }
...@@ -459,8 +459,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -459,8 +459,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$saveLater = array(); $saveLater = array();
foreach($record->getReferences() as $k=>$v) { foreach($record->getReferences() as $k=>$v) {
$fk = $record->getTable()->getRelation($k); $fk = $record->getTable()->getRelation($k);
if($fk instanceof Doctrine_ForeignKey || if($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
...@@ -482,7 +482,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -482,7 +482,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
break; break;
endswitch; endswitch;
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Relation_Association) {
$v->save(); $v->save();
} }
} }
...@@ -517,7 +517,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -517,7 +517,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$name = $table->getComponentName(); $name = $table->getComponentName();
$alias = $this->table->getAlias($name); $alias = $this->table->getAlias($name);
if($rel instanceof Doctrine_Association) { if($rel instanceof Doctrine_Relation_Association) {
switch($rel->getType()): switch($rel->getType()):
case Doctrine_Relation::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
break; break;
...@@ -552,8 +552,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -552,8 +552,8 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
break; break;
endswitch; endswitch;
} elseif($fk instanceof Doctrine_ForeignKey || } elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
......
...@@ -68,7 +68,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { ...@@ -68,7 +68,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
// group relations // group relations
foreach($rels as $key => $rel) { foreach($rels as $key => $rel) {
if($rel instanceof Doctrine_ForeignKey) { if($rel instanceof Doctrine_Relation_ForeignKey) {
unset($rels[$key]); unset($rels[$key]);
array_unshift($rels, $rel); array_unshift($rels, $rel);
} }
...@@ -83,7 +83,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { ...@@ -83,7 +83,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
if($name === $nm) if($name === $nm)
continue; continue;
if($rel instanceof Doctrine_ForeignKey) { if($rel instanceof Doctrine_Relation_ForeignKey) {
if($index2 !== false) { if($index2 !== false) {
if($index2 >= $index) if($index2 >= $index)
continue; continue;
...@@ -95,7 +95,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { ...@@ -95,7 +95,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
$tree[] = $name; $tree[] = $name;
} }
} elseif($rel instanceof Doctrine_LocalKey) { } elseif($rel instanceof Doctrine_Relation_LocalKey) {
if($index2 !== false) { if($index2 !== false) {
if($index2 <= $index) if($index2 <= $index)
continue; continue;
...@@ -106,7 +106,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable { ...@@ -106,7 +106,7 @@ class Doctrine_Connection_UnitOfWork implements IteratorAggregate, Countable {
array_unshift($tree,$name); array_unshift($tree,$name);
$index++; $index++;
} }
} elseif($rel instanceof Doctrine_Association) { } elseif($rel instanceof Doctrine_Relation_Association) {
$t = $rel->getAssociationFactory(); $t = $rel->getAssociationFactory();
$n = $t->getComponentName(); $n = $t->getComponentName();
......
...@@ -20,14 +20,14 @@ ...@@ -20,14 +20,14 @@
*/ */
Doctrine::autoload('Doctrine_Relation'); Doctrine::autoload('Doctrine_Relation');
/** /**
* Doctrine_ForeignKey * Doctrine_Relation_ForeignKey
* This class represents a foreign key relation * This class represents a foreign key relation
* *
* @author Konsta Vesterinen * @author Konsta Vesterinen
* @license LGPL * @license LGPL
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_ForeignKey extends Doctrine_Relation { class Doctrine_Relation_ForeignKey extends Doctrine_Relation {
/** /**
* fetchRelatedFor * fetchRelatedFor
* *
...@@ -35,7 +35,7 @@ class Doctrine_ForeignKey extends Doctrine_Relation { ...@@ -35,7 +35,7 @@ class Doctrine_ForeignKey extends Doctrine_Relation {
* *
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @return Doctrine_Record|Doctrine_Collection * @return Doctrine_Record|Doctrine_Collection
*/ */
public function fetchRelatedFor(Doctrine_Record $record) { public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->get($this->local); $id = $record->get($this->local);
......
...@@ -391,7 +391,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -391,7 +391,7 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
if($fk->isOneToOne()) { if($fk->isOneToOne()) {
// one-to-one relation // one-to-one relation
if($fk instanceof Doctrine_LocalKey) if($fk instanceof Doctrine_Relation_LocalKey)
$last->set($fk->getLocal(), $record->getIncremented(), false); $last->set($fk->getLocal(), $record->getIncremented(), false);
$last->set($fk->getAlias(), $record); $last->set($fk->getAlias(), $record);
......
...@@ -20,14 +20,14 @@ ...@@ -20,14 +20,14 @@
*/ */
Doctrine::autoload('Doctrine_Relation'); Doctrine::autoload('Doctrine_Relation');
/** /**
* Doctrine_LocalKey * Doctrine_Relation_LocalKey
* This class represents a local key relation * This class represents a local key relation
* *
* @author Konsta Vesterinen * @author Konsta Vesterinen
* @license LGPL * @license LGPL
* @package Doctrine * @package Doctrine
*/ */
class Doctrine_LocalKey extends Doctrine_Relation { class Doctrine_Relation_LocalKey extends Doctrine_Relation {
/** /**
* fetchRelatedFor * fetchRelatedFor
* *
......
...@@ -115,13 +115,22 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -115,13 +115,22 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
* *
* @return Doctrine_Manager * @return Doctrine_Manager
*/ */
final public static function getInstance() { public static function getInstance() {
static $instance; static $instance;
if( ! isset($instance)) if( ! isset($instance))
$instance = new self(); $instance = new self();
return $instance; return $instance;
} }
/**
* connection
* a short cut for Doctrine_Manager::getInstance()->openConnection($dbh);
*
* @return Doctrine_Connection
*/
public static function connection(PDO $dbh) {
return Doctrine_Manager::getInstance()->openConnection($dbh);
}
/** /**
* install * install
* *
......
...@@ -732,12 +732,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { ...@@ -732,12 +732,12 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
$this->needsSubquery = true; $this->needsSubquery = true;
} }
if($fk instanceof Doctrine_ForeignKey || if($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
$this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign(); $this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign();
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Relation_Association) {
$asf = $fk->getAssociationFactory(); $asf = $fk->getAssociationFactory();
$assocTableName = $asf->getTableName(); $assocTableName = $asf->getTableName();
......
...@@ -742,8 +742,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -742,8 +742,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$rel = $this->table->getRelation($name); $rel = $this->table->getRelation($name);
// one-to-many or one-to-one relation // one-to-many or one-to-one relation
if($rel instanceof Doctrine_ForeignKey || if($rel instanceof Doctrine_Relation_ForeignKey ||
$rel instanceof Doctrine_LocalKey) { $rel instanceof Doctrine_Relation_LocalKey) {
switch($rel->getType()) { switch($rel->getType()) {
case Doctrine_Relation::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
case Doctrine_Relation::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
...@@ -767,7 +767,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -767,7 +767,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
break; break;
} }
} elseif($rel instanceof Doctrine_Association) { } elseif($rel instanceof Doctrine_Relation_Association) {
// join table relation found // join table relation found
if( ! ($value instanceof Doctrine_Collection)) if( ! ($value instanceof Doctrine_Collection))
throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references."); throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
...@@ -979,11 +979,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -979,11 +979,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$name = $table->getComponentName(); $name = $table->getComponentName();
$alias = $this->table->getAlias($name); $alias = $this->table->getAlias($name);
if($fk instanceof Doctrine_Association) { if($fk instanceof Doctrine_Relation_Association) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Relation::MANY_COMPOSITE:
break;
case Doctrine_Relation::MANY_AGGREGATE: case Doctrine_Relation::MANY_AGGREGATE:
$asf = $fk->getAssociationFactory(); $asf = $fk->getAssociationFactory();
...@@ -1015,8 +1012,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1015,8 +1012,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
break; break;
endswitch; endswitch;
} elseif($fk instanceof Doctrine_ForeignKey || } elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
...@@ -1168,7 +1165,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1168,7 +1165,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return false; return false;
if( ! $connector->isOneToOne()) { if( ! $connector->isOneToOne()) {
if( ! ($connector instanceof Doctrine_Association)) if( ! ($connector instanceof Doctrine_Relation_Association))
$coll->setReference($this, $connector); $coll->setReference($this, $connector);
$this->references[$alias] = $coll; $this->references[$alias] = $coll;
...@@ -1216,7 +1213,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1216,7 +1213,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return void * @return void
*/ */
final public function loadReference($name) { final public function loadReference($name) {
$fk = $this->table->getRelation($name); $fk = $this->table->getRelation($name);
if($fk->isOneToOne()) { if($fk->isOneToOne()) {
......
<?php
/*
* $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_Relation_Association this class takes care of association mapping
* (= many-to-many relationships, where the relationship is handled with an additional relational table
* which holds 2 foreign keys)
*
*
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
*/
class Doctrine_Relation_Association extends Doctrine_Relation {
/**
* @var Doctrine_Table $associationTable
*/
protected $associationTable;
/**
* the constructor
* @param Doctrine_Table $table foreign factory object
* @param Doctrine_Table $associationTable factory which handles the association
* @param string $local local field name
* @param string $foreign foreign field name
* @param integer $type type of relation
* @see Doctrine_Table constants
*/
public function __construct(Doctrine_Table $table, Doctrine_Table $associationTable, $local, $foreign, $type, $alias) {
parent::__construct($table, $local, $foreign, $type, $alias);
$this->associationTable = $associationTable;
}
/**
* @return Doctrine_Table
*/
public function getAssociationFactory() {
return $this->associationTable;
}
/**
* getRelationDql
*
* @param integer $count
* @return string
*/
public function getRelationDql($count, $context = 'record') {
switch($context):
case "record":
$sub = "SELECT ".$this->foreign.
" FROM ".$this->associationTable->getTableName().
" WHERE ".$this->local.
" IN (".substr(str_repeat("?, ", $count),0,-2).")";
$dql = "FROM ".$this->table->getComponentName();
$dql .= ".".$this->associationTable->getComponentName();
$dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub)";
break;
case "collection":
$sub = substr(str_repeat("?, ", $count),0,-2);
$dql = "FROM ".$this->associationTable->getComponentName().".".$this->table->getComponentName();
$dql .= " WHERE ".$this->associationTable->getComponentName().".".$this->local." IN ($sub)";
endswitch;
return $dql;
}
/**
* fetchRelatedFor
*
* fetches a component related to given record
*
* @param Doctrine_Record $record
* @return Doctrine_Record|Doctrine_Collection
*/
public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->getIncremented();
if(empty($id))
$coll = new Doctrine_Collection($this->table);
else
$coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id));
return $coll;
}
}
<?php
/*
* $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>.
*/
class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association {
/**
* getRelationDql
*
* @param integer $count
* @return string
*/
public function getRelationDql($count, $context = 'record') {
switch($context):
case "record":
$sub = "SELECT ".$this->foreign.
" FROM ".$this->associationTable->getTableName().
" WHERE ".$this->local.
" = ?";
$sub2 = "SELECT ".$this->local.
" FROM ".$this->associationTable->getTableName().
" WHERE ".$this->foreign.
" = ?";
$dql = "FROM ".$this->table->getComponentName();
$dql .= ".".$this->associationTable->getComponentName();
$dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub)";
$dql .= " || ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub2)";
break;
case "collection":
$sub = substr(str_repeat("?, ", $count),0,-2);
$dql = "FROM ".$this->associationTable->getComponentName().".".$this->table->getComponentName();
$dql .= " WHERE ".$this->associationTable->getComponentName().".".$this->local." IN ($sub)";
endswitch;
return $dql;
}
public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->getIncremented();
$q = new Doctrine_RawSql();
$assocTable = $this->getAssociationFactory()->getTableName();
$tableName = $record->getTable()->getTableName();
$identifier = $record->getTable()->getIdentifier();
$sub = "SELECT ".$this->getForeign().
" FROM ".$assocTable.
" WHERE ".$this->getLocal().
" = ?";
$sub2 = "SELECT ".$this->getLocal().
" FROM ".$assocTable.
" WHERE ".$this->getForeign().
" = ?";
$q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}')
->from($tableName.' INNER JOIN '.$assocTable.' ON '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$this->getLocal().' OR '.
$tableName.'.'.$identifier.' = '.$assocTable.'.'.$this->getForeign()
)
->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '.
$tableName.'.'.$identifier.' IN ('.$sub2.')'
);
$q->addComponent($tableName, $record->getTable()->getComponentName());
$q->addComponent($assocTable, $record->getTable()->getComponentName(). '.' . $this->getAssociationFactory()->getComponentName());
return $q->execute(array($id, $id));
}
}
?>
<?php
/*
* $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::autoload('Doctrine_Relation');
/**
* Doctrine_Relation_ForeignKey
* This class represents a foreign key relation
*
* @author Konsta Vesterinen
* @license LGPL
* @package Doctrine
*/
class Doctrine_Relation_ForeignKey extends Doctrine_Relation {
/**
* fetchRelatedFor
*
* fetches a component related to given record
*
* @param Doctrine_Record $record
* @return Doctrine_Record|Doctrine_Collection
*/
public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->get($this->local);
if($this->isOneToOne()) {
if(empty($id)) {
$related = $this->table->create();
} else {
$dql = "FROM ".$this->table->getComponentName()." WHERE ".$this->table->getComponentName().".".$this->foreign." = ?";
$coll = $this->table->getConnection()->query($dql, array($id));
$related = $coll[0];
}
$related->set($this->foreign, $record, false);
} else {
if(empty($id)) {
$related = new Doctrine_Collection($this->table);
} else {
$query = $this->getRelationDql(1);
$related = $this->table->getConnection()->query($query, array($id));
}
$related->setReference($record, $this);
}
return $related;
}
}
<?php
/*
* $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::autoload('Doctrine_Relation');
/**
* Doctrine_Relation_LocalKey
* This class represents a local key relation
*
* @author Konsta Vesterinen
* @license LGPL
* @package Doctrine
*/
class Doctrine_Relation_LocalKey extends Doctrine_Relation {
/**
* fetchRelatedFor
*
* fetches a component related to given record
*
* @param Doctrine_Record $record
* @return Doctrine_Record|Doctrine_Collection
*/
public function fetchRelatedFor(Doctrine_Record $record) {
$id = $record->get($this->local);
if(empty($id))
$related = $this->table->create();
else {
if( ! ($related = $this->table->find($id)))
$related = $this->table->create();
}
$record->set($this->local, $related, false);
return $related;
}
}
...@@ -274,7 +274,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -274,7 +274,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
// group relations // group relations
foreach($rels as $key => $rel) { foreach($rels as $key => $rel) {
if($rel instanceof Doctrine_ForeignKey) { if($rel instanceof Doctrine_Relation_ForeignKey) {
unset($rels[$key]); unset($rels[$key]);
array_unshift($rels, $rel); array_unshift($rels, $rel);
} }
...@@ -289,7 +289,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -289,7 +289,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
if($name === $nm) if($name === $nm)
continue; continue;
if($rel instanceof Doctrine_ForeignKey) { if($rel instanceof Doctrine_Relation_ForeignKey) {
if($index2 !== false) { if($index2 !== false) {
if($index2 >= $index) if($index2 >= $index)
continue; continue;
...@@ -305,7 +305,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -305,7 +305,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
//print "$k -- adding $nm :$name...<br>"; //print "$k -- adding $nm :$name...<br>";
} }
} elseif($rel instanceof Doctrine_LocalKey) { } elseif($rel instanceof Doctrine_Relation_LocalKey) {
if($index2 !== false) { if($index2 !== false) {
if($index2 <= $index) if($index2 <= $index)
continue; continue;
...@@ -322,7 +322,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -322,7 +322,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
//print "$k -- pushing <b>$name</b> into 0...<br \>"; //print "$k -- pushing <b>$name</b> into 0...<br \>";
} }
} elseif($rel instanceof Doctrine_Association) { } elseif($rel instanceof Doctrine_Relation_Association) {
$t = $rel->getAssociationFactory(); $t = $rel->getAssociationFactory();
$n = $t->getComponentName(); $n = $t->getComponentName();
...@@ -715,8 +715,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -715,8 +715,8 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
$saveLater = array(); $saveLater = array();
foreach($record->getReferences() as $k=>$v) { foreach($record->getReferences() as $k=>$v) {
$fk = $record->getTable()->getRelation($k); $fk = $record->getTable()->getRelation($k);
if($fk instanceof Doctrine_ForeignKey || if($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()): switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE: case Doctrine_Relation::ONE_COMPOSITE:
case Doctrine_Relation::MANY_COMPOSITE: case Doctrine_Relation::MANY_COMPOSITE:
...@@ -744,7 +744,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab ...@@ -744,7 +744,7 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
} }
break; break;
endswitch; endswitch;
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Relation_Association) {
$v->save(); $v->save();
} }
} }
......
...@@ -609,7 +609,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -609,7 +609,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if( ! isset($local)) if( ! isset($local))
$local = $table->getIdentifier(); $local = $table->getIdentifier();
$relation = new Doctrine_LocalKey($table,$foreign,$local,$type, $alias); $relation = new Doctrine_Relation_LocalKey($table,$foreign,$local,$type, $alias);
} else } else
throw new Doctrine_Table_Exception("Only one-to-one relations are possible when local reference key is used."); throw new Doctrine_Table_Exception("Only one-to-one relations are possible when local reference key is used.");
...@@ -620,7 +620,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -620,7 +620,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$local = $this->identifier; $local = $this->identifier;
// ONE-TO-MANY or ONE-TO-ONE // ONE-TO-MANY or ONE-TO-ONE
$relation = new Doctrine_ForeignKey($table, $local, $foreign, $type, $alias); $relation = new Doctrine_Relation_ForeignKey($table, $local, $foreign, $type, $alias);
} else { } else {
// MANY-TO-MANY // MANY-TO-MANY
...@@ -651,9 +651,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -651,9 +651,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
if(count($fields) > 1) { if(count($fields) > 1) {
// SELF-REFERENCING THROUGH JOIN TABLE // SELF-REFERENCING THROUGH JOIN TABLE
$this->relations[$e2[0]] = new Doctrine_ForeignKey($associationTable,$local,$fields[0],Doctrine_Relation::MANY_COMPOSITE, $e2[0]); $this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($associationTable,$local,$fields[0],Doctrine_Relation::MANY_COMPOSITE, $e2[0]);
$relation = new Doctrine_Association_Self($table,$associationTable,$fields[0],$fields[1], $type, $alias); $relation = new Doctrine_Relation_Association_Self($table,$associationTable,$fields[0],$fields[1], $type, $alias);
} else { } else {
// auto initialize a new one-to-one relationship for association table // auto initialize a new one-to-one relationship for association table
...@@ -661,9 +661,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -661,9 +661,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
$associationTable->bind($table->getComponentName(), $associationTable->getComponentName(). '.' .$foreign, Doctrine_Relation::ONE_AGGREGATE, 'id'); $associationTable->bind($table->getComponentName(), $associationTable->getComponentName(). '.' .$foreign, Doctrine_Relation::ONE_AGGREGATE, 'id');
// NORMAL MANY-TO-MANY RELATIONSHIP // NORMAL MANY-TO-MANY RELATIONSHIP
$this->relations[$e2[0]] = new Doctrine_ForeignKey($associationTable,$local,$e2[1],Doctrine_Relation::MANY_COMPOSITE, $e2[0]); $this->relations[$e2[0]] = new Doctrine_Relation_ForeignKey($associationTable,$local,$e2[1],Doctrine_Relation::MANY_COMPOSITE, $e2[0]);
$relation = new Doctrine_Association($table, $associationTable, $e2[1], $foreign, $type, $alias); $relation = new Doctrine_Relation_Association($table, $associationTable, $e2[1], $foreign, $type, $alias);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment