Fixes for saving models.
This commit is contained in:
parent
9992cf30fc
commit
208fa1eef8
@ -12,4 +12,11 @@ class Doctrine_Resource_Client extends Doctrine_Resource
|
||||
{
|
||||
return new Doctrine_Resource_Query($this->config);
|
||||
}
|
||||
|
||||
public function newRecord($model)
|
||||
{
|
||||
$record = new Doctrine_Resource_Record($model, $this->config);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
@ -5,6 +5,11 @@ class Doctrine_Resource_Collection extends Doctrine_Access implements Countable,
|
||||
public $config = array();
|
||||
public $model = null;
|
||||
|
||||
public function __construct($model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($data);
|
||||
|
@ -78,14 +78,10 @@ class Doctrine_Resource_Query extends Doctrine_Resource
|
||||
{
|
||||
$model = $passedKey ? $passedKey:$this->getModel();
|
||||
|
||||
$collection = new Doctrine_Resource_Collection();
|
||||
$collection->model = $model;
|
||||
$collection->config = $this->config;
|
||||
$collection = new Doctrine_Resource_Collection($model, $this->config);
|
||||
|
||||
foreach ($array as $record) {
|
||||
$r = new Doctrine_Resource_Record();
|
||||
$r->config = $this->config;
|
||||
$r->model = $model;
|
||||
$r = new Doctrine_Resource_Record($model, $this->config);
|
||||
|
||||
foreach ($record as $key => $value) {
|
||||
if (is_array($value)) {
|
||||
|
@ -6,6 +6,12 @@ class Doctrine_Resource_Record extends Doctrine_Record_Abstract implements Count
|
||||
public $model = null;
|
||||
public $changes = array();
|
||||
|
||||
public function __construct($model, $config)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function get($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);
|
||||
|
||||
$array = Doctrine_Parser::load($response, $request['format']);
|
||||
|
||||
$this->data = array_merge($this->data, $array);
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
|
@ -27,25 +27,37 @@ class Doctrine_Resource_Server extends Doctrine_Resource
|
||||
throw new Doctrine_Resource_Exception('You must specify a dql query');
|
||||
}
|
||||
} 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_flip($pks);
|
||||
|
||||
$hasPk = false;
|
||||
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'];
|
||||
|
||||
foreach ($changes as $key => $value) {
|
||||
$record->$key = $value;
|
||||
if (isset($request['changes']) && !empty($request['changes'])) {
|
||||
$changes = $request['changes'];
|
||||
|
||||
foreach ($changes as $key => $value) {
|
||||
$record->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$record->save();
|
||||
|
||||
$result = array('success' => true);
|
||||
$result = $record->toArray();
|
||||
}
|
||||
|
||||
return Doctrine_Parser::dump($result, $format);
|
||||
|
Loading…
Reference in New Issue
Block a user