From c1280d31dc27768b20b90c047eaaea15beba01ea Mon Sep 17 00:00:00 2001 From: zYne Date: Wed, 18 Oct 2006 19:55:14 +0000 Subject: [PATCH] Refs #175, Removed $collections instance variable from Doctrine_Record --- lib/Doctrine/Query.php | 46 +++++++++++++++++++++++------- lib/Doctrine/Query/Orderby.php | 47 +++++++++++++++++++++++------- lib/Doctrine/Query/Part.php | 52 ++++++++++++++++++++++++++++------ lib/Doctrine/Query/Set.php | 34 ++++++++++++++++++++++ lib/Doctrine/Record.php | 50 ++------------------------------ 5 files changed, 153 insertions(+), 76 deletions(-) create mode 100644 lib/Doctrine/Query/Set.php diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 32f0d18f3..f2b060156 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -27,10 +27,21 @@ * @license LGPL */ class Doctrine_Query extends Doctrine_Hydrate implements Countable { + /** + * QUERY TYPE CONSTANTS + */ + + /** + * constant for SELECT queries + */ const SELECT = 0; - + /** + * constant for DELETE queries + */ const DELETE = 1; - + /** + * constant for UPDATE queries + */ const UPDATE = 2; /** * @param array $subqueryAliases the table aliases needed in some LIMIT subqueries @@ -52,7 +63,11 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { private $isDistinct = false; private $pendingFields = array(); - + /** + * @var integer $type the query type + * + * @see Doctrine_Query::* constants + */ protected $type = self::SELECT; /** @@ -297,7 +312,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { break; case 'update': $this->type = self::UPDATE; - break; + $name = 'from'; case 'from': $this->parts['from'] = array(); $this->parts['select'] = array(); @@ -359,6 +374,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { * @return boolean */ public function set($name, $value) { + /** if(isset($this->parts[$name])) { $method = "parse".ucwords($name); @@ -390,6 +406,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { return true; } return false; + */ + $class = new Doctrine_Query_Set($this); + $class->parse($name, $value); } /** * @return boolean @@ -451,8 +470,10 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $q .= implode(", ",$a); if($needsSubQuery) - $subquery = 'SELECT DISTINCT '.$table->getTableName().".".$table->getIdentifier(). - ' FROM '.$table->getTableName(); + $subquery = 'SELECT DISTINCT ' . $table->getTableName() + . '.' . $table->getIdentifier() + . ' FROM '.$table->getTableName(); + if( ! empty($this->parts['join'])) { foreach($this->parts['join'] as $part) { @@ -570,7 +591,9 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { $part = trim($part); switch(strtolower($part)) { case 'delete': + case 'update': case 'select': + case 'set': case 'from': case 'where': case 'limit': @@ -604,15 +627,18 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { case 'DELETE': $this->type = self::DELETE; break; - case 'UPDATE': - $this->type = self::UPDATE; - break; + case 'SELECT': $this->type = self::SELECT; $this->parseSelect($part); break; + case 'UPDATE': + $this->type = self::UPDATE; + $k = 'FROM'; + case 'FROM': - $class = "Doctrine_Query_".ucwords(strtolower($k)); + case 'SET': + $class = 'Doctrine_Query_'.ucwords(strtolower($k)); $parser = new $class($this); $parser->parse($part); break; diff --git a/lib/Doctrine/Query/Orderby.php b/lib/Doctrine/Query/Orderby.php index 579732242..842001500 100644 --- a/lib/Doctrine/Query/Orderby.php +++ b/lib/Doctrine/Query/Orderby.php @@ -1,6 +1,31 @@ . + */ +Doctrine::autoload('Doctrine_Query_Part'); +/** + * Doctrine_Query_Orderby + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ class Doctrine_Query_Orderby extends Doctrine_Query_Part { /** * DQL ORDER BY PARSER @@ -9,17 +34,17 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part { * @param string $str * @return void */ - final public function parse($str) { + public function parse($str) { $ret = array(); - foreach(explode(",",trim($str)) as $r) { + foreach(explode(',', trim($str)) as $r) { $r = trim($r); - $e = explode(" ",$r); - $a = explode(".",$e[0]); + $e = explode(' ', $r); + $a = explode('.', $e[0]); if(count($a) > 1) { $field = array_pop($a); - $reference = implode(".",$a); + $reference = implode('.', $a); $name = end($a); @@ -29,18 +54,18 @@ class Doctrine_Query_Orderby extends Doctrine_Query_Part { $tname = $this->query->getTable($alias)->getTableName(); - $r = $alias.".".$field; + $r = $alias . '.' . $field; if(isset($e[1])) - $r .= " ".$e[1]; + $r .= ' '.$e[1]; } $ret[] = $r; } - return implode(", ", $ret); + return implode(', ', $ret); } public function __toString() { - return ( ! empty($this->parts))?implode(", ", $this->parts):''; + return ( ! empty($this->parts))?implode(', ', $this->parts):''; } } diff --git a/lib/Doctrine/Query/Part.php b/lib/Doctrine/Query/Part.php index fcda65ba5..9d3e578f6 100644 --- a/lib/Doctrine/Query/Part.php +++ b/lib/Doctrine/Query/Part.php @@ -1,23 +1,59 @@ . + */ Doctrine::autoload("Doctrine_Access"); - +/** + * Doctrine_Query_Part + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ abstract class Doctrine_Query_Part extends Doctrine_Access { - + /** + * @var Doctrine_Query $query the query object associated with this parser + */ protected $query; - + /** + * @var string $name the name of this parser + */ protected $name; - + /** + * @var array $parts + */ protected $parts = array(); - + /** + * @param Doctrine_Query $query the query object associated with this parser + */ public function __construct(Doctrine_Query $query) { $this->query = $query; - } - + /** + * @return string $name the name of this parser + */ public function getName() { return $this->name; } - + /** + * @return Doctrine_Query $query the query object associated with this parser + */ public function getQuery() { return $this->query; } diff --git a/lib/Doctrine/Query/Set.php b/lib/Doctrine/Query/Set.php new file mode 100644 index 000000000..223f515ef --- /dev/null +++ b/lib/Doctrine/Query/Set.php @@ -0,0 +1,34 @@ +. + */ + +/** + * Doctrine_Query + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_Query_Set extends Doctrine_Query_Part { + public function parse($dql) { + $e = Doctrine_Query::sqlExplode($dql, '='); + } +} +?> diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index de5eb2646..e392226f2 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -95,10 +95,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @var Doctrine_Validator_ErrorStack error stack object */ protected $_errorStack; - /** - * @var array $collections the collections this record is in - */ - private $collections = array(); /** * @var array $references an array containing all the references */ @@ -494,38 +490,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this); } - - - /** - * addCollection - * - * @param Doctrine_Collection $collection - * @param mixed $key - */ - final public function addCollection(Doctrine_Collection $collection,$key = null) { - if($key !== null) { - $this->collections[$key] = $collection; - } else { - $this->collections[] = $collection; - } - } - /** - * getCollection - * @param integer $key - * @return Doctrine_Collection - */ - final public function getCollection($key) { - return $this->collections[$key]; - } - /** - * hasCollections - * whether or not this record is part of a collection - * - * @return boolean - */ - final public function hasCollections() { - return (! empty($this->collections)); - } /** * getState * returns the current state of the object @@ -646,16 +610,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite public function load() { // only load the data from database if the Doctrine_Record is in proxy state if($this->_state == Doctrine_Record::STATE_PROXY) { - if( ! empty($this->collections)) { - // delegate the loading operation to collections in which this record resides - foreach($this->collections as $collection) { - $collection->load($this); + $this->refresh(); - } - } else { - - $this->refresh(); - } $this->_state = Doctrine_Record::STATE_CLEAN; return true; @@ -680,9 +636,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if(isset($this->_data[$lower])) { // check if the property is null (= it is the Doctrine_Null object located in self::$null) - if($this->_data[$lower] === self::$null) { + if($this->_data[$lower] === self::$null) $this->load(); - } + if($this->_data[$lower] === self::$null) $value = null;