1
0
mirror of synced 2025-01-18 06:21:40 +03:00

Fixes for doctrine resource.

This commit is contained in:
Jonathan.Wage 2007-09-22 01:32:48 +00:00
parent 5aa73029cf
commit b98130db1e
10 changed files with 121 additions and 93 deletions

View File

@ -589,12 +589,15 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* *
* @param boolean $deep * @param boolean $deep
*/ */
public function toArray($deep = false) public function toArray($deep = false, $prefixKey = false)
{ {
if ($deep) { if ($deep) {
$data = array(); $data = array();
foreach ($this->data as $key => $record) { foreach ($this->data as $key => $record) {
$data[$key] = $record->toArray($deep);
$key = $prefixKey ? get_class($record) . '_' .$key:$key;
$data[$key] = $record->toArray($deep, $prefixKey);
} }
return $data; return $data;
} else { } else {

View File

@ -108,7 +108,7 @@ class Doctrine_Parser_Xml extends Doctrine_Parser
if (is_array($return)) { if (is_array($return)) {
return $return; return $return;
} else { } else {
return $false; return array();
} }
} }
} }

View File

@ -1118,7 +1118,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
* @param boolean $deep - Return also the relations * @param boolean $deep - Return also the relations
* @return array * @return array
*/ */
public function toArray($deep = false) public function toArray($deep = false, $prefixKey = false)
{ {
$a = array(); $a = array();
@ -1134,18 +1134,20 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
} }
if ($deep) { if ($deep) {
foreach ($this->_references as $key => $relation) { foreach ($this->_references as $key => $relation) {
$a[$key] = $relation->toArray($deep); $a[$key] = $relation->toArray($deep, $prefixKey);
} }
} }
return array_merge($a, $this->_values); return array_merge($a, $this->_values);
} }
public function fromArray($array) public function fromArray($array)
{ {
foreach ($array as $key => $value) { if (is_array($array)) {
if (!$this->getTable()->hasRelation($key) && $this->getTable()->hasColumn($key)) { foreach ($array as $key => $value) {
$this->$key = $value; if (is_array($value)) {
} else { $this->$key->fromArray($value);
$this->$key->fromArray($value); } else {
$this->$key = $value;
}
} }
} }
} }

View File

@ -6,6 +6,29 @@ class Doctrine_Resource
$url .= strstr($url, '?') ? '&':'?'; $url .= strstr($url, '?') ? '&':'?';
$url .= http_build_query($request); $url .= http_build_query($request);
return file_get_contents($url); $response = file_get_contents($url);
return $response;
}
public function hydrate(array $array, $model, $config, $passedKey = null)
{
$collection = new Doctrine_Resource_Collection($model, $config);
foreach ($array as $record) {
$r = new Doctrine_Resource_Record($model, $config);
foreach ($record as $key => $value) {
if (is_array($value)) {
$r->data[$key] = $this->hydrate($value, $model, $config, $key);
} else {
$r->data[$key] = $value;
}
}
$collection->data[] = $r;
}
return $collection;
} }
} }

View File

@ -17,4 +17,9 @@ class Doctrine_Resource_Client extends Doctrine_Resource
{ {
return new Doctrine_Resource_Record($model, $this->config); return new Doctrine_Resource_Record($model, $this->config);
} }
public function newCollection($model)
{
return new Doctrine_Resource_Collection($model, $this->config);
}
} }

View File

