. */ /** * Doctrine_Resource_Client * * @author Konsta Vesterinen * @author Jonathan H. Wage * @package Doctrine * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @version $Revision$ * @category Object Relational Mapping * @link www.phpdoctrine.com * @since 1.0 */ class Doctrine_Resource_Client extends Doctrine_Resource { public $loadDoctrine = false; static public function getInstance($config = null) { static $instance; if (!$instance) { $instance = new Doctrine_Resource_Client($config); if ($instance->loadDoctrine === true) { $instance->loadDoctrine(); } } return $instance; } public function loadDoctrine() { $path = '/tmp/' . md5(serialize($this->getConfig())); $classesPath = $path.'.classes.php'; if (!file_exists($path)) { $schema = file_get_contents($path); } else { $request = new Doctrine_Resource_Request(); $request->set('type', 'load'); $request->set('format', $this->getConfig()->get('format')); $schema = $request->execute(); if ($schema) { file_put_contents($path, $schema); } } if (file_exists($path) && $schema) { $import = new Doctrine_Import_Schema(); $schema = $import->buildSchema($path, $this->getConfig()->get('format')); if (file_exists($classesPath)) { $build = " $details) { $build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct(\$loadRelations = true) { parent::__construct(\$this->_model, \$loadRelations); } }\n"; } file_put_contents($classesPath, $build); } require_once($classesPath); $this->getConfig()->set('schema', $schema); } } public function find($model, $pk) { $record = $this->newRecord($model); $pk = is_array($pk) ? $pk:array($pk); $identifier = $record->identifier(); $identifier = is_array($identifier) ? $identifier:array($identifier); $where = ''; foreach (array_keys($identifier) as $key => $name) { $value = $pk[$key]; $where .= $model.'.' . $name . ' = '.$value; } $query = new Doctrine_Resource_Query(); $query->from($model)->where($where)->limit(1); return $query->execute()->getFirst(); } public function newQuery() { return new Doctrine_Resource_Query(); } public function newRecord($model, $loadRelations = true) { return new Doctrine_Resource_Record($model, $loadRelations); } public function newCollection($model) { return new Doctrine_Resource_Collection($model); } }