From 7c2dc1a978249244703668b45649093c02af4c63 Mon Sep 17 00:00:00 2001 From: zYne Date: Sun, 20 May 2007 19:28:21 +0000 Subject: [PATCH] --- lib/Doctrine/Relation/Parser.php | 49 +++++++++++++++--------------- tests/Relation/ParserTestCase.php | 50 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 tests/Relation/ParserTestCase.php diff --git a/lib/Doctrine/Relation/Parser.php b/lib/Doctrine/Relation/Parser.php index 0dd71bf3e..bbfbb77b6 100644 --- a/lib/Doctrine/Relation/Parser.php +++ b/lib/Doctrine/Relation/Parser.php @@ -38,15 +38,15 @@ class Doctrine_Relation_Parser /** * @var array $_relations an array containing all the Doctrine_Relation objects for this table */ - protected $_relations = array(); + protected $_relations = array(); /** - * @var array $_bound bound relations + * @var array $_pending relations waiting for parsing */ - protected $_bound = array(); + protected $_pending = array(); /** - * @var array $_boundAliases bound relation aliases + * @var array $_relationAliases relation aliases */ - protected $_boundAliases = array(); + protected $_aliases = array(); /** * constructor * @@ -63,9 +63,21 @@ class Doctrine_Relation_Parser */ public function getTable() { - return $this->_table; + return $this->_table; + } + /** + * getPendingRelation + * + * @return array an array defining a pending relation + */ + public function getPendingRelation($name) + { + if ( ! isset($this->_pending[$name])) { + throw new Doctrine_Relation_Exception('Unknown pending relation ' . $name); + } + + return $this->_pending[$name]; } - /** * binds a relation * @@ -73,7 +85,7 @@ class Doctrine_Relation_Parser * @param string $field * @return void */ - public function bind($name, $field, $type, $options = null) + public function bind($name, $options = array()) { if (isset($this->relations[$name])) { unset($this->relations[$name]); @@ -81,8 +93,8 @@ class Doctrine_Relation_Parser $lower = strtolower($name); - if (isset($this->columns[$lower])) { - throw new Doctrine_Table_Exception("Couldn't bind relation. Column with name " . $lower . ' already exists!'); + if ($this->_table->hasColumn($lower)) { + throw new Doctrine_Relation_Exception("Couldn't bind relation. Column with name " . $lower . ' already exists!'); } $e = explode(' as ', $name); @@ -90,25 +102,12 @@ class Doctrine_Relation_Parser if (isset($e[1])) { $alias = $e[1]; - $this->boundAliases[$name] = $alias; + $this->_aliases[$name] = $alias; } else { $alias = $name; } - $this->bound[$alias] = array('field' => $field, - 'type' => $type, - 'class' => $name, - 'alias' => $alias); - if ($options !== null) { - $opt = array(); - if (is_string($options)) { - $opt['local'] = $options; - } else { - $opt = (array) $options; - } - - $this->bound[$alias] = array_merge($this->bound[$alias], $opt); - } + $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); } /** * getRelation diff --git a/tests/Relation/ParserTestCase.php b/tests/Relation/ParserTestCase.php new file mode 100644 index 000000000..bf4d97687 --- /dev/null +++ b/tests/Relation/ParserTestCase.php @@ -0,0 +1,50 @@ +. + */ + +/** + * Doctrine_Relation_Parser_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Relation_Parser_TestCase extends Doctrine_UnitTestCase +{ + public function testPendingRelations() + { + $r = new Doctrine_Relation_Parser($this->conn->getTable('User')); + + $p = array('type' => Doctrine_Relation::ONE, + 'local' => 'email_id'); + + $r->bind('Email', $p); + + $this->assertEqual($r->getPendingRelation('Email'), array('type' => Doctrine_Relation::ONE, + 'local' => 'email_id', + 'class' => 'Email', + 'alias' => 'Email' + )); + } +}