Fixes and tweaks. Added code to generate classes for schema and load them.
This commit is contained in:
parent
b40fd36efa
commit
6d6f6c7598
@ -173,7 +173,7 @@ class Doctrine_Import_Schema
|
||||
$relation['type'] = Doctrine_Relation::ONE;
|
||||
}
|
||||
|
||||
$this->relations[$className][$class] = $relation;
|
||||
$this->relations[$className][$alias] = $relation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
140
lib/Doctrine/Resource/Access.php
Normal file
140
lib/Doctrine/Resource/Access.php
Normal file
@ -0,0 +1,140 @@
|
||||
<?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_Resource_Access
|
||||
*
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
* @author Jonathan H. Wage <jwage@mac.com>
|
||||
* @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);
|
||||
}
|
||||
}
|
@ -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 = "<?php\n";
|
||||
foreach ($schema['schema'] as $className => $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();
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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)) {
|
||||
|
@ -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;
|
||||
|
@ -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';
|
||||
|
@ -1,12 +1,13 @@
|
||||
<?php
|
||||
require_once('playground.php');
|
||||
require_once('connection.php');
|
||||
require_once('models.php');
|
||||
require_once('data.php');
|
||||
|
||||
$action = isset($_REQUEST['action']) ? $_REQUEST['action']:'client';
|
||||
|
||||
if ($action == 'server') {
|
||||
require_once('connection.php');
|
||||
require_once('models.php');
|
||||
require_once('data.php');
|
||||
|
||||
$config = array('name' => '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());
|
||||
}
|
@ -13,6 +13,7 @@ $tables = array('Entity',
|
||||
'User',
|
||||
'Album',
|
||||
'Book',
|
||||
'Author',
|
||||
'Song',
|
||||
'Element',
|
||||
'Error',
|
||||
|
Loading…
Reference in New Issue
Block a user