1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/lib/Doctrine/Resource/Record.php

281 lines
8.1 KiB
PHP
Raw Normal View History

2007-09-21 22:01:08 +04:00
<?php
2007-09-23 02:02:58 +04: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
*
* @package Doctrine
* @subpackage Resource
* @author Jonathan H. Wage <jwage@mac.com>
2007-09-23 02:02:58 +04:00
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @link www.phpdoctrine.com
* @since 1.0
*/
2007-09-24 22:22:52 +04:00
class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate
2007-09-21 22:01:08 +04:00
{
2007-09-24 08:58:57 +04:00
protected $_data = array();
protected $_model = null;
protected $_changes = array();
2007-09-21 22:01:08 +04:00
public function __construct($model)
2007-09-21 22:19:19 +04:00
{
2007-09-24 08:58:57 +04:00
$this->_model = $model;
$this->initialize();
2007-09-24 08:58:57 +04:00
}
public function clearChanges()
{
$this->_changes = array();
}
2007-09-24 08:58:57 +04:00
public function initialize($loadRelations = true)
{
2007-09-25 02:19:44 +04:00
$schema = $this->getTable()->getSchema();
$relations = $schema['relations'];
2007-09-24 08:58:57 +04:00
if (isset($schema['columns'])) {
$columns = $schema['columns'];
foreach ($columns as $column) {
if ( ! isset($this->_data[$column['name']]) || $this->_data[$column['name']]) {
2007-09-24 08:58:57 +04:00
$this->_data[$column['name']] = null;
}
}
}
}
public function getConfig($key = null)
{
return Doctrine_Resource_Client::getInstance()->getConfig($key);
2007-09-21 22:19:19 +04:00
}
public function get($key)
2007-09-21 22:01:08 +04:00
{
if ( ! isset($key)) {
return;
}
if ( ! isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
$this->_data[$key] = $this->createRelation($key);
}
if ( ! array_key_exists($key, $this->_data)) {
throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
}
return $this->_data[$key];
2007-09-21 22:01:08 +04:00
}
public function set($key, $value)
2007-09-21 22:01:08 +04:00
{
if ( ! isset($key)) {
return;
}
if ( ! isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
$this->_data[$key] = $this->createRelation($key);
}
if ( ! array_key_exists($key, $this->_data)) {
throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
}
if ($this->_data[$key] != $value && !$value instanceof Doctrine_Resource_Record && !$value instanceof Doctrine_Resource_Collection) {
$this->_changes[$key] = $value;
}
$this->_data[$key] = $value;
2007-09-21 22:01:08 +04:00
}
public function createRelation($key)
{
$relation = $this->getTable()->getRelation($key);
$class = $relation['class'];
if ($relation['type'] === Doctrine_Relation::ONE) {
$return = new $class();
} else {
$return = new Doctrine_Resource_Collection($class);
}
return $return;
}
2007-09-21 22:01:08 +04:00
public function count()
{
2007-09-24 08:58:57 +04:00
return count($this->_data);
2007-09-21 22:01:08 +04:00
}
public function getIterator()
{
2007-09-24 08:58:57 +04:00
return new ArrayIterator($this->_data);
2007-09-22 05:32:48 +04:00
}
public function getChanges()
{
global $gotten;
if ( ! $gotten) {
$gotten = array();
}
$md5Hash = $this->getMd5Hash();
if ( ! in_array($md5Hash, $gotten)) {
$gotten[] = $md5Hash;
}
$array = array();
foreach ($this->_data as $key => $value) {
2007-09-25 02:19:44 +04:00
if ($this->getTable()->hasRelation($key)) {
2007-09-25 02:19:44 +04:00
$relation = $this->getTable()->getRelation($key);
if ($value instanceof Doctrine_Resource_Record) {
if ($value->hasChanges() && !in_array($value->getMd5Hash(), $gotten)) {
$array[$key] = $value->getChanges();
}
} else if($value instanceof Doctrine_Resource_Collection) {
foreach ($value as $key2 => $record) {
if ($record->hasChanges() && !in_array($record->getMd5Hash(), $gotten)) {
$array[$key][$record->getModel() . '_' .$key2] = $record->getChanges();
}
}
}
2007-09-25 02:19:44 +04: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 05:32:48 +04:00
public function save()
{
2007-09-24 08:58:57 +04:00
$request = new Doctrine_Resource_Request();
$request->set('action', 'save');
2007-09-24 08:58:57 +04:00
$request->set('model', $this->getModel());
2007-09-25 01:46:05 +04:00
$request->set('identifier', $this->identifier());
$request->set('data', $this->getChanges());
2007-09-21 22:01:08 +04:00
2007-09-24 08:58:57 +04:00
$response = $request->execute();
2007-09-21 22:01:08 +04:00
$this->fromArray($response);
2007-09-24 08:58:57 +04:00
}
2007-09-25 01:46:05 +04:00
public function delete()
{
$request = new Doctrine_Resource_Request();
$request->set('action', 'delete');
2007-09-25 01:46:05 +04:00
$request->set('model', $this->getModel());
$request->set('identifier', $this->identifier());
$response = $request->execute();
}
2007-09-25 02:19:44 +04:00
public function getTable()
{
$model = $this->_model;
return Doctrine_Resource_Client::getInstance()->getTable($model);
2007-09-25 02:19:44 +04:00
}
2007-09-24 08:58:57 +04:00
public function getModel()
{
return $this->_model;
2007-09-21 22:01:08 +04:00
}
2007-09-24 22:22:52 +04:00
public function identifier()
{
$identifier = array();
2007-09-25 02:19:44 +04:00
$schema = $this->getTable()->getSchema();
$columns = $schema['columns'];
if (isset($columns) && is_array($columns)) {
foreach ($columns as $name => $column) {
2007-09-24 22:22:52 +04: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) {
2007-09-24 22:22:52 +04:00
return false;
}
}
return true;
}
public function toArray($deep = false)
2007-09-21 22:01:08 +04:00
{
$data = array();
2007-09-21 22:01:08 +04:00
2007-09-24 08:58:57 +04:00
foreach ($this->_data as $key => $value) {
if ($deep && $this->getTable()->hasRelation($key)) {
$data[$key] = $this->$key->toArray($deep);
} else if ($this->getTable()->hasColumn($key)) {
$data[$key] = $value;
2007-09-21 22:01:08 +04:00
}
}
return $data;
}
public function fromArray(array $array)
{
foreach ($array as $key => $value) {
if ($this->getTable()->hasRelation($key) && is_array($value)) {
$this->$key->fromArray($value);
} else if ($this->getTable()->hasColumn($key)) {
$this->$key = $value;
}
}
2007-09-21 22:01:08 +04:00
}
public function getMd5Hash()
{
return md5(serialize($this->_data));
}
}