1
0
mirror of synced 2024-12-14 07:06:04 +03:00

Fixes for Doctrine Resource

This commit is contained in:
Jonathan.Wage 2007-09-24 18:22:52 +00:00
parent d6c0bcb4fc
commit e9ba4504bf
7 changed files with 139 additions and 53 deletions

View File

@ -1143,9 +1143,9 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
{
if (is_array($array)) {
foreach ($array as $key => $value) {
if (is_array($value)) {
if ($this->getTable()->hasRelation($key) && $value) {
$this->$key->fromArray($value);
} else {
} else if($this->getTable()->hasColumn($key) && $value) {
$this->$key = $value;
}
}

View File

@ -59,7 +59,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource
} else {
$request = new Doctrine_Resource_Request();
$request->set('type', 'load');
$request->set('format', 'xml');
$request->set('format', $this->getConfig()->get('format'));
$schema = $request->execute();
@ -67,7 +67,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource
}
$import = new Doctrine_Import_Schema();
$schema = $import->buildSchema($path, 'xml');
$schema = $import->buildSchema($path, $this->getConfig()->get('format'));
$this->getConfig()->set('schema', $schema);
}

View File

@ -31,7 +31,7 @@
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, IteratorAggregate
class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements Countable, IteratorAggregate
{
protected $_data = array();
protected $_config = array();
@ -84,7 +84,9 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
$array = array();
foreach ($this->_data as $key => $record) {
$array[$key] = $record->toArray();
if ($record->exists()) {
$array[$this->_model . '_' .$key] = $record->toArray();
}
}
return $array;

View File

@ -31,7 +31,7 @@
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Countable, IteratorAggregate
class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate
{
protected $_data = array();
protected $_model = null;
@ -119,7 +119,7 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public function save()
{
$format = $this->getConfig('format') ? $this->getConfig('format'):'xml';
$format = $this->getConfig('format');
$request = new Doctrine_Resource_Request();
$request->set('format', $format);
@ -139,14 +139,73 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
return $this->_model;
}
public function identifier()
{
$identifier = array();
if (isset($this->_schema['columns']) && is_array($this->_schema['columns'])) {
foreach ($this->_schema['columns'] as $name => $column) {
if ($column['primary'] == true) {
$identifier[$name] = $this->_data[$name];
}
}
}
return $identifier;
}
public function exists()
{
$identifier = $this->identifier();
foreach ($identifier as $key => $value) {
if (!$value) {
return false;
}
}
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()
{
$array = array();
foreach ($this->_data as $key => $value) {
if ($value instanceof Doctrine_Resource_Collection OR $value instanceof Doctrine_Resource_Record) {
$array[$key] = $value->toArray();
} else {
if ($this->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) {
if ($value->exists()) {
$array[$key] = $value->toArray();
}
} else if (!$this->hasRelation($key) && $this->hasColumn($key)) {
$array[$key] = $value;
}
}

View File

@ -40,49 +40,49 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
return Doctrine_Resource_Client::getInstance()->getConfig($key);
}
public function getParams()
{
if ($this->_params === null) {
$this->_params = new Doctrine_Resource_Params();
}
return $this->_params;
}
public function get($key)
{
return $this->getParams()->get($key);
}
public function set($key, $value)
{
return $this->getParams()->set($key, $value);
}
public function execute()
{
$url = $this->getConfig()->get('url');
$url .= strstr($this->getConfig()->get('url'), '?') ? '&':'?';
$url .= http_build_query($this->getParams()->getAll());
$data = array('type' => $this->get('type'), 'format' => $this->getConfig()->get('format'), 'data' => Doctrine_Parser::dump($this->getAll(), $this->getConfig()->get('format')));
$response = file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Doctrine_Resource_Exception('Request failed');
}
curl_close($ch);
return $response;
}
public function hydrate(array $array, $model, $passedKey = null)
{
$config = $this->getConfig();
if (empty($array)) {
return false;
}
$collection = new Doctrine_Resource_Collection($model, $config);
$collection = new Doctrine_Resource_Collection($model);
foreach ($array as $record) {
$r = new Doctrine_Resource_Record($model, $config);
$r = new Doctrine_Resource_Record($model);
foreach ($record as $key => $value) {
if (is_array($value)) {
$r->set($key, $this->hydrate($value, $model, $config, $key));
} else {
if ($r->hasRelation($key) && !empty($value)) {
$relation = $r->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)) {
$r->set($key, $value);
}
}

View File

@ -31,7 +31,7 @@
* @link www.phpdoctrine.com
* @since 1.0
*/
class Doctrine_Resource_Server extends Doctrine_resource
class Doctrine_Resource_Server extends Doctrine_Resource
{
static public function getInstance($config = null)
{
@ -49,7 +49,29 @@ class Doctrine_Resource_Server extends Doctrine_resource
$model = $request->get('model');
$data = $request->get('data');
$record = new $model();
$table = Doctrine_Manager::getInstance()->getTable($model);
$identifier = $table->getIdentifier();
if (!is_array($identifier)) {
$identifier = array($identifier);
}
$existing = true;
$pks = array();
foreach ($identifier as $name) {
if (isset($data[$name])) {
$pks[$name] = $data[$name];
} else {
$existing = false;
}
}
if ($existing) {
$record = $table->find($pks);
} else {
$record = new $model();
}
$record->fromArray($data);
$record->save();
@ -82,20 +104,22 @@ class Doctrine_Resource_Server extends Doctrine_resource
return $schema;
}
public function execute($request)
public function execute(array $r)
{
if (!isset($request['type'])) {
throw new Doctrine_Resource_Exception('You must specify a request type');
}
if (!isset($r['data'])) {
throw new Doctrine_Resource_Exception('You must specify a data xml string in your request');
}
$type = $r['type'];
$format = isset($r['format']) ? $r['format']:'xml';
$data = Doctrine_Parser::load($r['data'], $format);
$format = isset($request['format']) ? $request['format']:'xml';
$type = $request['type'];
$funcName = 'execute' . Doctrine::classify($type);
$request = new Doctrine_Resource_Request($request);
$requestObj = new Doctrine_Resource_Request($data);
if (method_exists($this, $funcName)) {
$result = $this->$funcName($request);
$result = $this->$funcName($requestObj);
} else {
throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
}

View File

@ -18,9 +18,10 @@ if ($action == 'server') {
$client = Doctrine_Resource_Client::getInstance($config);
$user = $client->newRecord('User');
$user->name = 'jonathan h. wage';
$user->save();
$query = new Doctrine_Resource_Query();
$query->from('User u, u.Email e, u.Phonenumber p');
print_r($user->toArray());
$users = $query->execute();
print_r($users->toArray());
}