2007-01-19 23:47:24 +03:00
|
|
|
<?php
|
|
|
|
/*
|
2008-05-14 01:20:34 +04:00
|
|
|
* $Id: Record.php 4342 2008-05-08 14:17:35Z romanb $
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* 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
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-05-01 13:41:47 +04:00
|
|
|
|
2008-05-30 16:09:24 +04:00
|
|
|
#namespace Doctrine::ORM;
|
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Base class for all Entities (objects with persistent state in a RDBMS that are
|
|
|
|
* managed by Doctrine).
|
2008-06-15 19:56:28 +04:00
|
|
|
*
|
|
|
|
* NOTE: Methods that are intended for internal use only but must be public
|
|
|
|
* are marked INTERNAL: and begin with an underscore "_" to indicate that they
|
|
|
|
* ideally would not be public and to minimize naming collisions.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2008-02-04 00:29:57 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2007-01-19 23:47:24 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2008-05-14 01:20:34 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 4342 $
|
2008-07-21 00:13:24 +04:00
|
|
|
* @todo Split up into "Entity" and "ActiveEntity" (extends Entity).
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
abstract class Doctrine_Entity extends Doctrine_Access implements Serializable
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* MANAGED
|
|
|
|
* An Entity is in managed state when it has a primary key/identifier and is
|
|
|
|
* managed by an EntityManager (registered in the identity map).
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
const STATE_MANAGED = 1;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* NEW
|
|
|
|
* An Entity is new if it does not yet have an identifier/primary key
|
|
|
|
* and is not (yet) managed by an EntityManager.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
const STATE_NEW = 2;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2007-07-07 01:18:36 +04:00
|
|
|
* LOCKED STATE
|
2008-06-15 19:56:28 +04:00
|
|
|
* An Entity is temporarily locked during deletes and saves.
|
2007-07-21 00:41:13 +04:00
|
|
|
*
|
|
|
|
* This state is used internally to ensure that circular deletes
|
2008-06-15 19:56:28 +04:00
|
|
|
* and saves will not cause infinite loops.
|
2008-07-10 21:17:58 +04:00
|
|
|
* @todo Not sure this is a good idea. It is a problematic solution because
|
|
|
|
* it hides the original state while the locked state is active.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
const STATE_LOCKED = 6;
|
2008-02-24 01:04:39 +03:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
|
|
|
* A detached Entity is an instance with a persistent identity that is not
|
|
|
|
* (or no longer) associated with an EntityManager (and a UnitOfWork).
|
|
|
|
* This means its no longer in the identity map.
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
const STATE_DETACHED = 3;
|
2008-07-10 21:17:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A removed Entity instance is an instance with a persistent identity,
|
2008-07-27 23:38:56 +04:00
|
|
|
* associated with an EntityManager, whose persistent state has been
|
|
|
|
* deleted (or is scheduled for deletion).
|
2008-07-10 21:17:58 +04:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
const STATE_DELETED = 4;
|
2008-07-10 21:17:58 +04:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
|
|
|
* Index used for creating object identifiers (oid's).
|
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* @var integer $index
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
|
|
|
private static $_index = 1;
|
|
|
|
|
|
|
|
/**
|
2008-04-05 11:35:57 +04:00
|
|
|
* Boolean flag that indicates whether automatic accessor overriding is enabled.
|
2008-03-17 16:26:34 +03:00
|
|
|
*
|
|
|
|
* @var boolean
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
|
|
|
private static $_useAutoAccessorOverride;
|
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The accessor cache is used as a memory for the existance of custom accessors.
|
2008-03-17 16:26:34 +03:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
|
|
|
private static $_accessorCache = array();
|
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The mutator cache is used as a memory for the existance of custom mutators.
|
2008-03-17 16:26:34 +03:00
|
|
|
*
|
|
|
|
* @var array
|
2008-02-24 01:04:39 +03:00
|
|
|
*/
|
|
|
|
private static $_mutatorCache = array();
|
2008-03-17 16:26:34 +03:00
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The class descriptor.
|
2008-03-17 16:26:34 +03:00
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* @var Doctrine::ORM::ClassMetadata
|
2008-03-17 16:26:34 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_class;
|
2008-03-17 16:26:34 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
2008-05-14 01:20:34 +04:00
|
|
|
* The name of the Entity.
|
|
|
|
*
|
|
|
|
* @var string
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_entityName;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The values that make up the ID/primary key of the entity.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
|
|
|
* @var array
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_id = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The entity data.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
|
|
|
* @var array
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_data = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-02-04 00:29:57 +03:00
|
|
|
* The state of the object.
|
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @var integer
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_state;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-02-04 00:29:57 +03:00
|
|
|
* The names of fields that have been modified but not yet persisted.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Keys are field names, values oldValue => newValue tuples.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @var array
|
|
|
|
* @todo Rename to $_changeSet
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_modified = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-03-17 16:26:34 +03:00
|
|
|
* The references for all associations of the entity to other entities.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
2008-03-17 16:26:34 +03:00
|
|
|
* @var array
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_references = array();
|
2008-05-06 17:41:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The EntityManager that is responsible for the persistence of the entity.
|
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* @var Doctrine::ORM::EntityManager
|
2008-05-06 17:41:22 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private $_em;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* The object identifier of the object. Each object has a unique identifier
|
|
|
|
* during script execution.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
2008-03-17 16:26:34 +03:00
|
|
|
* @var integer
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2007-05-18 13:22:31 +04:00
|
|
|
private $_oid;
|
2007-01-19 23:47:24 +03:00
|
|
|
|
|
|
|
/**
|
2008-02-04 00:29:57 +03:00
|
|
|
* Constructor.
|
2008-07-21 00:13:24 +04:00
|
|
|
* Creates a new Entity instance.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
public function __construct()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_entityName = get_class($this);
|
2008-06-05 23:01:58 +04:00
|
|
|
$this->_em = Doctrine_EntityManagerFactory::getManager($this->_entityName);
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_class = $this->_em->getClassMetadata($this->_entityName);
|
|
|
|
$this->_oid = self::$_index++;
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_data = $this->_em->_getTmpEntityData();
|
|
|
|
if ($this->_data) {
|
|
|
|
$this->_extractIdentifier();
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_state = self::STATE_MANAGED;
|
2007-11-18 19:06:37 +03:00
|
|
|
} else {
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_state = self::STATE_NEW;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-02-24 01:04:39 +03:00
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
// @todo read from attribute the first time and move this initialization elsewhere.
|
|
|
|
self::$_useAutoAccessorOverride = true;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Returns the object identifier.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
2008-07-10 21:17:58 +04:00
|
|
|
final public function getOid()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-05-18 13:22:31 +04:00
|
|
|
return $this->_oid;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-16 23:19:29 +04:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* Hydrates this object from given array
|
2007-04-17 21:25:08 +04:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-07-10 21:17:58 +04:00
|
|
|
final public function hydrate(array $data)
|
2007-04-17 21:25:08 +04:00
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_data = array_merge($this->_data, $data);
|
2008-07-10 21:17:58 +04:00
|
|
|
$this->_extractIdentifier();
|
2007-04-17 21:25:08 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Copies the identifier names and values from _data into _id.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
private function _extractIdentifier()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
if ( ! $this->_class->isIdentifierComposite()) {
|
|
|
|
// Single field identifier
|
|
|
|
$name = $this->_class->getIdentifier();
|
|
|
|
$name = $name[0];
|
|
|
|
if (isset($this->_data[$name]) && $this->_data[$name] !== Doctrine_Null::$INSTANCE) {
|
|
|
|
$this->_id[$name] = $this->_data[$name];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Composite identifier
|
|
|
|
$names = $this->_class->getIdentifier();
|
|
|
|
foreach ($names as $name) {
|
|
|
|
if ($this->_data[$name] === Doctrine_Null::$INSTANCE) {
|
|
|
|
$this->_id[$name] = null;
|
|
|
|
} else {
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_id[$name] = $this->_data[$name];
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
}
|
2007-01-23 19:27:20 +03:00
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
|
|
|
*/
|
2008-07-27 23:38:56 +04:00
|
|
|
/*final public function _setIdentifier(array $identifier)
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
|
|
|
$this->_id = $identifier;
|
2008-07-27 23:38:56 +04:00
|
|
|
}*/
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Serializes the entity.
|
|
|
|
* This method is automatically called when the entity is serialized.
|
|
|
|
*
|
|
|
|
* Part of the implementation of the Serializable interface.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function serialize()
|
|
|
|
{
|
2008-07-10 21:17:58 +04:00
|
|
|
//$this->_em->getEventManager()->dispatchEvent(Event::preSerialize);
|
|
|
|
//$this->_class->dispatchLifecycleEvent(Event::preSerialize, $this);
|
2007-01-19 23:47:24 +03:00
|
|
|
|
|
|
|
$vars = get_object_vars($this);
|
|
|
|
|
2007-05-22 21:42:47 +04:00
|
|
|
unset($vars['_references']);
|
2008-05-06 17:41:22 +04:00
|
|
|
unset($vars['_em']);
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2008-02-24 23:31:49 +03:00
|
|
|
//$name = (array)$this->_table->getIdentifier();
|
2007-01-19 23:47:24 +03:00
|
|
|
$this->_data = array_merge($this->_data, $this->_id);
|
|
|
|
|
|
|
|
foreach ($this->_data as $k => $v) {
|
2008-07-11 14:48:04 +04:00
|
|
|
if ($v instanceof Doctrine_Entity && $this->_class->getTypeOfField($k) != 'object') {
|
2007-01-19 23:47:24 +03:00
|
|
|
unset($vars['_data'][$k]);
|
2008-03-19 21:33:14 +03:00
|
|
|
} else if ($v === Doctrine_Null::$INSTANCE) {
|
2007-01-19 23:47:24 +03:00
|
|
|
unset($vars['_data'][$k]);
|
|
|
|
} else {
|
2008-07-11 14:48:04 +04:00
|
|
|
switch ($this->_class->getTypeOfField($k)) {
|
2007-05-08 02:46:47 +04:00
|
|
|
case 'array':
|
|
|
|
case 'object':
|
2007-01-19 23:47:24 +03:00
|
|
|
$vars['_data'][$k] = serialize($vars['_data'][$k]);
|
|
|
|
break;
|
2007-05-08 02:46:47 +04:00
|
|
|
case 'gzip':
|
|
|
|
$vars['_data'][$k] = gzcompress($vars['_data'][$k]);
|
|
|
|
break;
|
|
|
|
case 'enum':
|
2008-03-17 16:26:34 +03:00
|
|
|
$vars['_data'][$k] = $this->_class->enumIndex($k, $vars['_data'][$k]);
|
2007-05-08 02:46:47 +04:00
|
|
|
break;
|
2007-01-23 19:27:20 +03:00
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-06-25 21:24:20 +04:00
|
|
|
$str = serialize($vars);
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2008-06-05 23:01:58 +04:00
|
|
|
//$this->postSerialize($event);
|
2007-06-25 21:24:20 +04:00
|
|
|
|
|
|
|
return $str;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Reconstructs the entity from it's serialized form.
|
|
|
|
* This method is automatically called everytime the entity is unserialized.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @param string $serialized Doctrine_Entity as serialized string
|
2007-01-19 23:47:24 +03:00
|
|
|
* @throws Doctrine_Record_Exception if the cleanData operation fails somehow
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
2008-06-05 23:01:58 +04:00
|
|
|
//$event = new Doctrine_Event($this, Doctrine_Event::RECORD_UNSERIALIZE);
|
|
|
|
//$this->preUnserialize($event);
|
2007-07-12 02:03:47 +04:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
$this->_entityName = get_class($this);
|
2008-06-05 23:01:58 +04:00
|
|
|
$manager = Doctrine_EntityManagerFactory::getManager($this->_entityName);
|
2008-05-17 16:22:24 +04:00
|
|
|
$connection = $manager->getConnection();
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2007-05-22 21:42:47 +04:00
|
|
|
$this->_oid = self::$_index;
|
|
|
|
self::$_index++;
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
$this->_em = $manager;
|
2007-01-19 23:47:24 +03:00
|
|
|
|
|
|
|
$array = unserialize($serialized);
|
|
|
|
|
2007-05-22 21:42:47 +04:00
|
|
|
foreach($array as $k => $v) {
|
|
|
|
$this->$k = $v;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_class = $this->_em->getClassMetadata($this->_entityName);
|
2007-05-22 22:09:54 +04:00
|
|
|
|
2007-08-07 19:37:30 +04:00
|
|
|
foreach ($this->_data as $k => $v) {
|
2008-07-11 14:48:04 +04:00
|
|
|
switch ($this->_class->getTypeOfField($k)) {
|
2007-08-07 19:37:30 +04:00
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
$this->_data[$k] = unserialize($this->_data[$k]);
|
|
|
|
break;
|
|
|
|
case 'gzip':
|
|
|
|
$this->_data[$k] = gzuncompress($this->_data[$k]);
|
|
|
|
break;
|
|
|
|
case 'enum':
|
2008-03-17 16:26:34 +03:00
|
|
|
$this->_data[$k] = $this->_class->enumValue($k, $this->_data[$k]);
|
2007-08-07 19:37:30 +04:00
|
|
|
break;
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2007-08-07 19:37:30 +04:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_extractIdentifier(!$this->isNew());
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2008-06-05 23:01:58 +04:00
|
|
|
//$this->postUnserialize($event);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
2008-07-10 21:17:58 +04:00
|
|
|
* Gets or sets the state of this Entity.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param integer|string $state if set, this method tries to set the record state to $state
|
2008-05-14 01:20:34 +04:00
|
|
|
* @see Doctrine_Entity::STATE_* constants
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @throws Doctrine_Record_State_Exception if trying to set an unknown state
|
|
|
|
* @return null|integer
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _state($state = null)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
|
|
|
if ($state == null) {
|
|
|
|
return $this->_state;
|
|
|
|
}
|
2008-06-15 19:56:28 +04:00
|
|
|
|
|
|
|
/* TODO: Do we really need this check? This is only for internal use after all. */
|
|
|
|
switch ($state) {
|
2008-07-21 00:13:24 +04:00
|
|
|
case self::STATE_MANAGED:
|
|
|
|
case self::STATE_DELETED:
|
|
|
|
case self::STATE_DETACHED:
|
|
|
|
case self::STATE_NEW:
|
2008-06-15 19:56:28 +04:00
|
|
|
case self::STATE_LOCKED:
|
2007-01-19 23:47:24 +03:00
|
|
|
$this->_state = $state;
|
2008-06-15 19:56:28 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Doctrine_Entity_Exception::invalidState($state);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-27 23:38:56 +04:00
|
|
|
* Refresh internal data from the database
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2007-11-28 22:35:44 +03:00
|
|
|
* @param bool $deep If true, fetch also current relations. Caution: this deletes
|
2007-12-11 14:10:27 +03:00
|
|
|
* any aggregated values you may have queried beforee
|
2007-11-28 22:35:44 +03:00
|
|
|
*
|
2007-01-19 23:47:24 +03:00
|
|
|
* @throws Doctrine_Record_Exception When the refresh operation fails (when the database row
|
|
|
|
* this record represents does not exist anymore)
|
|
|
|
* @return boolean
|
2008-05-14 01:20:34 +04:00
|
|
|
* @todo Implementation to EntityManager.
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Move to ActiveEntity (extends Entity). Implementation to EntityManager.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2007-11-28 22:35:44 +03:00
|
|
|
public function refresh($deep = false)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-07-06 03:21:29 +04:00
|
|
|
$id = $this->identifier();
|
2007-01-19 23:47:24 +03:00
|
|
|
if ( ! is_array($id)) {
|
|
|
|
$id = array($id);
|
|
|
|
}
|
|
|
|
if (empty($id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$id = array_values($id);
|
|
|
|
|
2007-11-28 22:35:44 +03:00
|
|
|
if ($deep) {
|
2008-06-15 19:56:28 +04:00
|
|
|
$query = $this->_em->createQuery()->from($this->_entityName);
|
2007-11-28 22:35:44 +03:00
|
|
|
foreach (array_keys($this->_references) as $name) {
|
|
|
|
$query->leftJoin(get_class($this) . '.' . $name);
|
|
|
|
}
|
2008-03-17 16:26:34 +03:00
|
|
|
$query->where(implode(' = ? AND ', $this->_class->getIdentifierColumnNames()) . ' = ?');
|
2008-03-05 14:24:33 +03:00
|
|
|
$this->clearRelated();
|
2007-11-28 22:35:44 +03:00
|
|
|
$record = $query->fetchOne($id);
|
|
|
|
} else {
|
|
|
|
// Use FETCH_ARRAY to avoid clearing object relations
|
2008-05-06 17:41:22 +04:00
|
|
|
$record = $this->getRepository()->find($this->identifier(), Doctrine::HYDRATE_ARRAY);
|
2007-11-28 22:35:44 +03:00
|
|
|
if ($record) {
|
|
|
|
$this->hydrate($record);
|
|
|
|
}
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2007-09-04 05:23:11 +04:00
|
|
|
if ($record === false) {
|
2007-07-01 15:27:45 +04:00
|
|
|
throw new Doctrine_Record_Exception('Failed to refresh. Record does not exist.');
|
2007-05-22 20:58:34 +04:00
|
|
|
}
|
2007-05-22 21:42:47 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
$this->_modified = array();
|
|
|
|
|
2008-02-20 23:54:20 +03:00
|
|
|
$this->_extractIdentifier();
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
$this->_state = Doctrine_Entity::STATE_CLEAN;
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2007-07-06 00:03:38 +04:00
|
|
|
return $this;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Gets the current field values.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2008-06-15 19:56:28 +04:00
|
|
|
* @return array The fields and their values.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function getData()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
|
|
|
return $this->_data;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* INTERNAL: (Usage from within extending classes is intended)
|
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* Gets the value of a field (regular field or reference).
|
2008-07-10 21:17:58 +04:00
|
|
|
* If the field is not yet loaded this method does NOT load it.
|
2008-05-14 01:20:34 +04:00
|
|
|
*
|
|
|
|
* NOTE: Use of this method from outside the scope of an extending class
|
|
|
|
* is strongly discouraged.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param $name name of the property
|
2008-05-14 01:20:34 +04:00
|
|
|
* @throws Doctrine_Entity_Exception if trying to get an unknown field
|
2007-01-19 23:47:24 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-07-27 23:38:56 +04:00
|
|
|
final public function _get($fieldName)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-05-14 01:20:34 +04:00
|
|
|
if (isset($this->_data[$fieldName])) {
|
2008-06-15 19:56:28 +04:00
|
|
|
return $this->_rawGetField($fieldName);
|
2008-05-14 01:20:34 +04:00
|
|
|
} else if (isset($this->_references[$fieldName])) {
|
2008-06-15 19:56:28 +04:00
|
|
|
return $this->_rawGetReference($fieldName);
|
2008-05-14 01:20:34 +04:00
|
|
|
} else {
|
|
|
|
throw Doctrine_Entity_Exception::unknownField($fieldName);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* INTERNAL: (Usage from within extending classes is intended)
|
|
|
|
*
|
2008-06-15 19:56:28 +04:00
|
|
|
* Sets the value of a field (regular field or reference).
|
|
|
|
* If the field is not yet loaded this method does NOT load it.
|
|
|
|
*
|
|
|
|
* NOTE: Use of this method from outside the scope of an extending class
|
|
|
|
* is strongly discouraged.
|
|
|
|
*
|
|
|
|
* @param $name name of the field
|
|
|
|
* @throws Doctrine_Entity_Exception if trying to get an unknown field
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-07-27 23:38:56 +04:00
|
|
|
final public function _set($fieldName, $value)
|
2008-06-15 19:56:28 +04:00
|
|
|
{
|
|
|
|
if (isset($this->_data[$fieldName])) {
|
|
|
|
return $this->_rawSetField($fieldName, $value);
|
|
|
|
} else if (isset($this->_references[$fieldName])) {
|
|
|
|
return $this->_rawSetReference($fieldName, $value);
|
|
|
|
} else {
|
|
|
|
throw Doctrine_Entity_Exception::unknownField($fieldName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* INTERNAL:
|
2008-05-14 01:20:34 +04:00
|
|
|
* Gets the value of a field.
|
|
|
|
*
|
|
|
|
* NOTE: Use of this method from outside the scope of an extending class
|
|
|
|
* is strongly discouraged. This method does NOT check whether the field
|
2008-07-27 23:38:56 +04:00
|
|
|
* exists. _get() in extending classes should be preferred.
|
2008-05-14 01:20:34 +04:00
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return mixed
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo Rename to _unsafeGetField()
|
2008-05-14 01:20:34 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _rawGetField($fieldName)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
2008-03-19 21:33:14 +03:00
|
|
|
if ($this->_data[$fieldName] === Doctrine_Null::$INSTANCE) {
|
2007-01-19 23:47:24 +03:00
|
|
|
return null;
|
2007-11-18 19:06:37 +03:00
|
|
|
}
|
|
|
|
return $this->_data[$fieldName];
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-05-14 01:20:34 +04:00
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
2008-05-14 01:20:34 +04:00
|
|
|
* Sets the value of a field.
|
|
|
|
*
|
|
|
|
* NOTE: Use of this method from outside the scope of an extending class
|
|
|
|
* is strongly discouraged. This method does NOT check whether the field
|
2008-07-27 23:38:56 +04:00
|
|
|
* exists. _set() in extending classes should be preferred.
|
2008-05-14 01:20:34 +04:00
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @param mixed $value
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo Rename to _unsafeSetField()
|
2008-05-14 01:20:34 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _rawSetField($fieldName, $value)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
|
|
|
$this->_data[$fieldName] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a reference to another Entity.
|
|
|
|
*
|
|
|
|
* NOTE: Use of this method from outside the scope of an extending class
|
|
|
|
* is strongly discouraged. This method does NOT check whether the reference
|
|
|
|
* exists.
|
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* @param string $fieldName
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo Rename to _unsafeGetReference().
|
2008-05-14 01:20:34 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _rawGetReference($fieldName)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
|
|
|
if ($this->_references[$fieldName] === Doctrine_Null::$INSTANCE) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->_references[$fieldName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
2008-05-14 01:20:34 +04:00
|
|
|
* Sets a reference to another Entity.
|
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* NOTE: Use of this method is strongly discouraged for user-code.
|
2008-05-14 01:20:34 +04:00
|
|
|
*
|
2008-06-15 19:56:28 +04:00
|
|
|
* @param string $fieldName
|
|
|
|
* @param mixed $value
|
2008-05-14 01:20:34 +04:00
|
|
|
* @todo Refactor. What about composite keys?
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo Rename to _unsafeSetReference()
|
2008-05-14 01:20:34 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _rawSetReference($name, $value)
|
2008-05-14 01:20:34 +04:00
|
|
|
{
|
|
|
|
if ($value === Doctrine_Null::$INSTANCE) {
|
|
|
|
$this->_references[$name] = $value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rel = $this->_class->getRelation($name);
|
|
|
|
|
|
|
|
// one-to-many or one-to-one relation
|
|
|
|
if ($rel instanceof Doctrine_Relation_ForeignKey ||
|
|
|
|
$rel instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
if ( ! $rel->isOneToOne()) {
|
|
|
|
// one-to-many relation found
|
|
|
|
if ( ! $value instanceof Doctrine_Collection) {
|
|
|
|
throw Doctrine_Entity_Exception::invalidValueForOneToManyReference();
|
|
|
|
}
|
|
|
|
if (isset($this->_references[$name])) {
|
|
|
|
$this->_references[$name]->setData($value->getData());
|
2008-07-10 21:17:58 +04:00
|
|
|
return;
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$relatedTable = $value->getTable();
|
|
|
|
$foreignFieldName = $rel->getForeignFieldName();
|
|
|
|
$localFieldName = $rel->getLocalFieldName();
|
|
|
|
|
|
|
|
// one-to-one relation found
|
|
|
|
if ( ! ($value instanceof Doctrine_Entity)) {
|
|
|
|
throw Doctrine_Entity_Exception::invalidValueForOneToOneReference();
|
|
|
|
}
|
|
|
|
if ($rel instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$idFieldNames = $value->getTable()->getIdentifier();
|
|
|
|
if ( ! empty($foreignFieldName) && $foreignFieldName != $idFieldNames[0]) {
|
2008-07-27 23:38:56 +04:00
|
|
|
$this->set($localFieldName, $value->_get($foreignFieldName));
|
2008-05-14 01:20:34 +04:00
|
|
|
} else {
|
2008-07-10 21:17:58 +04:00
|
|
|
$this->set($localFieldName, $value);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
} else {
|
2008-07-10 21:17:58 +04:00
|
|
|
$value->set($foreignFieldName, $this);
|
2008-05-14 01:20:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($rel instanceof Doctrine_Relation_Association) {
|
|
|
|
if ( ! ($value instanceof Doctrine_Collection)) {
|
|
|
|
throw Doctrine_Entity_Exception::invalidValueForManyToManyReference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_references[$name] = $value;
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Generic getter.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param mixed $name name of the property or related component
|
2007-06-05 02:38:39 +04:00
|
|
|
* @param boolean $load whether or not to invoke the loading procedure
|
2007-01-19 23:47:24 +03:00
|
|
|
* @throws Doctrine_Record_Exception if trying to get a value of unknown property / related component
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function get($fieldName)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-05-26 00:57:32 +04:00
|
|
|
if ($getter = $this->_getCustomAccessor($fieldName)) {
|
|
|
|
return $this->$getter();
|
|
|
|
}
|
2008-02-24 01:04:39 +03:00
|
|
|
|
2008-05-17 16:22:24 +04:00
|
|
|
// Use built-in accessor functionality
|
2008-03-19 21:33:14 +03:00
|
|
|
$nullObj = Doctrine_Null::$INSTANCE;
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_data[$fieldName])) {
|
2008-05-17 16:22:24 +04:00
|
|
|
return $this->_data[$fieldName] !== $nullObj ?
|
|
|
|
$this->_data[$fieldName] : null;
|
|
|
|
} else if (isset($this->_references[$fieldName])) {
|
|
|
|
return $this->_references[$fieldName] !== $nullObj ?
|
|
|
|
$this->_references[$fieldName] : null;
|
|
|
|
} else {
|
2008-06-15 19:56:28 +04:00
|
|
|
$class = $this->_class;
|
|
|
|
if ($class->hasField($fieldName)) {
|
|
|
|
return null;
|
|
|
|
} else if ($class->hasRelation($fieldName)) {
|
|
|
|
$rel = $class->getRelation($fieldName);
|
|
|
|
if ($rel->isLazilyLoaded()) {
|
|
|
|
$this->_references[$fieldName] = $rel->lazyLoadFor($this);
|
2008-05-17 16:22:24 +04:00
|
|
|
return $this->_references[$fieldName] !== $nullObj ?
|
|
|
|
$this->_references[$fieldName] : null;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw Doctrine_Entity_Exception::invalidField($fieldName);
|
2007-09-21 17:13:43 +04:00
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2008-02-28 18:30:55 +03:00
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
|
|
|
* Gets the custom mutator method for a field, if it exists.
|
|
|
|
*
|
|
|
|
* @param string $fieldName The field name.
|
|
|
|
* @return mixed The name of the custom mutator or FALSE, if the field does
|
|
|
|
* not have a custom mutator.
|
|
|
|
*/
|
2008-05-26 00:57:32 +04:00
|
|
|
private function _getCustomMutator($fieldName)
|
|
|
|
{
|
|
|
|
if ( ! isset(self::$_mutatorCache[$this->_entityName][$fieldName])) {
|
|
|
|
if (self::$_useAutoAccessorOverride) {
|
|
|
|
$setterMethod = 'set' . Doctrine::classify($fieldName);
|
|
|
|
if (method_exists($this, $setterMethod)) {
|
|
|
|
self::$_mutatorCache[$this->_entityName][$fieldName] = $setterMethod;
|
|
|
|
} else {
|
|
|
|
self::$_mutatorCache[$this->_entityName][$fieldName] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($setter = $this->_class->getCustomMutator($fieldName)) {
|
|
|
|
self::$_mutatorCache[$this->_entityName][$fieldName] = $setter;
|
|
|
|
} else if ( ! isset(self::$_mutatorCache[$this->_entityName][$fieldName])) {
|
|
|
|
self::$_mutatorCache[$this->_entityName][$fieldName] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$_mutatorCache[$this->_entityName][$fieldName];
|
|
|
|
}
|
|
|
|
|
2008-07-10 21:17:58 +04:00
|
|
|
/**
|
|
|
|
* Gets the custom accessor method of a field, if it exists.
|
|
|
|
*
|
|
|
|
* @param string $fieldName The field name.
|
|
|
|
* @return mixed The name of the custom accessor method, or FALSE if the
|
|
|
|
* field does not have a custom accessor.
|
|
|
|
*/
|
2008-05-26 00:57:32 +04:00
|
|
|
private function _getCustomAccessor($fieldName)
|
2008-05-24 21:10:45 +04:00
|
|
|
{
|
|
|
|
if ( ! isset(self::$_accessorCache[$this->_entityName][$fieldName])) {
|
|
|
|
if (self::$_useAutoAccessorOverride) {
|
|
|
|
$getterMethod = 'get' . Doctrine::classify($fieldName);
|
|
|
|
if (method_exists($this, $getterMethod)) {
|
|
|
|
self::$_accessorCache[$this->_entityName][$fieldName] = $getterMethod;
|
|
|
|
} else {
|
|
|
|
self::$_accessorCache[$this->_entityName][$fieldName] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($getter = $this->_class->getCustomAccessor($fieldName)) {
|
|
|
|
self::$_accessorCache[$this->_entityName][$fieldName] = $getter;
|
|
|
|
} else if ( ! isset(self::$_accessorCache[$this->_entityName][$fieldName])) {
|
|
|
|
self::$_accessorCache[$this->_entityName][$fieldName] = false;
|
|
|
|
}
|
|
|
|
}
|
2008-05-26 00:57:32 +04:00
|
|
|
|
|
|
|
return self::$_accessorCache[$this->_entityName][$fieldName];
|
2008-05-24 21:10:45 +04:00
|
|
|
}
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
/**
|
|
|
|
* Gets the entity class name.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
final public function getClassName()
|
2008-02-28 18:30:55 +03:00
|
|
|
{
|
|
|
|
return $this->_entityName;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Generic setter.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param mixed $name name of the property or reference
|
|
|
|
* @param mixed $value value of the property or reference
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function set($fieldName, $value)
|
2008-05-26 00:57:32 +04:00
|
|
|
{
|
|
|
|
if ($setter = $this->_getCustomMutator($fieldName)) {
|
|
|
|
return $this->$setter($value);
|
|
|
|
}
|
|
|
|
|
2008-06-16 22:31:21 +04:00
|
|
|
if ($this->_class->hasField($fieldName)) {
|
|
|
|
/*if ($value instanceof Doctrine_Entity) {
|
2008-06-15 19:56:28 +04:00
|
|
|
$type = $class->getTypeOf($fieldName);
|
2008-05-06 17:41:22 +04:00
|
|
|
// FIXME: composite key support
|
|
|
|
$ids = $value->identifier();
|
|
|
|
$id = count($ids) > 0 ? array_pop($ids) : null;
|
2007-08-14 02:01:27 +04:00
|
|
|
if ($id !== null && $type !== 'object') {
|
2007-01-19 23:47:24 +03:00
|
|
|
$value = $id;
|
2007-01-23 19:27:20 +03:00
|
|
|
}
|
2008-06-16 22:31:21 +04:00
|
|
|
}*/
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
$old = isset($this->_data[$fieldName]) ? $this->_data[$fieldName] : null;
|
2008-07-21 00:13:24 +04:00
|
|
|
//FIXME: null == 0 => true
|
2008-06-16 22:31:21 +04:00
|
|
|
if ($old != $value) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$this->_data[$fieldName] = $value;
|
2008-07-10 21:17:58 +04:00
|
|
|
$this->_modified[$fieldName] = array($old => $value);
|
|
|
|
if ($this->isNew() && $this->_class->isIdentifier($fieldName)) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_id[$fieldName] = $value;
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-06-16 22:31:21 +04:00
|
|
|
} else if ($this->_class->hasRelation($fieldName)) {
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_rawSetReference($fieldName, $value);
|
2007-01-19 23:47:24 +03:00
|
|
|
} else {
|
2008-05-17 16:22:24 +04:00
|
|
|
throw Doctrine_Entity_Exception::invalidField($fieldName);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Checks whether a field is set (not null).
|
|
|
|
*
|
|
|
|
* NOTE: Invoked by Doctrine::ORM::Access#__isset().
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function contains($fieldName)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_data[$fieldName])) {
|
2008-05-08 18:17:35 +04:00
|
|
|
if ($this->_data[$fieldName] === Doctrine_Null::$INSTANCE) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_id[$fieldName])) {
|
2007-01-19 23:47:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
2007-12-11 14:10:27 +03:00
|
|
|
if (isset($this->_references[$fieldName]) &&
|
2008-03-19 21:33:14 +03:00
|
|
|
$this->_references[$fieldName] !== Doctrine_Null::$INSTANCE) {
|
2007-01-19 23:47:24 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Clears the value of a field.
|
|
|
|
*
|
|
|
|
* NOTE: Invoked by Doctrine::ORM::Access#__unset().
|
|
|
|
*
|
2007-01-19 23:47:24 +03:00
|
|
|
* @param string $name
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function remove($fieldName)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
if (isset($this->_data[$fieldName])) {
|
|
|
|
$this->_data[$fieldName] = array();
|
2007-12-09 20:46:28 +03:00
|
|
|
} else if (isset($this->_references[$fieldName])) {
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($this->_references[$fieldName] instanceof Doctrine_Entity) {
|
2007-12-09 04:56:53 +03:00
|
|
|
// todo: delete related record when saving $this
|
2008-03-19 21:33:14 +03:00
|
|
|
$this->_references[$fieldName] = Doctrine_Null::$INSTANCE;
|
2008-01-05 22:55:56 +03:00
|
|
|
} else if ($this->_references[$fieldName] instanceof Doctrine_Collection) {
|
2007-12-09 04:56:53 +03:00
|
|
|
$this->_references[$fieldName]->setData(array());
|
|
|
|
}
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-03-17 16:26:34 +03:00
|
|
|
* Saves the current state of the entity into the database.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @param Doctrine_Connection $conn optional connection parameter
|
|
|
|
* @return void
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
public function save()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-04-13 00:11:11 +04:00
|
|
|
// TODO: Forward to EntityManager. There: registerNew() OR registerDirty() on UnitOfWork.
|
2008-06-15 19:56:28 +04:00
|
|
|
$this->_em->save($this);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
|
|
|
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
|
|
|
|
* query, except that if there is already a row in the table with the same
|
|
|
|
* key field values, the REPLACE query just updates its values instead of
|
|
|
|
* inserting a new row.
|
|
|
|
*
|
|
|
|
* The REPLACE type of query does not make part of the SQL standards. Since
|
|
|
|
* practically only MySQL and SQLIte implement it natively, this type of
|
|
|
|
* query isemulated through this method for other DBMS using standard types
|
|
|
|
* of queries inside a transaction to assure the atomicity of the operation.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Connection $conn optional connection parameter
|
|
|
|
* @throws Doctrine_Connection_Exception if some of the key values was null
|
|
|
|
* @throws Doctrine_Connection_Exception if there were no key fields
|
2007-11-10 16:21:40 +03:00
|
|
|
* @throws Doctrine_Connection_Exception if something fails at database level
|
2007-01-19 23:47:24 +03:00
|
|
|
* @return integer number of rows affected
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
public function replace()
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
return $this->_em->replace(
|
|
|
|
$this->_class,
|
|
|
|
$this->getPrepared(),
|
|
|
|
$this->_id);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-07-27 23:38:56 +04:00
|
|
|
* INTERNAL:
|
|
|
|
* Gets the changeset of the entities persistent state.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-07-27 23:38:56 +04:00
|
|
|
final public function _getChangeSet()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-07-27 23:38:56 +04:00
|
|
|
//return $this->_changeSet;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Returns an array of modified fields and values with data preparation
|
2007-01-19 23:47:24 +03:00
|
|
|
* adds column aggregation inheritance and converts Records into primary key values
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @return array
|
2007-11-18 19:06:37 +03:00
|
|
|
* @todo What about a little bit more expressive name? getPreparedData?
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo Does not look like the best place here ...
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Prop: Move to EntityPersister. There call _getChangeSet() and apply this logic.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function getPrepared(array $array = array())
|
2007-07-26 00:45:25 +04:00
|
|
|
{
|
2008-02-24 01:04:39 +03:00
|
|
|
$dataSet = array();
|
2007-01-19 23:47:24 +03:00
|
|
|
|
|
|
|
if (empty($array)) {
|
2007-11-18 19:06:37 +03:00
|
|
|
$modifiedFields = $this->_modified;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-07-26 00:45:25 +04:00
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
foreach ($modifiedFields as $field) {
|
2008-07-11 14:48:04 +04:00
|
|
|
$type = $this->_class->getTypeOfField($field);
|
2007-01-19 23:47:24 +03:00
|
|
|
|
2008-03-19 21:33:14 +03:00
|
|
|
if ($this->_data[$field] === Doctrine_Null::$INSTANCE) {
|
2008-02-24 01:04:39 +03:00
|
|
|
$dataSet[$field] = null;
|
2007-01-19 23:47:24 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
2008-02-24 01:04:39 +03:00
|
|
|
$dataSet[$field] = serialize($this->_data[$field]);
|
2007-01-19 23:47:24 +03:00
|
|
|
break;
|
|
|
|
case 'gzip':
|
2008-02-24 01:04:39 +03:00
|
|
|
$dataSet[$field] = gzcompress($this->_data[$field],5);
|
2007-01-19 23:47:24 +03:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
2008-06-15 19:56:28 +04:00
|
|
|
$dataSet[$field] = $this->_em->getConnection()
|
|
|
|
->convertBooleans($this->_data[$field]);
|
2007-01-19 23:47:24 +03:00
|
|
|
break;
|
|
|
|
case 'enum':
|
2008-03-17 16:26:34 +03:00
|
|
|
$dataSet[$field] = $this->_class->enumIndex($field, $this->_data[$field]);
|
2007-01-19 23:47:24 +03:00
|
|
|
break;
|
|
|
|
default:
|
2008-02-24 01:04:39 +03:00
|
|
|
$dataSet[$field] = $this->_data[$field];
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
|
|
|
|
// @todo cleanup
|
2008-02-08 01:21:18 +03:00
|
|
|
// populates the discriminator field in Single & Class Table Inheritance
|
2008-06-16 22:31:21 +04:00
|
|
|
if ($this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED ||
|
|
|
|
$this->_class->getInheritanceType() == Doctrine::INHERITANCE_TYPE_SINGLE_TABLE) {
|
|
|
|
$discCol = $this->_class->getInheritanceOption('discriminatorColumn');
|
|
|
|
$discMap = $this->_class->getInheritanceOption('discriminatorMap');
|
2008-02-04 00:29:57 +03:00
|
|
|
$old = $this->get($discCol, false);
|
2008-02-24 01:04:39 +03:00
|
|
|
$discValue = array_search($this->_entityName, $discMap);
|
|
|
|
if ((string) $old !== (string) $discValue || $old === null) {
|
|
|
|
$dataSet[$discCol] = $discValue;
|
|
|
|
$this->_data[$discCol] = $discValue;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
return $dataSet;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-02-08 01:21:18 +03:00
|
|
|
* Creates an array representation of the object's data.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2007-08-28 20:45:22 +04:00
|
|
|
* @param boolean $deep - Return also the relations
|
2007-01-19 23:47:24 +03:00
|
|
|
* @return array
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2007-11-27 21:23:13 +03:00
|
|
|
public function toArray($deep = true, $prefixKey = false)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
|
|
|
$a = array();
|
|
|
|
|
|
|
|
foreach ($this as $column => $value) {
|
2008-03-19 21:33:14 +03:00
|
|
|
if ($value === Doctrine_Null::$INSTANCE || is_object($value)) {
|
2007-07-06 03:47:48 +04:00
|
|
|
$value = null;
|
|
|
|
}
|
2007-01-19 23:47:24 +03:00
|
|
|
$a[$column] = $value;
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
if ($this->_class->getIdentifierType() == Doctrine::IDENTIFIER_AUTOINC) {
|
2008-06-15 19:56:28 +04:00
|
|
|
$idFieldNames = $this->_class->getIdentifier();
|
2008-05-06 17:41:22 +04:00
|
|
|
$idFieldName = $idFieldNames[0];
|
|
|
|
|
|
|
|
$ids = $this->identifier();
|
|
|
|
$id = count($ids) > 0 ? array_pop($ids) : null;
|
|
|
|
|
|
|
|
$a[$idFieldName] = $id;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
2007-08-28 20:45:22 +04:00
|
|
|
if ($deep) {
|
|
|
|
foreach ($this->_references as $key => $relation) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $relation instanceof Doctrine_Null) {
|
2007-09-26 01:39:38 +04:00
|
|
|
$a[$key] = $relation->toArray($deep, $prefixKey);
|
|
|
|
}
|
2007-08-28 20:45:22 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
// [FIX] Prevent mapped Doctrine_Entitys from being displayed fully
|
2008-01-17 16:26:31 +03:00
|
|
|
foreach ($this->_values as $key => $value) {
|
2008-05-14 01:20:34 +04:00
|
|
|
if ($value instanceof Doctrine_Entity) {
|
2008-01-17 16:26:31 +03:00
|
|
|
$a[$key] = $value->toArray($deep, $prefixKey);
|
|
|
|
} else {
|
|
|
|
$a[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Merges this Entity with an array of values
|
|
|
|
* or with another existing instance of.
|
2007-11-27 21:23:13 +03:00
|
|
|
*
|
|
|
|
* @param mixed $data Data to merge. Either another instance of this model or an array
|
|
|
|
* @param bool $deep Bool value for whether or not to merge the data deep
|
|
|
|
* @return void
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-11-27 21:23:13 +03:00
|
|
|
*/
|
|
|
|
public function merge($data, $deep = true)
|
|
|
|
{
|
|
|
|
if ($data instanceof $this) {
|
|
|
|
$array = $data->toArray($deep);
|
|
|
|
} else if (is_array($data)) {
|
|
|
|
$array = $data;
|
2008-01-05 22:55:56 +03:00
|
|
|
} else {
|
|
|
|
$array = array();
|
2007-11-27 21:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->fromArray($array, $deep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fromArray
|
|
|
|
*
|
|
|
|
* @param string $array
|
|
|
|
* @param bool $deep Bool value for whether or not to merge the data deep
|
|
|
|
* @return void
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-11-27 21:23:13 +03:00
|
|
|
*/
|
|
|
|
public function fromArray($array, $deep = true)
|
2007-09-22 01:40:54 +04:00
|
|
|
{
|
2007-09-22 05:32:48 +04:00
|
|
|
if (is_array($array)) {
|
|
|
|
foreach ($array as $key => $value) {
|
2008-04-09 18:31:25 +04:00
|
|
|
if ($deep && $this->getTable()->hasRelation($key)) {
|
2007-11-27 21:23:13 +03:00
|
|
|
$this->$key->fromArray($value, $deep);
|
2007-12-11 18:25:23 +03:00
|
|
|
} else if ($this->getTable()->hasField($key)) {
|
2007-11-08 21:54:55 +03:00
|
|
|
$this->set($key, $value);
|
2007-09-22 05:32:48 +04:00
|
|
|
}
|
2007-09-22 01:40:54 +04:00
|
|
|
}
|
2007-12-09 04:56:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Synchronizes a Doctrine_Entity and its relations with data from an array
|
2007-12-09 04:56:53 +03:00
|
|
|
*
|
2008-06-15 19:56:28 +04:00
|
|
|
* It expects an array representation of a Doctrine_Entity similar to the return
|
2007-12-09 04:56:53 +03:00
|
|
|
* value of the toArray() method. If the array contains relations it will create
|
|
|
|
* those that don't exist, update the ones that do, and delete the ones missing
|
2008-05-14 01:20:34 +04:00
|
|
|
* on the array but available on the Doctrine_Entity
|
2007-12-09 04:56:53 +03:00
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @param array $array representation of a Doctrine_Entity
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-12-09 04:56:53 +03:00
|
|
|
*/
|
2008-01-23 03:54:58 +03:00
|
|
|
public function synchronizeFromArray(array $array)
|
2007-12-09 04:56:53 +03:00
|
|
|
{
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if ($this->getTable()->hasRelation($key)) {
|
2008-01-23 11:04:54 +03:00
|
|
|
$this->get($key)->synchronizeFromArray($value);
|
|
|
|
} else if ($this->getTable()->hasColumn($key)) {
|
2007-12-09 04:56:53 +03:00
|
|
|
$this->set($key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// eliminate relationships missing in the $array
|
|
|
|
foreach ($this->_references as $name => $obj) {
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! isset($array[$name])) {
|
2007-12-09 04:56:53 +03:00
|
|
|
unset($this->$name);
|
|
|
|
}
|
2007-09-22 01:40:54 +04:00
|
|
|
}
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* exportTo
|
|
|
|
*
|
2007-12-11 14:10:27 +03:00
|
|
|
* @param string $type
|
|
|
|
* @param string $deep
|
2007-11-27 21:23:13 +03:00
|
|
|
* @return void
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-11-27 21:23:13 +03:00
|
|
|
*/
|
|
|
|
public function exportTo($type, $deep = true)
|
2007-09-27 02:36:32 +04:00
|
|
|
{
|
|
|
|
if ($type == 'array') {
|
|
|
|
return $this->toArray($deep);
|
|
|
|
} else {
|
|
|
|
return Doctrine_Parser::dump($this->toArray($deep, true), $type);
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* importFrom
|
|
|
|
*
|
2007-12-11 14:10:27 +03:00
|
|
|
* @param string $type
|
|
|
|
* @param string $data
|
2007-11-27 21:23:13 +03:00
|
|
|
* @return void
|
|
|
|
* @author Jonathan H. Wage
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-11-27 21:23:13 +03:00
|
|
|
*/
|
2007-09-27 02:36:32 +04:00
|
|
|
public function importFrom($type, $data)
|
|
|
|
{
|
|
|
|
if ($type == 'array') {
|
|
|
|
return $this->fromArray($data);
|
|
|
|
} else {
|
|
|
|
return $this->fromArray(Doctrine_Parser::load($data, $type));
|
|
|
|
}
|
|
|
|
}
|
2008-04-16 02:19:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the entity already has a persistent state.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if the object is new, FALSE otherwise.
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function isNew()
|
2008-04-16 02:19:22 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
return $this->_state == self::STATE_NEW;
|
2008-04-16 02:19:22 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-06-13 16:31:03 +04:00
|
|
|
/**
|
2008-02-08 01:21:18 +03:00
|
|
|
* Checks whether the entity has been modified since it was last synchronized
|
|
|
|
* with the database.
|
2007-06-13 16:31:03 +04:00
|
|
|
*
|
2008-02-08 01:21:18 +03:00
|
|
|
* @return boolean TRUE if the object has been modified, FALSE otherwise.
|
2007-06-13 16:31:03 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function isModified()
|
2007-06-13 16:31:03 +04:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
return count($this->_modified) > 0;
|
2007-06-13 16:31:03 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-27 23:38:56 +04:00
|
|
|
* Deletes the persistent state of the entity.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2008-07-27 23:38:56 +04:00
|
|
|
* @return boolean TRUE on success, FALSE on failure.
|
2008-04-16 02:19:22 +04:00
|
|
|
* @todo ActiveRecord method.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-27 23:38:56 +04:00
|
|
|
public function delete()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-04-13 00:11:11 +04:00
|
|
|
// TODO: Forward to EntityManager. There: registerRemoved() on UnitOfWork
|
2008-07-27 23:38:56 +04:00
|
|
|
return $this->_em->remove($this);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-02-08 01:21:18 +03:00
|
|
|
* Creates a copy of the entity.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @return Doctrine_Entity
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method. Implementation to EntityManager.
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2007-11-27 21:23:13 +03:00
|
|
|
public function copy($deep = true)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$data = $this->_data;
|
2007-08-04 01:02:17 +04:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
if ($this->_class->getIdentifierType() === Doctrine::IDENTIFIER_AUTOINC) {
|
|
|
|
$idFieldNames = (array)$this->_class->getIdentifier();
|
2008-02-24 23:31:49 +03:00
|
|
|
$id = $idFieldNames[0];
|
2007-08-04 01:02:17 +04:00
|
|
|
unset($data[$id]);
|
|
|
|
}
|
|
|
|
|
2008-05-06 17:41:22 +04:00
|
|
|
$ret = $this->_em->createEntity($this->_entityName, $data);
|
2007-01-19 23:47:24 +03:00
|
|
|
$modified = array();
|
2007-08-04 01:02:17 +04:00
|
|
|
|
|
|
|
foreach ($data as $key => $val) {
|
2007-04-13 22:06:48 +04:00
|
|
|
if ( ! ($val instanceof Doctrine_Null)) {
|
2007-01-19 23:47:24 +03:00
|
|
|
$ret->_modified[] = $key;
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
}
|
2007-04-13 22:06:48 +04:00
|
|
|
|
2007-11-27 21:23:13 +03:00
|
|
|
if ($deep) {
|
|
|
|
foreach ($this->_references as $key => $value) {
|
|
|
|
if ($value instanceof Doctrine_Collection) {
|
|
|
|
foreach ($value as $record) {
|
2008-02-08 01:27:29 +03:00
|
|
|
$ret->{$key}[] = $record->copy($deep);
|
2007-11-27 21:23:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
2008-02-08 01:27:29 +03:00
|
|
|
$ret->set($key, $value->copy($deep));
|
2007-04-12 22:32:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-11-27 21:23:13 +03:00
|
|
|
|
|
|
|
return $ret;
|
2007-04-12 22:32:07 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
2008-07-21 00:13:24 +04:00
|
|
|
* Assigns an identifier to the entity. This is only intended for use by
|
|
|
|
* the EntityPersisters or the UnitOfWork.
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
2008-07-21 00:13:24 +04:00
|
|
|
* @param mixed $id
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
final public function _assignIdentifier($id)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2008-07-21 00:13:24 +04:00
|
|
|
if (is_array($id)) {
|
|
|
|
foreach ($id as $fieldName => $value) {
|
|
|
|
$this->_id[$fieldName] = $value;
|
|
|
|
$this->_data[$fieldName] = $value;
|
2007-12-28 14:51:48 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
} else {
|
|
|
|
$name = $this->_class->getSingleIdentifierFieldName();
|
|
|
|
$this->_id[$name] = $id;
|
|
|
|
$this->_data[$name] = $id;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
$this->_modified = array();
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-07-21 00:13:24 +04:00
|
|
|
* INTERNAL:
|
|
|
|
* Returns the primary keys of the entity (key => value pairs).
|
2007-01-19 23:47:24 +03:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
final public function _identifier()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
|
|
|
return $this->_id;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
|
|
|
* hasRefence
|
|
|
|
* @param string $name
|
|
|
|
* @return boolean
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Better name? hasAssociation() ? Remove?
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-10 21:17:58 +04:00
|
|
|
final public function hasReference($name)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-05-22 01:06:17 +04:00
|
|
|
return isset($this->_references[$name]);
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
|
|
|
* obtainReference
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @throws Doctrine_Record_Exception if trying to get an unknown related component
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Better name? Remove?
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-07-10 21:17:58 +04:00
|
|
|
final public function obtainReference($name)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-05-22 01:06:17 +04:00
|
|
|
if (isset($this->_references[$name])) {
|
|
|
|
return $this->_references[$name];
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-02-08 01:21:18 +03:00
|
|
|
throw new Doctrine_Record_Exception("Unknown reference $name.");
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
|
|
|
*
|
2007-01-19 23:47:24 +03:00
|
|
|
* getReferences
|
|
|
|
* @return array all references
|
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _getReferences()
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-05-22 01:06:17 +04:00
|
|
|
return $this->_references;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* INTERNAL:
|
2007-01-19 23:47:24 +03:00
|
|
|
* setRelated
|
|
|
|
*
|
|
|
|
* @param string $alias
|
|
|
|
* @param Doctrine_Access $coll
|
2008-07-27 23:38:56 +04:00
|
|
|
* @todo Name? Remove?
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function _setRelated($alias, Doctrine_Access $coll)
|
2007-01-19 23:47:24 +03:00
|
|
|
{
|
2007-05-22 01:06:17 +04:00
|
|
|
$this->_references[$alias] = $coll;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-04-16 02:19:22 +04:00
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Removes links from this record to given records
|
2007-09-13 02:07:57 +04:00
|
|
|
* if no ids are given, it removes all links
|
2007-08-12 01:35:58 +04:00
|
|
|
*
|
2007-08-15 00:30:33 +04:00
|
|
|
* @param string $alias related component alias
|
|
|
|
* @param array $ids the identifiers of the related records
|
2008-05-14 01:20:34 +04:00
|
|
|
* @return Doctrine_Entity this object
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-08-12 01:35:58 +04:00
|
|
|
*/
|
2007-09-13 02:07:57 +04:00
|
|
|
public function unlink($alias, $ids = array())
|
2007-08-12 01:35:58 +04:00
|
|
|
{
|
|
|
|
$ids = (array) $ids;
|
2007-12-11 14:10:27 +03:00
|
|
|
|
2007-08-12 01:35:58 +04:00
|
|
|
$q = new Doctrine_Query();
|
|
|
|
|
|
|
|
$rel = $this->getTable()->getRelation($alias);
|
|
|
|
|
2007-08-15 00:30:33 +04:00
|
|
|
if ($rel instanceof Doctrine_Relation_Association) {
|
|
|
|
$q->delete()
|
|
|
|
->from($rel->getAssociationTable()->getComponentName())
|
2007-09-13 02:07:57 +04:00
|
|
|
->where($rel->getLocal() . ' = ?', array_values($this->identifier()));
|
|
|
|
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
$q->whereIn($rel->getForeign(), $ids);
|
|
|
|
}
|
2007-08-15 00:49:07 +04:00
|
|
|
|
|
|
|
$q->execute();
|
|
|
|
|
2007-11-18 19:06:37 +03:00
|
|
|
} else if ($rel instanceof Doctrine_Relation_ForeignKey) {
|
2007-08-15 01:20:00 +04:00
|
|
|
$q->update($rel->getTable()->getComponentName())
|
|
|
|
->set($rel->getForeign(), '?', array(null))
|
2007-09-13 02:07:57 +04:00
|
|
|
->addWhere($rel->getForeign() . ' = ?', array_values($this->identifier()));
|
|
|
|
|
|
|
|
if (count($ids) > 0) {
|
2008-02-24 23:31:49 +03:00
|
|
|
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
|
|
|
|
$q->whereIn($relTableIdFieldNames[0], $ids);
|
2007-09-13 02:07:57 +04:00
|
|
|
}
|
2007-08-15 01:20:00 +04:00
|
|
|
|
|
|
|
$q->execute();
|
|
|
|
}
|
|
|
|
if (isset($this->_references[$alias])) {
|
|
|
|
foreach ($this->_references[$alias] as $k => $record) {
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-08-15 01:20:00 +04:00
|
|
|
if (in_array(current($record->identifier()), $ids)) {
|
|
|
|
$this->_references[$alias]->remove($k);
|
2007-08-12 01:35:58 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-08-12 01:35:58 +04:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2007-08-15 01:20:00 +04:00
|
|
|
$this->_references[$alias]->takeSnapshot();
|
2007-08-12 01:35:58 +04:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-12-11 14:10:27 +03:00
|
|
|
|
|
|
|
/**
|
2008-06-15 19:56:28 +04:00
|
|
|
* Creates links from this record to given records.
|
2007-12-11 14:10:27 +03:00
|
|
|
*
|
|
|
|
* @param string $alias related component alias
|
|
|
|
* @param array $ids the identifiers of the related records
|
2008-05-14 01:20:34 +04:00
|
|
|
* @return Doctrine_Entity this object
|
2008-06-15 19:56:28 +04:00
|
|
|
* @todo ActiveEntity method.
|
2007-12-11 14:10:27 +03:00
|
|
|
*/
|
|
|
|
public function link($alias, array $ids)
|
|
|
|
{
|
|
|
|
if ( ! count($ids)) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$identifier = array_values($this->identifier());
|
|
|
|
$identifier = array_shift($identifier);
|
|
|
|
|
|
|
|
$rel = $this->getTable()->getRelation($alias);
|
|
|
|
|
|
|
|
if ($rel instanceof Doctrine_Relation_Association) {
|
|
|
|
$modelClassName = $rel->getAssociationTable()->getComponentName();
|
2007-12-11 14:31:13 +03:00
|
|
|
$localFieldName = $rel->getLocalFieldName();
|
2007-12-11 14:10:27 +03:00
|
|
|
$localFieldDef = $rel->getAssociationTable()->getColumnDefinition($localFieldName);
|
|
|
|
if ($localFieldDef['type'] == 'integer') {
|
|
|
|
$identifier = (integer) $identifier;
|
|
|
|
}
|
2007-12-11 14:31:13 +03:00
|
|
|
$foreignFieldName = $rel->getForeignFieldName();
|
2007-12-11 14:10:27 +03:00
|
|
|
$foreignFieldDef = $rel->getAssociationTable()->getColumnDefinition($foreignFieldName);
|
|
|
|
if ($foreignFieldDef['type'] == 'integer') {
|
|
|
|
for ($i = 0; $i < count($ids); $i++) {
|
|
|
|
$ids[$i] = (integer) $ids[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$record = new $modelClassName;
|
|
|
|
$record[$localFieldName] = $identifier;
|
|
|
|
$record[$foreignFieldName] = $id;
|
|
|
|
$record->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($rel instanceof Doctrine_Relation_ForeignKey) {
|
|
|
|
|
|
|
|
$q = new Doctrine_Query();
|
|
|
|
|
|
|
|
$q->update($rel->getTable()->getComponentName())
|
|
|
|
->set($rel->getForeign(), '?', array_values($this->identifier()));
|
|
|
|
|
|
|
|
if (count($ids) > 0) {
|
2008-02-24 23:31:49 +03:00
|
|
|
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
|
|
|
|
$q->whereIn($relTableIdFieldNames[0], $ids);
|
2007-12-11 14:10:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
} else if ($rel instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
$q = new Doctrine_Query();
|
|
|
|
$q->update($this->getTable()->getComponentName())
|
2008-01-05 22:55:56 +03:00
|
|
|
->set($rel->getLocalFieldName(), '?', $ids);
|
2007-12-11 14:10:27 +03:00
|
|
|
|
|
|
|
if (count($ids) > 0) {
|
2008-02-24 23:31:49 +03:00
|
|
|
$relTableIdFieldNames = (array)$rel->getTable()->getIdentifier();
|
|
|
|
$q->whereIn($relTableIdFieldNames[0], array_values($this->identifier()));
|
2007-12-11 14:10:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$q->execute();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2008-07-21 00:13:24 +04:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
/**
|
|
|
|
* Gets the ClassMetadata object that describes the entity class.
|
2008-07-10 21:17:58 +04:00
|
|
|
*
|
|
|
|
* @return Doctrine::ORM::Mapping::ClassMetadata
|
2008-03-17 16:26:34 +03:00
|
|
|
*/
|
2008-07-21 00:13:24 +04:00
|
|
|
final public function getClass()
|
2008-03-17 16:26:34 +03:00
|
|
|
{
|
|
|
|
return $this->_class;
|
|
|
|
}
|
2008-05-06 17:41:22 +04:00
|
|
|
|
2008-05-14 01:20:34 +04:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Gets the EntityManager that is responsible for the persistence of
|
2008-07-27 23:38:56 +04:00
|
|
|
* the entity.
|
2008-05-14 01:20:34 +04:00
|
|
|
*
|
2008-07-10 21:17:58 +04:00
|
|
|
* @return Doctrine::ORM::EntityManager
|
2008-05-14 01:20:34 +04:00
|
|
|
*/
|
2008-06-15 19:56:28 +04:00
|
|
|
final public function getEntityManager()
|
2008-05-06 17:41:22 +04:00
|
|
|
{
|
|
|
|
return $this->_em;
|
|
|
|
}
|
|
|
|
|
2008-06-15 19:56:28 +04:00
|
|
|
/**
|
2008-07-10 21:17:58 +04:00
|
|
|
* Gets the EntityRepository of the Entity.
|
2008-06-15 19:56:28 +04:00
|
|
|
*
|
2008-07-10 21:17:58 +04:00
|
|
|
* @return Doctrine::ORM::EntityRepository
|
2008-06-15 19:56:28 +04:00
|
|
|
*/
|
|
|
|
final public function getRepository()
|
2008-03-17 16:26:34 +03:00
|
|
|
{
|
2008-06-15 19:56:28 +04:00
|
|
|
return $this->_em->getRepository($this->_entityName);
|
2008-03-17 16:26:34 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
/**
|
|
|
|
* @todo Why toString() and __toString() ?
|
|
|
|
*/
|
2007-05-24 17:01:58 +04:00
|
|
|
public function toString()
|
|
|
|
{
|
|
|
|
return Doctrine::dump(get_object_vars($this));
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-19 23:47:24 +03:00
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
2008-03-17 16:26:34 +03:00
|
|
|
* @todo Why toString() and __toString() ?
|
2007-01-19 23:47:24 +03:00
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2007-05-24 17:01:58 +04:00
|
|
|
return (string) $this->_oid;
|
2007-01-19 23:47:24 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-02-24 01:04:39 +03:00
|
|
|
/**
|
|
|
|
* Helps freeing the memory occupied by the entity.
|
|
|
|
* Cuts all references the entity has to other entities and removes the entity
|
|
|
|
* from the instance pool.
|
|
|
|
* Note: The entity is no longer useable after free() has been called. Any operations
|
|
|
|
* done with the entity afterwards can lead to unpredictable results.
|
|
|
|
*/
|
2008-03-05 14:24:33 +03:00
|
|
|
public function free($deep = false)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-02-29 22:33:31 +03:00
|
|
|
if ($this->_state != self::STATE_LOCKED) {
|
2008-05-06 17:41:22 +04:00
|
|
|
$this->_em->detach($this);
|
2008-02-29 22:33:31 +03:00
|
|
|
$this->_data = array();
|
|
|
|
$this->_id = array();
|
|
|
|
|
|
|
|
if ($deep) {
|
|
|
|
foreach ($this->_references as $name => $reference) {
|
|
|
|
if ( ! ($reference instanceof Doctrine_Null)) {
|
|
|
|
$reference->free($deep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_references = array();
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2007-10-08 02:43:04 +04:00
|
|
|
}
|