1
0
mirror of synced 2024-12-13 22:56:04 +03:00

Fixes for saving models.

This commit is contained in:
Jonathan.Wage 2007-09-21 18:19:19 +00:00
parent 9992cf30fc
commit 208fa1eef8
5 changed files with 44 additions and 14 deletions

View File

@ -12,4 +12,11 @@ class Doctrine_Resource_Client extends Doctrine_Resource
{ {
return new Doctrine_Resource_Query($this->config); return new Doctrine_Resource_Query($this->config);
} }
public function newRecord($model)
{
$record = new Doctrine_Resource_Record($model, $this->config);
return $record;
}
} }

View File

@ -5,6 +5,11 @@ 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)
{
$this->model = $model;
}
public function count() public function count()
{ {
return count($data); return count($data);

View File

@ -78,14 +78,10 @@ class Doctrine_Resource_Query extends Doctrine_Resource
{ {
$model = $passedKey ? $passedKey:$this->getModel(); $model = $passedKey ? $passedKey:$this->getModel();
$collection = new Doctrine_Resource_Collection(); $collection = new Doctrine_Resource_Collection($model, $this->config);
$collection->model = $model;
$collection->config = $this->config;
foreach ($array as $record) { foreach ($array as $record) {
$r = new Doctrine_Resource_Record(); $r = new Doctrine_Resource_Record($model, $this->config);
$r->config = $this->config;
$r->model = $model;
foreach ($record as $key => $value) { foreach ($record as $key => $value) {
if (is_array($value)) { if (is_array($value)) {

View File

@ -6,6 +6,12 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
public $model = null; public $model = null;
public $changes = array(); public $changes = array();
public function __construct($model, $config)
{
$this->model = $model;
$this->config = $config;
}
public function get($get) public function get($get)
{ {
if (!isset($this->data[$get])) { if (!isset($this->data[$get])) {
@ -46,6 +52,10 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
$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);
return $array;
} }
public function toArray() public function toArray()

View File

@ -27,25 +27,37 @@ class Doctrine_Resource_Server extends Doctrine_Resource
throw new Doctrine_Resource_Exception('You must specify a dql query'); throw new Doctrine_Resource_Exception('You must specify a dql query');
} }
} else if ($request['type'] == 'save') { } else if ($request['type'] == 'save') {
$table = Doctrine_Manager::getInstance()->getTable($request['model']); $model = $request['model'];
$table = Doctrine_Manager::getInstance()->getTable($model);
$pks = (array) $table->getIdentifier(); $pks = (array) $table->getIdentifier();
$pks = array_flip($pks); $pks = array_flip($pks);
$hasPk = false;
foreach (array_keys($pks) as $key) { foreach (array_keys($pks) as $key) {
$pks[$key] = $request['data'][$key]; if (isset($request['data'][$key]) && $request['data'][$key]) {
$pks[$key] = $request['data'][$key];
$hasPk = true;
}
} }
$record = $table->find($pks); if ($hasPk) {
$record = $table->find($pks);
} else {
$record = new $model();
}
$changes = $request['changes']; if (isset($request['changes']) && !empty($request['changes'])) {
$changes = $request['changes'];
foreach ($changes as $key => $value) {
$record->$key = $value; foreach ($changes as $key => $value) {
$record->$key = $value;
}
} }
$record->save(); $record->save();
$result = array('success' => true); $result = $record->toArray();
} }
return Doctrine_Parser::dump($result, $format); return Doctrine_Parser::dump($result, $format);