1
0
mirror of synced 2025-01-19 06:51:40 +03:00

244 lines
7.4 KiB
PHP
Raw Normal View History

2007-09-21 18:01:08 +00:00
<?php
2007-09-22 22:02:58 +00:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Resource_Record
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
2007-09-24 04:58:57 +00:00
* @author Jonathan H. Wage <jwage@mac.com>
2007-09-22 22:02:58 +00:00
* @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
*/
2007-09-24 18:22:52 +00:00
class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate
2007-09-21 18:01:08 +00:00
{
2007-09-24 04:58:57 +00:00
protected $_data = array();
protected $_model = null;
2007-09-24 22:19:44 +00:00
protected $_table = null;
protected $_changes = array();
2007-09-21 18:01:08 +00:00
2007-09-24 04:58:57 +00:00
public function __construct($model, $loadRelations = true)
2007-09-21 18:19:19 +00:00
{
2007-09-24 04:58:57 +00:00
$this->_model = $model;
2007-09-24 22:19:44 +00:00
$this->_table = Doctrine_Resource_Client::getInstance()->getTable($model);
2007-09-24 04:58:57 +00:00
$this->initialize($loadRelations);
}
public function clearChanges()
{
$this->_changes = array();
}
2007-09-24 04:58:57 +00:00
public function initialize($loadRelations = true)
{
2007-09-24 22:19:44 +00:00
$schema = $this->getTable()->getSchema();
$relations = $schema['relations'];
2007-09-24 04:58:57 +00:00
if (isset($schema['columns'])) {
$columns = $schema['columns'];
foreach ($columns as $column) {
if (!isset($this->_data[$column['name']]) || $this->_data[$column['name']]) {
$this->_data[$column['name']] = null;
}
}
}
if (isset($schema['relations']) && $loadRelations) {
$relations = $schema['relations'];
foreach ($relations as $relation) {
if ($relation['type'] === Doctrine_Relation::ONE) {
$this->_data[$relation['alias']] = Doctrine_Resource_Client::getInstance()->newRecord($relation['class'], false);
} else {
$this->_data[$relation['alias']] = Doctrine_Resource_Client::getInstance()->newCollection($relation['class']);
}
}
}
}
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
2007-09-21 18:19:19 +00:00
}
public function get($key)
2007-09-21 18:01:08 +00:00
{
return $this->_data[$key];
2007-09-21 18:01:08 +00:00
}
public function set($key, $value)
2007-09-21 18:01:08 +00:00
{
if ($this->_data[$key] != $value) {
$this->_changes[$key] = $value;
}
$this->_data[$key] = $value;
2007-09-21 18:01:08 +00:00
}
public function count()
{
2007-09-24 04:58:57 +00:00
return count($this->_data);
2007-09-21 18:01:08 +00:00
}
public function getIterator()
{
2007-09-24 04:58:57 +00:00
return new ArrayIterator($this->_data);
2007-09-22 01:32:48 +00:00
}
public function getChanges()
{
$array = array();
foreach ($this->_data as $key => $value) {
2007-09-24 22:19:44 +00:00
if ($this->getTable()->hasRelation($key)) {
2007-09-24 22:19:44 +00:00
$relation = $this->getTable()->getRelation($key);
if ($relation['type'] === Doctrine_Relation::ONE) {
if ($this->_data[$key]->hasChanges()) {
$array[$key] = $this->_data[$key]->getChanges();
}
} else {
foreach ($this->_data[$key] as $key2 => $record) {
if ($record->hasChanges()) {
$array[$key][$record->getModel() . '_' .$key2] = $record->getChanges();
}
}
}
2007-09-24 22:19:44 +00:00
} else if ($this->getTable()->hasColumn($key)) {
if (isset($this->_changes[$key])) {
$array[$key] = $value;
}
}
}
$identifier = $this->identifier();
$array = array_merge($identifier, $array);
return $array;
}
public function hasChanges()
{
return !empty($this->_changes) ? true:false;
}
2007-09-22 01:32:48 +00:00
public function save()
{
2007-09-24 18:22:52 +00:00
$format = $this->getConfig('format');
2007-09-24 04:58:57 +00:00
$request = new Doctrine_Resource_Request();
$request->set('format', $format);
$request->set('type', 'save');
$request->set('model', $this->getModel());
$request->set('data', $this->getChanges());
2007-09-24 21:46:05 +00:00
$request->set('identifier', $this->identifier());
2007-09-21 18:01:08 +00:00
2007-09-24 04:58:57 +00:00
$response = $request->execute();
2007-09-21 18:01:08 +00:00
2007-09-24 04:58:57 +00:00
$array = Doctrine_Parser::load($response, $format);
2007-09-21 18:19:19 +00:00
2007-09-24 04:58:57 +00:00
$this->_data = $request->hydrate(array($array), $this->_model)->getFirst()->_data;
}
2007-09-24 21:46:05 +00:00
public function delete()
{
$format = $this->getConfig('format');
$request = new Doctrine_Resource_Request();
$request->set('format', $format);
$request->set('type', 'delete');
$request->set('model', $this->getModel());
$request->set('identifier', $this->identifier());
$response = $request->execute();
}
2007-09-24 22:19:44 +00:00
public function getTable()
{
return $this->_table;
}
2007-09-24 04:58:57 +00:00
public function getModel()
{
return $this->_model;
2007-09-21 18:01:08 +00:00
}
2007-09-24 18:22:52 +00:00
public function identifier()
{
$identifier = array();
2007-09-24 22:19:44 +00:00
$schema = $this->getTable()->getSchema();
$columns = $schema['columns'];
if (isset($columns) && is_array($columns)) {
foreach ($columns as $name => $column) {
2007-09-24 18:22:52 +00:00
if ($column['primary'] == true) {
$identifier[$name] = $this->_data[$name];
}
}
}
return $identifier;
}
public function exists()
{
$identifier = $this->identifier();
foreach ($identifier as $key => $value) {
if (!$value) {
return false;
}
}
return true;
}
2007-09-21 18:01:08 +00:00
public function toArray()
{
$array = array();
2007-09-24 04:58:57 +00:00
foreach ($this->_data as $key => $value) {
2007-09-24 18:22:52 +00:00
2007-09-24 22:19:44 +00:00
if ($this->getTable()->hasRelation($key) && $value instanceof Doctrine_Resource_Collection) {
2007-09-24 18:22:52 +00:00
if ($value->count() > 0) {
$array[$key] = $value->toArray();
}
2007-09-24 22:19:44 +00:00
} else if ($this->getTable()->hasRelation($key) && $value instanceof Doctrine_Resource_Record) {
if ($value->exists() || $value->hasChanges()) {
2007-09-24 18:22:52 +00:00
$array[$key] = $value->toArray();
}
2007-09-24 22:19:44 +00:00
} else if (!$this->getTable()->hasRelation($key) && $this->getTable()->hasColumn($key)) {
2007-09-21 18:01:08 +00:00
$array[$key] = $value;
}
}
return $array;
}
2007-09-22 22:02:58 +00:00
}