Commit 9992cf30 authored by Jonathan.Wage's avatar Jonathan.Wage

Fixes for Doctrine_Resource.

parent ce0ec8df
<?php
class Doctrine_Resource
{
public static function request($url, $request)
{
$url .= strstr($url, '?') ? '&':'?';
$url .= http_build_query($request);
return file_get_contents($url);
}
}
\ No newline at end of file
This diff is collapsed.
<?php
class Doctrine_Resource_Collection extends Doctrine_Access implements Countable, IteratorAggregate
{
public $data = array();
public $config = array();
public $model = null;
public function count()
{
return count($data);
}
public function getIterator()
{
$data = $this->data;
return new ArrayIterator($data);
}
public function save()
{
foreach ($data as $record) {
$record->save();
}
}
public function getFirst()
{
return $this->data[0];
}
public function toArray()
{
$array = array();
foreach($this->data as $key => $record) {
$array[$key] = $record->toArray();
}
return $array;
}
}
\ No newline at end of file
<?php
class Doctrine_Resource_Exception extends Doctrine_Exception
{
}
\ No newline at end of file
This diff is collapsed.
<?php
class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Countable, IteratorAggregate
{
public $data = array();
public $config = array();
public $model = null;
public $changes = array();
public function get($get)
{
if (!isset($this->data[$get])) {
$this->data[$get] = null;
}
return $this->data[$get];
}
public function set($set, $value)
{
$this->data[$set] = $value;
$this->changes[$set] = $value;
}
public function count()
{
return count($this->data);
}
public function getIterator()
{
$data = $this->data;
return new ArrayIterator($data);
}
public function save()
{
$request = array();
$request['format'] = $this->config['format'];
$request['type'] = 'save';
$request['model'] = $this->model;
$request['data'] = $this->data;
$request['changes'] = $this->changes;
$response = Doctrine_Resource::request($this->config['url'], $request);
$array = Doctrine_Parser::load($response, $request['format']);
}
public function toArray()
{
$array = array();
foreach ($this->data as $key => $value) {
if ($value instanceof Doctrine_Resource_Collection) {
$array[$key] = $value->toArray();
} else {
$array[$key] = $value;
}
}
return $array;
}
}
\ No newline at end of file
<?php
class Doctrine_Resource_Server extends Doctrine_Resource
{
public $config = array();
public function __construct($config)
{
$this->config = array_merge($config, $this->config);
}
public function execute($request)
{
if (isset($request['dql'])) {
$query = new Doctrine_Query();
$result = $query->query($request['dql']);
} else {
$result = $this->buildDql($request['parts']);
if (!isset($request['type'])) {
throw new Doctrine_Resource_Exception('You must specify a request type: query or save');
}
$data = array();
foreach ($result as $recordKey => $record) {
$array = $record->toArray();
$format = isset($request['format']) ? $request['format']:'xml';
if ($request['type'] == 'query') {
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') {
$table = Doctrine_Manager::getInstance()->getTable($request['model']);
$pks = (array) $table->getIdentifier();
$pks = array_flip($pks);
foreach (array_keys($pks) as $key) {
$pks[$key] = $request['data'][$key];
}
$record = $table->find($pks);
$recordKey = get_class($record). '_' .($recordKey + 1);
$changes = $request['changes'];
foreach ($array as $valueKey => $value) {
$data[get_class($record)][$recordKey][$valueKey] = $value;
foreach ($changes as $key => $value) {
$record->$key = $value;
}
$record->save();
$result = array('success' => true);
}
$format = isset($request['format']) ? $request['format']:'xml';
return Doctrine_Parser::dump($data, $format);
return Doctrine_Parser::dump($result, $format);
}
public function buildDql($parts)
public function run($request)
{
}
public function run()
{
$request = $_REQUEST;
echo $this->execute($request);
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ $tables = array('Entity',
'Group',
'User',
'Album',
'Book',
'Song',
'Element',
'Error',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment