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);
|
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 $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);
|
||||||
|
@ -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)) {
|
||||||
|
@ -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()
|
||||||
|
@ -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) {
|
||||||
|
if (isset($request['data'][$key]) && $request['data'][$key]) {
|
||||||
$pks[$key] = $request['data'][$key];
|
$pks[$key] = $request['data'][$key];
|
||||||
|
|
||||||
|
$hasPk = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($hasPk) {
|
||||||
$record = $table->find($pks);
|
$record = $table->find($pks);
|
||||||
|
} else {
|
||||||
|
$record = new $model();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($request['changes']) && !empty($request['changes'])) {
|
||||||
$changes = $request['changes'];
|
$changes = $request['changes'];
|
||||||
|
|
||||||
foreach ($changes as $key => $value) {
|
foreach ($changes as $key => $value) {
|
||||||
$record->$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);
|
||||||
|
Loading…
Reference in New Issue
Block a user