diff --git a/lib/Doctrine/Import/Schema.php b/lib/Doctrine/Import/Schema.php index 4deb122c1..763ce66f2 100644 --- a/lib/Doctrine/Import/Schema.php +++ b/lib/Doctrine/Import/Schema.php @@ -173,7 +173,7 @@ class Doctrine_Import_Schema $relation['type'] = Doctrine_Relation::ONE; } - $this->relations[$className][$class] = $relation; + $this->relations[$className][$alias] = $relation; } } } diff --git a/lib/Doctrine/Resource/Access.php b/lib/Doctrine/Resource/Access.php new file mode 100644 index 000000000..6e965f8b3 --- /dev/null +++ b/lib/Doctrine/Resource/Access.php @@ -0,0 +1,140 @@ +. + */ + +/** + * Doctrine_Resource_Access + * + * @author Konsta Vesterinen + * @author Jonathan H. Wage + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @version $Revision$ + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + */ +class Doctrine_Resource_Access implements ArrayAccess +{ + /** + * setArray + * + * @param array $array an array of key => value pairs + * @since 1.0 + * @return Doctrine_Access + */ + public function setArray(array $array) + { + foreach ($array as $k=>$v) { + $this->set($k,$v); + } + + return $this; + } + /** + * __set an alias of set() + * + * @see set, offsetSet + * @param $name + * @param $value + * @since 1.0 + * @return void + */ + public function __set($name,$value) + { + $this->set($name,$value); + } + /** + * __get -- an alias of get() + * + * @see get, offsetGet + * @param mixed $name + * @since 1.0 + * @return mixed + */ + public function __get($name) + { + return $this->get($name); + } + /** + * __isset() + * + * @param string $name + * @since 1.0 + * @return boolean whether or not this object contains $name + */ + public function __isset($name) + { + return $this->contains($name); + } + /** + * __unset() + * + * @param string $name + * @since 1.0 + * @return void + */ + public function __unset($name) + { + return $this->remove($name); + } + /** + * @param mixed $offset + * @return boolean whether or not this object contains $offset + */ + public function offsetExists($offset) + { + return $this->contains($offset); + } + /** + * offsetGet an alias of get() + * @see get, __get + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) + { + return $this->get($offset); + } + /** + * sets $offset to $value + * @see set, __set + * @param mixed $offset + * @param mixed $value + * @return void + */ + public function offsetSet($offset, $value) + { + if ( ! isset($offset)) { + $this->add($value); + } else { + $this->set($offset, $value); + } + } + /** + * unset a given offset + * @see set, offsetSet, __set + * @param mixed $offset + */ + public function offsetUnset($offset) + { + return $this->remove($offset); + } +} \ No newline at end of file diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index f142213b8..dc1f7eb74 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -53,6 +53,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource public function loadDoctrine() { $path = '/tmp/' . md5(serialize($this->getConfig())); + $classesPath = $path.'.classes.php'; if (!file_exists($path)) { $schema = file_get_contents($path); @@ -72,10 +73,41 @@ class Doctrine_Resource_Client extends Doctrine_Resource $import = new Doctrine_Import_Schema(); $schema = $import->buildSchema($path, $this->getConfig()->get('format')); + if (file_exists($classesPath)) { + $build = " $details) { + $build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct(\$loadRelations = true) { parent::__construct(\$this->_model, \$loadRelations); } }\n"; + } + + file_put_contents($classesPath, $build); + } + + require_once($classesPath); + $this->getConfig()->set('schema', $schema); } } + public function find($model, $pk) + { + $record = $this->newRecord($model); + + $pk = is_array($pk) ? $pk:array($pk); + $identifier = $record->identifier(); + $identifier = is_array($identifier) ? $identifier:array($identifier); + + $where = ''; + foreach (array_keys($identifier) as $key => $name) { + $value = $pk[$key]; + $where .= $model.'.' . $name . ' = '.$value; + } + + $query = new Doctrine_Resource_Query(); + $query->from($model)->where($where)->limit(1); + + return $query->execute()->getFirst(); + } + public function newQuery() { return new Doctrine_Resource_Query(); diff --git a/lib/Doctrine/Resource/Collection.php b/lib/Doctrine/Resource/Collection.php index b87e2464b..215393a4f 100644 --- a/lib/Doctrine/Resource/Collection.php +++ b/lib/Doctrine/Resource/Collection.php @@ -64,9 +64,15 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C $this->_data[$set] = $value; } - public function add($value) + public function add($value = null) { + if (!$value) { + $value = Doctrine_Resource_Client::getInstance()->newRecord($this->_model); + } + $this->_data[] = $value; + + return $value; } public function getIterator() @@ -84,7 +90,7 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C $array = array(); foreach ($this->_data as $key => $record) { - if ($record->exists()) { + if ($record->exists() || $record->hasChanges()) { $array[$this->_model . '_' .$key] = $record->toArray(); } } diff --git a/lib/Doctrine/Resource/Record.php b/lib/Doctrine/Resource/Record.php index 5835330a2..4abe18305 100644 --- a/lib/Doctrine/Resource/Record.php +++ b/lib/Doctrine/Resource/Record.php @@ -36,6 +36,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count protected $_data = array(); protected $_model = null; protected $_schema = null; + protected $_changes = array(); public function __construct($model, $loadRelations = true) { @@ -54,6 +55,11 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count $this->initialize($loadRelations); } + public function clearChanges() + { + $this->_changes = array(); + } + public function initialize($loadRelations = true) { if (!$this->_schema) { @@ -91,20 +97,18 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count return Doctrine_Resource_Client::getInstance()->getConfig($key); } - public function get($get) + public function get($key) { - if (!isset($this->_data[$get])) { - $this->_data[$get] = null; - } else { - $this->_data[$get] = Doctrine_Resource_Client::getInstance()->newRecord($get, false); - } - - return $this->_data[$get]; + return $this->_data[$key]; } - public function set($set, $value) + public function set($key, $value) { - $this->_data[$set] = $value; + if ($this->_data[$key] != $value) { + $this->_changes[$key] = $value; + } + + $this->_data[$key] = $value; } public function count() @@ -117,6 +121,45 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count return new ArrayIterator($this->_data); } + public function getChanges() + { + $array = array(); + + foreach ($this->_data as $key => $value) { + if ($this->hasRelation($key)) { + + $relation = $this->getRelation($key); + + if ($relation['type'] === Doctrine_Relation::ONE) { + if ($this->_data[$key]->hasChanges()) { + $array[$key] = $this->_data[$key]->getChanges(); + } + } else { + foreach ($this->_data[$key] as $key2 => $record) { + if ($record->hasChanges()) { + $array[$key][$record->getModel() . '_' .$key2] = $record->getChanges(); + } + } + } + } else if ($this->hasColumn($key)) { + if (isset($this->_changes[$key])) { + $array[$key] = $value; + } + } + } + + $identifier = $this->identifier(); + + $array = array_merge($identifier, $array); + + return $array; + } + + public function hasChanges() + { + return !empty($this->_changes) ? true:false; + } + public function save() { $format = $this->getConfig('format'); @@ -125,7 +168,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count $request->set('format', $format); $request->set('type', 'save'); $request->set('model', $this->getModel()); - $request->set('data', $this->toArray()); + $request->set('data', $this->getChanges()); $response = $request->execute(); @@ -202,7 +245,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count $array[$key] = $value->toArray(); } } else if ($this->hasRelation($key) && $value instanceof Doctrine_Resource_Record) { - if ($value->exists()) { + if ($value->exists() || $value->hasChanges()) { $array[$key] = $value->toArray(); } } else if (!$this->hasRelation($key) && $this->hasColumn($key)) { diff --git a/lib/Doctrine/Resource/Request.php b/lib/Doctrine/Resource/Request.php index 717039a8c..ba0a177f0 100644 --- a/lib/Doctrine/Resource/Request.php +++ b/lib/Doctrine/Resource/Request.php @@ -85,6 +85,8 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params } else if($r->hasColumn($key)) { $r->set($key, $value); } + + $r->clearChanges(); } $collection[] = $r; diff --git a/playground/data.php b/playground/data.php index 9cdfd8191..3bdd8b583 100644 --- a/playground/data.php +++ b/playground/data.php @@ -12,7 +12,6 @@ $groups->save(); $users = new Doctrine_Collection('User'); - $users[0]->name = 'zYne'; $users[0]['Email']->address = 'zYne@example.com'; $users[0]['Phonenumber'][0]->phonenumber = '123 123'; diff --git a/playground/index.php b/playground/index.php index 35a3b2af7..23bdf5a5e 100644 --- a/playground/index.php +++ b/playground/index.php @@ -1,12 +1,13 @@ 'Doctrine_Resource_Test_Server', 'models' => $tables); @@ -14,15 +15,11 @@ if ($action == 'server') { $server->run($_REQUEST); } else { - //$config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); - $config = array('url' => 'http://dev.centresource.com/jwage/hca/web/frontend_dev.php/main'); + $config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'); $client = Doctrine_Resource_Client::getInstance($config); - $user = $client->newRecord('sfGuardUser'); - - $user->name = 'jonnnywage'; + $user = new User(); + $user->name = 'jonnwage'; $user->save(); - - print_r($user->toArray()); } \ No newline at end of file diff --git a/playground/models.php b/playground/models.php index 78a7b6a4b..7f4c60717 100644 --- a/playground/models.php +++ b/playground/models.php @@ -13,6 +13,7 @@ $tables = array('Entity', 'User', 'Album', 'Book', + 'Author', 'Song', 'Element', 'Error',