Commit 9c34cb29 authored by doctrine's avatar doctrine

Started the building of Doctrine_ValueHolder

parent 8d6c72cd
...@@ -196,6 +196,20 @@ final class Doctrine { ...@@ -196,6 +196,20 @@ final class Doctrine {
* mode for lazy offset fetching * mode for lazy offset fetching
*/ */
const FETCH_LAZY_OFFSET = 4; const FETCH_LAZY_OFFSET = 4;
/**
* RETURN CONSTANTS
*/
/**
* RETURN VALUEHOLDER
*/
const RETURN_VHOLDER = 1;
/**
* RETURN RECORD
*/
const RETURN_RECORD = 2;
/** /**
* LOCKMODE CONSTANTS * LOCKMODE CONSTANTS
*/ */
......
<?php
class Doctrine_Filter {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
?>
...@@ -92,6 +92,11 @@ class Doctrine_Hydrate extends Doctrine_Access { ...@@ -92,6 +92,11 @@ class Doctrine_Hydrate extends Doctrine_Access {
public function __construct(Doctrine_Session $session) { public function __construct(Doctrine_Session $session) {
$this->session = $session; $this->session = $session;
} }
/**
* remove
*
* @param $name
*/
public function remove($name) { public function remove($name) {
if(isset($this->parts[$name])) { if(isset($this->parts[$name])) {
if($name == "limit" || $name == "offset") if($name == "limit" || $name == "offset")
...@@ -215,7 +220,7 @@ class Doctrine_Hydrate extends Doctrine_Access { ...@@ -215,7 +220,7 @@ class Doctrine_Hydrate extends Doctrine_Access {
* @param string $params * @param string $params
* @return Doctrine_Collection the root collection * @return Doctrine_Collection the root collection
*/ */
public function execute($params = array()) { public function execute($params = array(), $return = Doctrine::RETURN_RECORD) {
$this->data = array(); $this->data = array();
$this->collections = array(); $this->collections = array();
...@@ -224,6 +229,8 @@ class Doctrine_Hydrate extends Doctrine_Access { ...@@ -224,6 +229,8 @@ class Doctrine_Hydrate extends Doctrine_Access {
else else
$query = $this->view->getSelectSql(); $query = $this->view->getSelectSql();
switch(count($this->tables)): switch(count($this->tables)):
case 0: case 0:
throw new Doctrine_Exception("No tables selected"); throw new Doctrine_Exception("No tables selected");
...@@ -262,6 +269,9 @@ class Doctrine_Hydrate extends Doctrine_Access { ...@@ -262,6 +269,9 @@ class Doctrine_Hydrate extends Doctrine_Access {
$array = $this->parseData($stmt); $array = $this->parseData($stmt);
if($return == Doctrine::RETURN_VHOLDER) {
return $this->hydrateHolders($array);
}
$colls = array(); $colls = array();
...@@ -387,13 +397,47 @@ class Doctrine_Hydrate extends Doctrine_Access { ...@@ -387,13 +397,47 @@ class Doctrine_Hydrate extends Doctrine_Access {
return $coll; return $coll;
endswitch; 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 * applyInheritance
* applies column aggregation inheritance to DQL query * applies column aggregation inheritance to DQL / SQL query
* *
* @return string * @return string
*/ */
final public function applyInheritance() { public function applyInheritance() {
// get the inheritance maps // get the inheritance maps
$array = array(); $array = array();
......
...@@ -721,17 +721,17 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -721,17 +721,17 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
return $users; return $users;
} }
/** /**
* findBySql * findByDql
* finds records with given sql where clause * finds records with given DQL where clause
* returns a collection of records * returns a collection of records
* *
* @param string $sql SQL after WHERE clause * @param string $dql DQL after WHERE clause
* @param array $params query parameters * @param array $params query parameters
* @return Doctrine_Collection * @return Doctrine_Collection
*/ */
public function findBySql($sql, array $params = array()) { public function findBySql($dql, array $params = array()) {
$q = new Doctrine_Query($this->session); $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; return $users;
} }
/** /**
......
<?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_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.");
}
}
?>
<?php
require_once("UnitTestCase.php");
class Doctrine_Filter_TestCase extends Doctrine_UnitTestCase {
public function prepareData() { }
public function prepareTables() {
$this->tables = array("FilterTest","FilterTest2");
}
public function testOperations() {
$t = new FilterTest;
}
}
?>
...@@ -22,6 +22,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -22,6 +22,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
protected $listener; protected $listener;
protected $cache; protected $cache;
protected $users; protected $users;
protected $valueHolder;
protected $tables = array(); protected $tables = array();
private $init = false; private $init = false;
...@@ -70,10 +71,11 @@ class Doctrine_UnitTestCase extends UnitTestCase { ...@@ -70,10 +71,11 @@ class Doctrine_UnitTestCase extends UnitTestCase {
$this->listener = new Doctrine_EventListener_Debugger(); $this->listener = new Doctrine_EventListener_Debugger();
$this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener); $this->manager->setAttribute(Doctrine::ATTR_LISTENER, $this->listener);
} }
$this->query = new Doctrine_Query($this->session); $this->query = new Doctrine_Query($this->session);
$this->prepareTables(); $this->prepareTables();
$this->prepareData(); $this->prepareData();
$this->valueHolder = new Doctrine_ValueHolder($this->session->getTable('User'));
} }
public function prepareTables() { public function prepareTables() {
foreach($this->tables as $name) { foreach($this->tables as $name) {
......
<?php
require_once("UnitTestCase.php");
class Doctrine_ValueHolder_TestCase extends Doctrine_UnitTestCase {
public function testGetSet() {
$this->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);
}
}
?>
...@@ -22,6 +22,7 @@ require_once("ViewTestCase.php"); ...@@ -22,6 +22,7 @@ require_once("ViewTestCase.php");
require_once("RawSqlTestCase.php"); require_once("RawSqlTestCase.php");
require_once("CustomPrimaryKeyTestCase.php"); require_once("CustomPrimaryKeyTestCase.php");
require_once("FilterTestCase.php"); require_once("FilterTestCase.php");
require_once("ValueHolderTestCase.php");
error_reporting(E_ALL); error_reporting(E_ALL);
...@@ -63,6 +64,7 @@ $test->addTestCase(new Doctrine_CustomPrimaryKeyTestCase()); ...@@ -63,6 +64,7 @@ $test->addTestCase(new Doctrine_CustomPrimaryKeyTestCase());
$test->addTestCase(new Doctrine_Filter_TestCase()); $test->addTestCase(new Doctrine_Filter_TestCase());
$test->addTestCase(new Doctrine_ValueHolder_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
......
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