1
0
mirror of synced 2025-02-07 15:59:27 +03:00

70 lines
2.2 KiB
PHP
Raw Normal View History

<?php
class Doctrine_Resource_Server extends Doctrine_Resource
{
2007-09-21 18:01:08 +00:00
public $config = array();
public function __construct($config)
{
$this->config = array_merge($config, $this->config);
}
public function execute($request)
{
2007-09-21 18:01:08 +00:00
if (!isset($request['type'])) {
throw new Doctrine_Resource_Exception('You must specify a request type: query or save');
}
2007-09-21 18:01:08 +00:00
$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') {
2007-09-21 18:19:19 +00:00
$model = $request['model'];
$table = Doctrine_Manager::getInstance()->getTable($model);
2007-09-21 18:01:08 +00:00
$pks = (array) $table->getIdentifier();
$pks = array_flip($pks);
2007-09-21 18:19:19 +00:00
$hasPk = false;
2007-09-21 18:01:08 +00:00
foreach (array_keys($pks) as $key) {
2007-09-21 18:19:19 +00:00
if (isset($request['data'][$key]) && $request['data'][$key]) {
$pks[$key] = $request['data'][$key];
$hasPk = true;
}
2007-09-21 18:01:08 +00:00
}
2007-09-21 18:19:19 +00:00
if ($hasPk) {
$record = $table->find($pks);
} else {
$record = new $model();
}
2007-09-21 18:19:19 +00:00
if (isset($request['changes']) && !empty($request['changes'])) {
$changes = $request['changes'];
foreach ($changes as $key => $value) {
$record->$key = $value;
}
}
2007-09-21 18:01:08 +00:00
$record->save();
2007-09-21 18:19:19 +00:00
$result = $record->toArray();
}
2007-09-21 18:01:08 +00:00
return Doctrine_Parser::dump($result, $format);
}
2007-09-21 18:01:08 +00:00
public function run($request)
{
echo $this->execute($request);
}
}