diff --git a/lib/Doctrine/Resource/Record.php b/lib/Doctrine/Resource/Record.php index b422e4177..5ae2e0e69 100644 --- a/lib/Doctrine/Resource/Record.php +++ b/lib/Doctrine/Resource/Record.php @@ -35,22 +35,13 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count { protected $_data = array(); protected $_model = null; - protected $_schema = null; + protected $_table = null; protected $_changes = array(); public function __construct($model, $loadRelations = true) { $this->_model = $model; - - $schema = $this->getConfig('schema'); - - if (isset($schema['schema'][$model]) && $schema['schema'][$model]) { - $this->_schema = $schema['schema'][$model]; - } - - if (isset($schema['relations'][$model]) && $schema['relations'][$model]) { - $this->_schema['relations'] = $schema['relations'][$model]; - } + $this->_table = Doctrine_Resource_Client::getInstance()->getTable($model); $this->initialize($loadRelations); } @@ -62,12 +53,8 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count public function initialize($loadRelations = true) { - if (!$this->_schema) { - return false; - } - - $schema = $this->_schema; - $relations = $this->_schema['relations']; + $schema = $this->getTable()->getSchema(); + $relations = $schema['relations']; if (isset($schema['columns'])) { $columns = $schema['columns']; @@ -126,9 +113,9 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count $array = array(); foreach ($this->_data as $key => $value) { - if ($this->hasRelation($key)) { + if ($this->getTable()->hasRelation($key)) { - $relation = $this->getRelation($key); + $relation = $this->getTable()->getRelation($key); if ($relation['type'] === Doctrine_Relation::ONE) { if ($this->_data[$key]->hasChanges()) { @@ -141,7 +128,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count } } } - } else if ($this->hasColumn($key)) { + } else if ($this->getTable()->hasColumn($key)) { if (isset($this->_changes[$key])) { $array[$key] = $value; } @@ -191,6 +178,11 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count $response = $request->execute(); } + public function getTable() + { + return $this->_table; + } + public function getModel() { return $this->_model; @@ -200,8 +192,11 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count { $identifier = array(); - if (isset($this->_schema['columns']) && is_array($this->_schema['columns'])) { - foreach ($this->_schema['columns'] as $name => $column) { + $schema = $this->getTable()->getSchema(); + $columns = $schema['columns']; + + if (isset($columns) && is_array($columns)) { + foreach ($columns as $name => $column) { if ($column['primary'] == true) { $identifier[$name] = $this->_data[$name]; } @@ -223,30 +218,6 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count return true; } - - public function hasColumn($name) - { - return isset($this->_schema['columns'][$name]) ? true:false; - } - - public function getColumn($name) - { - if ($this->hasColumn($name)) { - return $this->_columns[$name]; - } - } - - public function hasRelation($name) - { - return isset($this->_schema['relations'][$name]) ? true:false; - } - - public function getRelation($name) - { - if ($this->hasRelation($name)) { - return $this->_schema['relations'][$name]; - } - } public function toArray() { @@ -254,15 +225,15 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count foreach ($this->_data as $key => $value) { - if ($this->hasRelation($key) && $value instanceof Doctrine_Resource_Collection) { + if ($this->getTable()->hasRelation($key) && $value instanceof Doctrine_Resource_Collection) { if ($value->count() > 0) { $array[$key] = $value->toArray(); } - } else if ($this->hasRelation($key) && $value instanceof Doctrine_Resource_Record) { + } else if ($this->getTable()->hasRelation($key) && $value instanceof Doctrine_Resource_Record) { if ($value->exists() || $value->hasChanges()) { $array[$key] = $value->toArray(); } - } else if (!$this->hasRelation($key) && $this->hasColumn($key)) { + } else if (!$this->getTable()->hasRelation($key) && $this->getTable()->hasColumn($key)) { $array[$key] = $value; } } diff --git a/lib/Doctrine/Resource/Request.php b/lib/Doctrine/Resource/Request.php index ba0a177f0..10238ede4 100644 --- a/lib/Doctrine/Resource/Request.php +++ b/lib/Doctrine/Resource/Request.php @@ -71,18 +71,18 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params $collection = new Doctrine_Resource_Collection($model); foreach ($array as $record) { - $r = new Doctrine_Resource_Record($model); + $r = new $model(); foreach ($record as $key => $value) { - if ($r->hasRelation($key) && !empty($value)) { - $relation = $r->getRelation($key); + if ($r->getTable()->hasRelation($key) && !empty($value)) { + $relation = $r->getTable()->getRelation($key); if ($relation['type'] === Doctrine_Relation::MANY) { $r->set($key, $this->hydrate($value, $relation['class'], $key)); } else { $r->set($key, $this->hydrate(array($value), $relation['class'], $key)->getFirst()); } - } else if($r->hasColumn($key)) { + } else if($r->getTable()->hasColumn($key)) { $r->set($key, $value); } diff --git a/lib/Doctrine/Resource/Table.php b/lib/Doctrine/Resource/Table.php index c82ef56be..72907794f 100644 --- a/lib/Doctrine/Resource/Table.php +++ b/lib/Doctrine/Resource/Table.php @@ -34,10 +34,36 @@ class Doctrine_Resource_Table { protected $_model = null; + protected $_schema = null; public function __construct($model) { $this->_model = $model; + + $schema = $this->getConfig('schema'); + + if (isset($schema['schema'][$model]) && $schema['schema'][$model]) { + $this->_schema = $schema['schema'][$model]; + } + + if (isset($schema['relations'][$model]) && $schema['relations'][$model]) { + $this->_schema['relations'] = $schema['relations'][$model]; + } + } + + public function getSchema() + { + return $this->_schema; + } + + public function getRelations() + { + return $this->_schema['relations']; + } + + public function getConfig($key = null) + { + return Doctrine_Resource_Client::getInstance()->getConfig($key); } public function find($pk) @@ -61,4 +87,28 @@ class Doctrine_Resource_Table return $query->execute()->getFirst(); } + + public function hasColumn($name) + { + return isset($this->_schema['columns'][$name]) ? true:false; + } + + public function getColumn($name) + { + if ($this->hasColumn($name)) { + return $this->_columns[$name]; + } + } + + public function hasRelation($name) + { + return isset($this->_schema['relations'][$name]) ? true:false; + } + + public function getRelation($name) + { + if ($this->hasRelation($name)) { + return $this->_schema['relations'][$name]; + } + } } \ No newline at end of file diff --git a/playground/index.php b/playground/index.php index a1d1ca1b1..a5b220302 100644 --- a/playground/index.php +++ b/playground/index.php @@ -18,12 +18,12 @@ if ($action == 'server') { $config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); $client = Doctrine_Resource_Client::getInstance($config); - $table = $client->getTable('User'); - $user = $table->find(4); - $user->Phonenumber->add()->phonenumber = '555-5555'; - $user->name = 'jonnwage'; - $user->save(); + //$table = $client->getTable('User'); - print_r($user->toArray()); + $query = new Doctrine_Resource_Query(); + + $users = $query->from('User u, u.Phonenumber p, u.Address a, u.Book b, b.Author a')->execute(); + + print_r($users); } \ No newline at end of file