@ -5,9 +5,10 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
public $config = array(); public $config = array();
public $model = null; public $model = null;
public function __construct($model) public function __construct($model, $config)
{ {
$this->model = $model; $this->model = $model;
$this->config = $config;
} }
public function count() public function count()
@ -15,6 +16,18 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
return count($data); return count($data);
} }
public function get($get)
{
if (isset($this->data[$get])) {
return $this->data[$get];
}
}
public function set($set, $value)
{
$this->data[$set] = $value;
}
public function getIterator() public function getIterator()
{ {
$data = $this->data; $data = $this->data;
@ -30,6 +43,7 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
public function toArray() public function toArray()
{ {
$array = array(); $array = array();
foreach ($this->data as $key => $record) { foreach ($this->data as $key => $record) {
$array[$key] = $record->toArray(); $array[$key] = $record->toArray();
} }

View File

@ -54,8 +54,8 @@ class Doctrine_Resource_Query extends Doctrine_Resource
{ {
$array = Doctrine_Parser::load($response, $this->getFormat()); $array = Doctrine_Parser::load($response, $this->getFormat());
$hydrated = $this->hydrate($array); $hydrated = $this->hydrate($array, $this->getModel(), $this->config);
return $hydrated; return $hydrated;
} }
@ -74,29 +74,6 @@ class Doctrine_Resource_Query extends Doctrine_Resource
return $url; return $url;
} }
public function hydrate(array $array, $passedKey = null)
{
$model = $passedKey ? $passedKey:$this->getModel();
$collection = new Doctrine_Resource_Collection($model, $this->config);
foreach ($array as $record) {
$r = new Doctrine_Resource_Record($model, $this->config);
foreach ($record as $key => $value) {
if (is_array($value)) {
$r->data[$key] = $this->hydrate($value, $key);
} else {
$r->data[$key] = $value;
}
}
$collection->data[] = $r;
}
return $collection;
}
public function getModel() public function getModel()
{ {
$dql = $this->getDql(); $dql = $this->getDql();

View File

@ -4,7 +4,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public $data = array(); public $data = array();
public $config = array(); public $config = array();
public $model = null; public $model = null;
public $changes = array();
public function __construct($model, $config) public function __construct($model, $config)
{ {
@ -24,8 +23,6 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public function set($set, $value) public function set($set, $value)
{ {
$this->data[$set] = $value; $this->data[$set] = $value;
$this->changes[$set] = $value;
} }
public function count() public function count()
@ -40,22 +37,27 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
return new ArrayIterator($data); return new ArrayIterator($data);
} }
public function save() public function newRequest($type)
{ {
$request = array(); $request = array();
$request['format'] = $this->config['format']; $request['format'] = isset($this->config['format']) ? $this->config['format']:'xml';
$request['type'] = 'save'; $request['type'] = $type;
$request['model'] = $this->model; $request['model'] = $this->model;
$request['data'] = $this->data;
$request['changes'] = $this->changes; return $request;
}
public function save()
{
$request = $this->newRequest('save');
$request['data'] = $this->toArray();
$response = Doctrine_Resource::request($this->config['url'], $request); $response = Doctrine_Resource::request($this->config['url'], $request);
$array = Doctrine_Parser::load($response, $request['format']); $array = Doctrine_Parser::load($response, $request['format']);
$this->data = array_merge($this->data, $array); $resource = new Doctrine_Resource();
$this->data = $resource->hydrate(array($array), $this->model, $this->config)->getFirst()->data;
return $array;
} }
public function toArray() public function toArray()
@ -63,7 +65,7 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
$array = array(); $array = array();
foreach ($this->data as $key => $value) { foreach ($this->data as $key => $value) {
if ($value instanceof Doctrine_Resource_Collection) { if ($value instanceof Doctrine_Resource_Collection OR $value instanceof Doctrine_Resource_Record) {
$array[$key] = $value->toArray(); $array[$key] = $value->toArray();
} else { } else {
$array[$key] = $value; $array[$key] = $value;

View File

@ -2,12 +2,35 @@
class Doctrine_Resource_Server extends Doctrine_Resource class Doctrine_Resource_Server extends Doctrine_Resource
{ {
public $config = array(); public $config = array();
public $format = 'xml';
public function __construct($config) public function __construct($config)
{ {
$this->config = array_merge($config, $this->config); $this->config = array_merge($config, $this->config);
} }
public function executeSave($request)
{
$model = $request['model'];
$data = $request['data'];
$record = new $model();
$record->fromArray($data);
$record->save();
return $record->toArray(true, true);
}
public function executeQuery($request)
{
$dql = $request['dql'];
$params = isset($request['params']) ? $request['params']:array();
$conn = Doctrine_Manager::connection();
return $conn->query($dql, $params)->toArray(true, true);
}
public function execute($request) public function execute($request)
{ {
if (!isset($request['type'])) { if (!isset($request['type'])) {
@ -15,50 +38,10 @@ class Doctrine_Resource_Server extends Doctrine_Resource
} }
$format = isset($request['format']) ? $request['format']:'xml'; $format = isset($request['format']) ? $request['format']:'xml';
$type = $request['type'];
$funcName = 'execute' . Doctrine::classify($type);
if ($request['type'] == 'query') { $result = $this->$funcName($request);
if (isset($request['dql']) && $request['dql']) {
$dql = $request['dql'];
$params = isset($request['params']) ? $request['params']:array();
$conn = Doctrine_Manager::connection();
$result = $conn->query($dql, $params, Doctrine::FETCH_ARRAY);
} else {
throw new Doctrine_Resource_Exception('You must specify a dql query');
}
} else if ($request['type'] == 'save') {
$model = $request['model'];
$table = Doctrine_Manager::getInstance()->getTable($model);
$pks = (array) $table->getIdentifier();
$pks = array_flip($pks);
$hasPk = false;
foreach (array_keys($pks) as $key) {
if (isset($request['data'][$key]) && $request['data'][$key]) {
$pks[$key] = $request['data'][$key];
$hasPk = true;
}
}
if ($hasPk) {
$record = $table->find($pks);
} else {
$record = new $model();
}
if (isset($request['changes']) && !empty($request['changes'])) {
$changes = $request['changes'];
foreach ($changes as $key => $value) {
$record->$key = $value;
}
}
$record->save();
$result = $record->toArray();
}
return Doctrine_Parser::dump($result, $format); return Doctrine_Parser::dump($result, $format);
} }

View File

@ -2,4 +2,23 @@
require_once('playground.php'); require_once('playground.php');
require_once('connection.php'); require_once('connection.php');
require_once('models.php'); require_once('models.php');
require_once('data.php'); require_once('data.php');
$action = isset($_REQUEST['action']) ? $_REQUEST['action']:'client';
if ($action == 'server') {
$config = array();
$server = new Doctrine_Resource_Server($config);
$server->run($_REQUEST);
} else {
$config = array('url' => 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server');
$client = new Doctrine_Resource_Client($config);
$record = $client->newRecord('User');
$record->name = 'jon wage';
$record->loginname = 'test';
$record->save();
print_r($record->toArray());
}