From 9c34cb2937a39f0fa00629f42d7acac412895a54 Mon Sep 17 00:00:00 2001 From: doctrine Date: Mon, 7 Aug 2006 09:55:46 +0000 Subject: [PATCH] Started the building of Doctrine_ValueHolder --- Doctrine.php | 14 ++++++++ Doctrine/Filter.php | 14 ++++++++ Doctrine/Hydrate.php | 50 +++++++++++++++++++++++++-- Doctrine/Table.php | 10 +++--- Doctrine/ValueHolder.php | 64 +++++++++++++++++++++++++++++++++++ tests/FilterTestCase.php | 14 ++++++++ tests/UnitTestCase.php | 4 ++- tests/ValueHolderTestCase.php | 61 +++++++++++++++++++++++++++++++++ tests/run.php | 2 ++ 9 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 Doctrine/Filter.php create mode 100644 Doctrine/ValueHolder.php create mode 100644 tests/FilterTestCase.php create mode 100644 tests/ValueHolderTestCase.php diff --git a/Doctrine.php b/Doctrine.php index 610ae41cd..fbf9397f3 100644 --- a/Doctrine.php +++ b/Doctrine.php @@ -196,6 +196,20 @@ final class Doctrine { * mode for lazy offset fetching */ const FETCH_LAZY_OFFSET = 4; + /** + * RETURN CONSTANTS + */ + + + /** + * RETURN VALUEHOLDER + */ + const RETURN_VHOLDER = 1; + /** + * RETURN RECORD + */ + const RETURN_RECORD = 2; + /** * LOCKMODE CONSTANTS */ diff --git a/Doctrine/Filter.php b/Doctrine/Filter.php new file mode 100644 index 000000000..770b6d6bb --- /dev/null +++ b/Doctrine/Filter.php @@ -0,0 +1,14 @@ +name = $name; + } + + public function getName() { + return $this->name; + } +} +?> diff --git a/Doctrine/Hydrate.php b/Doctrine/Hydrate.php index a9867b1a9..ce38a00d3 100644 --- a/Doctrine/Hydrate.php +++ b/Doctrine/Hydrate.php @@ -92,6 +92,11 @@ class Doctrine_Hydrate extends Doctrine_Access { public function __construct(Doctrine_Session $session) { $this->session = $session; } + /** + * remove + * + * @param $name + */ public function remove($name) { if(isset($this->parts[$name])) { if($name == "limit" || $name == "offset") @@ -215,7 +220,7 @@ class Doctrine_Hydrate extends Doctrine_Access { * @param string $params * @return Doctrine_Collection the root collection */ - public function execute($params = array()) { + public function execute($params = array(), $return = Doctrine::RETURN_RECORD) { $this->data = array(); $this->collections = array(); @@ -224,6 +229,8 @@ class Doctrine_Hydrate extends Doctrine_Access { else $query = $this->view->getSelectSql(); + + switch(count($this->tables)): case 0: throw new Doctrine_Exception("No tables selected"); @@ -262,6 +269,9 @@ class Doctrine_Hydrate extends Doctrine_Access { $array = $this->parseData($stmt); + if($return == Doctrine::RETURN_VHOLDER) { + return $this->hydrateHolders($array); + } $colls = array(); @@ -387,13 +397,47 @@ class Doctrine_Hydrate extends Doctrine_Access { return $coll; endswitch; } + /** + * hydrateHolders + * + * @param array $array + */ + public function hydrateHolders(array $array) { + $keys = array_keys($this->tables); + $root = $keys[0]; + $coll = new Doctrine_ValueHolder($this->tables[$root]); + + foreach($keys as $key) { + $prev[$key] = array(); + } + + foreach($array as $data) { + foreach($data as $alias => $row) { + if(isset($prev[$alias]) && $row !== $prev[$alias]) { + $holder = new Doctrine_ValueHolder($this->tables[$alias]); + $holder->data = $row; + + if($alias === $root) { + $coll->data[] = $holder; + } else { + $pointer = $this->joins[$alias]; + $component = $this->tables[$alias]->getComponentName(); + $last[$pointer]->data[$component][] = $holder; + } + $last[$alias] = $holder; + } + $prev[$alias] = $row; + } + } + return $coll; + } /** * applyInheritance - * applies column aggregation inheritance to DQL query + * applies column aggregation inheritance to DQL / SQL query * * @return string */ - final public function applyInheritance() { + public function applyInheritance() { // get the inheritance maps $array = array(); diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 2e3a4c6bb..9eaaca358 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -721,17 +721,17 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { return $users; } /** - * findBySql - * finds records with given sql where clause + * findByDql + * finds records with given DQL where clause * returns a collection of records * - * @param string $sql SQL after WHERE clause + * @param string $dql DQL after WHERE clause * @param array $params query parameters * @return Doctrine_Collection */ - public function findBySql($sql, array $params = array()) { + public function findBySql($dql, array $params = array()) { $q = new Doctrine_Query($this->session); - $users = $q->query("FROM ".$this->name." WHERE ".$sql, $params); + $users = $q->query("FROM ".$this->name." WHERE ".$dql, $params); return $users; } /** diff --git a/Doctrine/ValueHolder.php b/Doctrine/ValueHolder.php new file mode 100644 index 000000000..598060f01 --- /dev/null +++ b/Doctrine/ValueHolder.php @@ -0,0 +1,64 @@ +. + */ +Doctrine::autoload('Doctrine_Access'); +/** + * Doctrine_ValueHolder is a simple class representing a lightweight + * version of Doctrine_Record. It can be used when developer does not + * intend to save / delete the object. + * + * + * @package Doctrine ORM + * @url www.phpdoctrine.com + * @license LGPL + */ +class Doctrine_ValueHolder extends Doctrine_Access implements Countable { + + public $data = array(); + + private $table; + + public function __construct(Doctrine_Table $table) { + $this->table = $table; + } + + public function set($name, $value) { + $this->data[$name] = $value; + } + + public function get($name) { + if( ! isset($this->data[$name])) + throw new InvalidKeyException("Unknown property $name."); + + return $this->data[$name]; + } + public function count() { + return count($this->data); + } + + public function delete() { + throw new Doctrine_Exception("Method 'delete' not availible on Doctrine_ValueHolder."); + } + + public function save() { + throw new Doctrine_Exception("Method 'save' not availible on Doctrine_ValueHolder."); + } +} +?> diff --git a/tests/FilterTestCase.php b/tests/FilterTestCase.php new file mode 100644 index 000000000..0cfe57521 --- /dev/null +++ b/tests/FilterTestCase.php @@ -0,0 +1,14 @@ +tables = array("FilterTest","FilterTest2"); + } + public function testOperations() { + $t = new FilterTest; + } +} +?> diff --git a/tests/UnitTestCase.php b/tests/UnitTestCase.php index 986bc6db2..a12a9e793 100644 --- a/tests/UnitTestCase.php +++ b/tests/UnitTestCase.php @@ -22,6 +22,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { protected $listener; protected $cache; protected $users; + protected $valueHolder; protected $tables = array(); private $init = false; @@ -70,10 +71,11 @@ class Doctrine_UnitTestCase extends UnitTestCase { $this->listener = new Doctrine_EventListener_Debugger(); $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener); } - $this->query = new Doctrine_Query($this->session); $this->prepareTables(); $this->prepareData(); + + $this->valueHolder = new Doctrine_ValueHolder($this->session->getTable('User')); } public function prepareTables() { foreach($this->tables as $name) { diff --git a/tests/ValueHolderTestCase.php b/tests/ValueHolderTestCase.php new file mode 100644 index 000000000..81a73d881 --- /dev/null +++ b/tests/ValueHolderTestCase.php @@ -0,0 +1,61 @@ +valueHolder->data[0] = 'first'; + + $this->assertEqual($this->valueHolder->data[0], 'first'); + $this->assertEqual($this->valueHolder[0], 'first'); + $this->assertEqual($this->valueHolder->get(0), 'first'); + + $this->valueHolder->data['key'] = 'second'; + + $this->assertEqual($this->valueHolder->data['key'], 'second'); + $this->assertEqual($this->valueHolder->key, 'second'); + $this->assertEqual($this->valueHolder['key'], 'second'); + $this->assertEqual($this->valueHolder->get('key'), 'second'); + } + public function testSimpleQuery() { + $q = new Doctrine_Query($this->session); + $q->from("User"); + $users = $q->execute(array(), Doctrine::RETURN_VHOLDER); + $this->assertEqual($users->count(), 8); + + + } + public function testQueryWithOneToManyRelation() { + $q = new Doctrine_Query($this->session); + $q->from("User.Phonenumber"); + $users = $q->execute(array(), Doctrine::RETURN_VHOLDER); + $this->assertEqual($users->count(), 8); + $this->assertTrue($users[0] instanceof Doctrine_ValueHolder); + $this->assertTrue($users[3] instanceof Doctrine_ValueHolder); + $this->assertTrue($users[7] instanceof Doctrine_ValueHolder); + + $this->assertEqual(count($users[0]->Phonenumber), 1); + $this->assertEqual(count($users[1]->Phonenumber), 3); + $this->assertEqual(count($users[2]->Phonenumber), 1); + $this->assertEqual(count($users[3]->Phonenumber), 1); + $this->assertEqual(count($users[4]->Phonenumber), 3); + } + public function testDelete() { + $f = false; + try { + $this->valueHolder->delete(); + } catch(Doctrine_Exception $e) { + $f = true; + } + $this->assertTrue($f); + } + public function testSave() { + $f = false; + try { + $this->valueHolder->save(); + } catch(Doctrine_Exception $e) { + $f = true; + } + $this->assertTrue($f); + } +} +?> diff --git a/tests/run.php b/tests/run.php index 175bd9fe7..817d15a5a 100644 --- a/tests/run.php +++ b/tests/run.php @@ -22,6 +22,7 @@ require_once("ViewTestCase.php"); require_once("RawSqlTestCase.php"); require_once("CustomPrimaryKeyTestCase.php"); require_once("FilterTestCase.php"); +require_once("ValueHolderTestCase.php"); error_reporting(E_ALL); @@ -63,6 +64,7 @@ $test->addTestCase(new Doctrine_CustomPrimaryKeyTestCase()); $test->addTestCase(new Doctrine_Filter_TestCase()); +$test->addTestCase(new Doctrine_ValueHolder_TestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());