2008-01-05 22:55:56 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $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
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Mapper is responsible for mapping between the domain model and the database
|
|
|
|
* back and forth. Each entity in the domain model has a corresponding mapper.
|
|
|
|
*
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision: 3406 $
|
|
|
|
* @link www.phpdoctrine.org
|
2008-04-13 00:11:11 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @todo Move all finder stuff to EntityRepository.
|
|
|
|
* @todo Rename to "EntityPersister" or similar.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-03-17 16:26:34 +03:00
|
|
|
class Doctrine_Mapper
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
|
|
|
/**
|
2008-03-17 16:26:34 +03:00
|
|
|
* Metadata object that descibes the mapping of the mapped entity class.
|
|
|
|
*
|
|
|
|
* @var Doctrine_ClassMetadata
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
protected $_classMetadata;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the domain class this mapper is used for.
|
|
|
|
*/
|
|
|
|
protected $_domainClassName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Doctrine_Connection object that the database connection of this mapper.
|
|
|
|
*
|
|
|
|
* @var Doctrine_Connection $conn
|
|
|
|
*/
|
|
|
|
protected $_conn;
|
2008-02-20 23:54:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete mapping strategy that is used.
|
|
|
|
*/
|
|
|
|
protected $_mappingStrategy;
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $identityMap first level cache
|
2008-02-28 18:30:55 +03:00
|
|
|
* @todo Move to UnitOfWork.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
protected $_identityMap = array();
|
2008-03-17 16:26:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Null object.
|
|
|
|
*/
|
|
|
|
private $_nullObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of registered entity listeners.
|
|
|
|
*/
|
|
|
|
private $_entityListeners = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new mapper.
|
|
|
|
*
|
|
|
|
* @param string $name The name of the domain class this mapper is used for.
|
|
|
|
* @param Doctrine_Table $table The table object used for the mapping procedure.
|
|
|
|
* @throws Doctrine_Connection_Exception if there are no opened connections
|
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
public function __construct($name, Doctrine_ClassMetadata $classMetadata)
|
2008-03-17 16:26:34 +03:00
|
|
|
{
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_domainClassName = $name;
|
2008-02-20 23:54:20 +03:00
|
|
|
$this->_conn = $classMetadata->getConnection();
|
|
|
|
$this->_classMetadata = $classMetadata;
|
2008-03-19 21:33:14 +03:00
|
|
|
$this->_nullObject = Doctrine_Null::$INSTANCE;
|
2008-03-23 14:30:29 +03:00
|
|
|
if ($classMetadata->getInheritanceType() == Doctrine::INHERITANCE_TYPE_JOINED) {
|
2008-02-20 23:54:20 +03:00
|
|
|
$this->_mappingStrategy = new Doctrine_Mapper_JoinedStrategy($this);
|
|
|
|
} else {
|
|
|
|
$this->_mappingStrategy = new Doctrine_Mapper_DefaultStrategy($this);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* createQuery
|
|
|
|
* creates a new Doctrine_Query object and adds the component name
|
|
|
|
* of this table as the query 'from' part
|
|
|
|
*
|
|
|
|
* @param string Optional alias name for component aliasing.
|
|
|
|
*
|
|
|
|
* @return Doctrine_Query
|
|
|
|
*/
|
|
|
|
public function createQuery($alias = '')
|
|
|
|
{
|
|
|
|
if ( ! empty($alias)) {
|
|
|
|
$alias = ' ' . trim($alias);
|
|
|
|
}
|
|
|
|
return Doctrine_Query::create($this->_conn)->from($this->getComponentName() . $alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sets the connection for this class
|
|
|
|
*
|
|
|
|
* @params Doctrine_Connection a connection object
|
|
|
|
* @return Doctrine_Table this object
|
2008-02-20 23:54:20 +03:00
|
|
|
* @todo refactor
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function setConnection(Doctrine_Connection $conn)
|
|
|
|
{
|
|
|
|
$this->_conn = $conn;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-04 00:29:57 +03:00
|
|
|
* Returns the connection the mapper is currently using.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-02-04 00:29:57 +03:00
|
|
|
* @return Doctrine_Connection|null The connection object.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
return $this->_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a new record
|
|
|
|
*
|
|
|
|
* @param $array an array where keys are field names and
|
|
|
|
* values representing field values
|
|
|
|
* @return Doctrine_Record the created record object
|
|
|
|
*/
|
|
|
|
public function create(array $array = array())
|
|
|
|
{
|
|
|
|
$record = new $this->_domainClassName($this, true);
|
|
|
|
$record->fromArray($array);
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
2008-02-28 18:30:55 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
public function addEntityListener(Doctrine_Record_Listener $listener)
|
|
|
|
{
|
|
|
|
if ( ! in_array($listener, $this->_entityListeners)) {
|
|
|
|
$this->_entityListeners[] = $listener;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeEntityListener(Doctrine_Record_Listener $listener)
|
|
|
|
{
|
|
|
|
if ($key = array_search($listener, $this->_entityListeners, true)) {
|
|
|
|
unset($this->_entityListeners[$key]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notifyEntityListeners(Doctrine_Record $entity, $callback, $eventType)
|
|
|
|
{
|
|
|
|
if ($this->_entityListeners) {
|
|
|
|
$event = new Doctrine_Event($entity, $eventType);
|
|
|
|
foreach ($this->_entityListeners as $listener) {
|
|
|
|
$listener->$callback($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-28 18:30:55 +03:00
|
|
|
public function detach(Doctrine_Record $entity)
|
|
|
|
{
|
2008-03-05 14:24:33 +03:00
|
|
|
return $this->_conn->unitOfWork->detach($entity);
|
2008-02-28 18:30:55 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
2008-02-20 23:54:20 +03:00
|
|
|
* Finds an entity by its primary key.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
|
|
|
* @param $id database row id
|
|
|
|
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
|
|
|
|
* @return mixed Array or Doctrine_Record or false if no result
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function find($id, $hydrationMode = null)
|
|
|
|
{
|
|
|
|
if (is_null($id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = is_array($id) ? array_values($id) : array($id);
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->createQuery()
|
2008-02-04 00:29:57 +03:00
|
|
|
->where(implode(' = ? AND ', (array) $this->_classMetadata->getIdentifier()) . ' = ?')
|
2008-01-05 22:55:56 +03:00
|
|
|
->fetchOne($id, $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-20 23:54:20 +03:00
|
|
|
* Finds all entities of the mapper's class.
|
|
|
|
* Use with care.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-02-20 23:54:20 +03:00
|
|
|
* @param int $hydrationMode Doctrine::HYDRATE_ARRAY or Doctrine::HYDRATE_RECORD
|
2008-01-05 22:55:56 +03:00
|
|
|
* @return Doctrine_Collection
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function findAll($hydrationMode = null)
|
|
|
|
{
|
|
|
|
return $this->createQuery()->execute(array(), $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findBySql
|
|
|
|
* finds records with given SQL where clause
|
|
|
|
* returns a collection of records
|
|
|
|
*
|
|
|
|
* @param string $dql DQL after WHERE clause
|
|
|
|
* @param array $params query parameters
|
|
|
|
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
|
|
|
|
* @return Doctrine_Collection
|
|
|
|
*
|
|
|
|
* @todo This actually takes DQL, not SQL, but it requires column names
|
|
|
|
* instead of field names. This should be fixed to use raw SQL instead.
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function findBySql($dql, array $params = array(), $hydrationMode = null)
|
|
|
|
{
|
|
|
|
return $this->createQuery()->where($dql)->execute($params, $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findByDql
|
|
|
|
* finds records with given DQL where clause
|
|
|
|
* returns a collection of records
|
|
|
|
*
|
|
|
|
* @param string $dql DQL after WHERE clause
|
|
|
|
* @param array $params query parameters
|
|
|
|
* @param int $hydrationMode Doctrine::FETCH_ARRAY or Doctrine::FETCH_RECORD
|
|
|
|
* @return Doctrine_Collection
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function findByDql($dql, array $params = array(), $hydrationMode = null)
|
|
|
|
{
|
|
|
|
$query = new Doctrine_Query($this->_conn);
|
|
|
|
$component = $this->getComponentName();
|
|
|
|
$dql = 'FROM ' . $component . ' WHERE ' . $dql;
|
|
|
|
|
|
|
|
return $query->query($dql, $params, $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-20 23:54:20 +03:00
|
|
|
* Executes a named query.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-02-20 23:54:20 +03:00
|
|
|
* @param string $queryName The name that was used when storing the query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @return mixed The result.
|
|
|
|
* @deprecated
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
public function executeNamedQuery($queryName, $params = array(), $hydrationMode = Doctrine::HYDRATE_RECORD)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
|
|
|
return Doctrine_Manager::getInstance()
|
2008-02-20 23:54:20 +03:00
|
|
|
->createNamedQuery($queryName)
|
|
|
|
->execute($params, $hydrationMode);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clear
|
|
|
|
* clears the first level cache (identityMap)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @todo what about a more descriptive name? clearIdentityMap?
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->_identityMap = array();
|
2008-04-13 00:11:11 +04:00
|
|
|
//$this->_conn->unitOfWork->clearIdentitiesForEntity($this->_classMetadata->getRootClassName());
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* addRecord
|
|
|
|
* adds a record to identity map
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be added
|
|
|
|
* @return boolean
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo Better name? registerRecord? Move elsewhere to the new location of the identity maps.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function addRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$id = implode(' ', $record->identifier());
|
2008-04-13 00:11:11 +04:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
if (isset($this->_identityMap[$id])) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
/*if ($this->_conn->unitOfWork->containsIdentity($id, $record->getClassMetadata()->getRootClassname())) {
|
|
|
|
return false;
|
|
|
|
}*/
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-04-13 00:11:11 +04:00
|
|
|
//$this->_conn->unitOfWork->registerIdentity($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_identityMap[$id] = $record;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2008-02-24 01:04:39 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells the mapper to manage the entity if it's not already managed.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if the entity was previously not managed and is now managed,
|
|
|
|
* FALSE otherwise (the entity is already managed).
|
|
|
|
*/
|
|
|
|
public function manage(Doctrine_Record $record)
|
|
|
|
{
|
2008-03-05 14:24:33 +03:00
|
|
|
return $this->_conn->unitOfWork->manage($record);
|
2008-02-24 01:04:39 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* removeRecord
|
|
|
|
* removes a record from the identity map, returning true if the record
|
|
|
|
* was found and removed and false if the record wasn't found.
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be removed
|
|
|
|
* @return boolean
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo Move elsewhere to the new location of the identity maps.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function removeRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$id = implode(' ', $record->identifier());
|
|
|
|
|
|
|
|
if (isset($this->_identityMap[$id])) {
|
|
|
|
unset($this->_identityMap[$id]);
|
|
|
|
return true;
|
|
|
|
}
|
2008-04-13 00:11:11 +04:00
|
|
|
/*if ($this->_conn->unitOfWork->containsIdentity($id, $record->getClassMetadata()->getRootClassName())) {
|
|
|
|
$this->_conn->unitOfWork->unregisterIdentity($record);
|
|
|
|
return true;
|
|
|
|
}*/
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getRecord
|
2008-02-20 23:54:20 +03:00
|
|
|
* First checks if record exists in identityMap, if not
|
|
|
|
* returns a new record.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
|
|
|
* @return Doctrine_Record
|
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
public function getRecord(array $data)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
if ( ! empty($data)) {
|
2008-02-24 23:31:49 +03:00
|
|
|
$identifierFieldNames = (array)$this->_classMetadata->getIdentifier();
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($identifierFieldNames as $fieldName) {
|
2008-01-12 22:49:11 +03:00
|
|
|
if ( ! isset($data[$fieldName])) {
|
2008-01-05 22:55:56 +03:00
|
|
|
// primary key column not found return new record
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
$id[] = $data[$fieldName];
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
2008-01-12 22:49:11 +03:00
|
|
|
$record = new $this->_domainClassName($this, true, $data);
|
|
|
|
$data = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$id = implode(' ', $id);
|
|
|
|
|
|
|
|
if (isset($this->_identityMap[$id])) {
|
2008-04-13 00:11:11 +04:00
|
|
|
//if ($this->_conn->unitOfWork->containsIdentity($id, $this->_classMetadata->getRootClassName())) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$record = $this->_identityMap[$id];
|
2008-04-13 00:11:11 +04:00
|
|
|
//$record = $this->_conn->unitOfWork->getByIdentity($id, $this->_classMetadata->getRootClassName());
|
2008-01-12 22:49:11 +03:00
|
|
|
$record->hydrate($data);
|
2008-01-05 22:55:56 +03:00
|
|
|
} else {
|
2008-01-12 22:49:11 +03:00
|
|
|
$record = new $this->_domainClassName($this, false, $data);
|
2008-04-13 00:11:11 +04:00
|
|
|
//$this->_conn->unitOfWork->registerIdentity($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->_identityMap[$id] = $record;
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
$data = array();
|
2008-01-05 22:55:56 +03:00
|
|
|
} else {
|
2008-01-12 22:49:11 +03:00
|
|
|
$record = new $this->_domainClassName($this, true, $data);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id database row id
|
|
|
|
*/
|
|
|
|
final public function getProxy($id = null)
|
|
|
|
{
|
|
|
|
if ($id !== null) {
|
2008-02-04 00:29:57 +03:00
|
|
|
$identifierColumnNames = $this->_classMetadata->getIdentifierColumnNames();
|
2008-01-05 22:55:56 +03:00
|
|
|
$query = 'SELECT ' . implode(', ', $identifierColumnNames)
|
2008-02-04 00:29:57 +03:00
|
|
|
. ' FROM ' . $this->_classMetadata->getTableName()
|
2008-01-05 22:55:56 +03:00
|
|
|
. ' WHERE ' . implode(' = ? && ', $identifierColumnNames) . ' = ?';
|
|
|
|
$query = $this->applyInheritance($query);
|
|
|
|
|
2008-02-07 13:40:27 +03:00
|
|
|
$params = array_merge(array($id),array());
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
$data = $this->_conn->execute($query, $params)->fetch(PDO::FETCH_ASSOC);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
if ($data === false) {
|
2008-01-05 22:55:56 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
return $this->getRecord($data);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* applyInheritance
|
|
|
|
* @param $where query where part to be modified
|
|
|
|
* @return string query where part with column aggregation inheritance added
|
|
|
|
*/
|
|
|
|
final public function applyInheritance($where)
|
|
|
|
{
|
2008-02-04 00:29:57 +03:00
|
|
|
$discCol = $this->_classMetadata->getInheritanceOption('discriminatorColumn');
|
|
|
|
if ( ! $discCol) {
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
$discMap = $this->_classMetadata->getInheritanceOption('discriminatorMap');
|
|
|
|
$inheritanceMap = array($discCol => array_search($this->_domainClassName, $discMap));
|
2008-01-05 22:55:56 +03:00
|
|
|
if ( ! empty($inheritanceMap)) {
|
|
|
|
$a = array();
|
2008-02-04 00:29:57 +03:00
|
|
|
foreach ($inheritanceMap as $column => $value) {
|
|
|
|
$a[] = $column . ' = ?';
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
$i = implode(' AND ', $a);
|
|
|
|
$where .= ' AND ' . $i;
|
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* prepareValue
|
|
|
|
* this method performs special data preparation depending on
|
|
|
|
* the type of the given column
|
|
|
|
*
|
|
|
|
* 1. It unserializes array and object typed columns
|
|
|
|
* 2. Uncompresses gzip typed columns
|
|
|
|
* 3. Gets the appropriate enum values for enum typed columns
|
|
|
|
* 4. Initializes special null object pointer for null values (for fast column existence checking purposes)
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* <code type='php'>
|
|
|
|
* $field = 'name';
|
|
|
|
* $value = null;
|
|
|
|
* $table->prepareValue($field, $value); // Doctrine_Null
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Table_Exception if unserialization of array/object typed column fails or
|
|
|
|
* @throws Doctrine_Table_Exception if uncompression of gzip typed column fails *
|
|
|
|
* @param string $field the name of the field
|
|
|
|
* @param string $value field value
|
2008-02-20 23:54:20 +03:00
|
|
|
* @param string $typeHint A hint on the type of the value. If provided, the type lookup
|
|
|
|
* for the field can be skipped. Used i.e. during hydration to
|
|
|
|
* improve performance on large and/or complex results.
|
2008-01-05 22:55:56 +03:00
|
|
|
* @return mixed prepared value
|
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
public function prepareValue($fieldName, $value, $typeHint = null)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-03-17 16:26:34 +03:00
|
|
|
if ($value === $this->_nullObject) {
|
|
|
|
return $this->_nullObject;
|
2008-01-05 22:55:56 +03:00
|
|
|
} else if ($value === null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2008-02-20 23:54:20 +03:00
|
|
|
$type = is_null($typeHint) ? $this->_classMetadata->getTypeOf($fieldName) : $typeHint;
|
2008-01-05 22:55:56 +03:00
|
|
|
switch ($type) {
|
|
|
|
case 'integer':
|
|
|
|
case 'string';
|
|
|
|
// don't do any casting here PHP INT_MAX is smaller than what the databases support
|
|
|
|
break;
|
|
|
|
case 'enum':
|
2008-02-04 00:29:57 +03:00
|
|
|
return $this->_classMetadata->enumValue($fieldName, $value);
|
2008-01-05 22:55:56 +03:00
|
|
|
break;
|
|
|
|
case 'boolean':
|
|
|
|
return (boolean) $value;
|
|
|
|
break;
|
|
|
|
case 'array':
|
|
|
|
case 'object':
|
|
|
|
if (is_string($value)) {
|
|
|
|
$value = unserialize($value);
|
|
|
|
if ($value === false) {
|
|
|
|
throw new Doctrine_Mapper_Exception('Unserialization of ' . $fieldName . ' failed.');
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'gzip':
|
|
|
|
$value = gzuncompress($value);
|
|
|
|
if ($value === false) {
|
|
|
|
throw new Doctrine_Mapper_Exception('Uncompressing of ' . $fieldName . ' failed.');
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
2008-02-20 23:54:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydrates the given data into the entity.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function hydrate(Doctrine_Record $entity, array $data)
|
|
|
|
{
|
|
|
|
$this->_values = array_merge($this->_values, $this->cleanData($data));
|
|
|
|
$this->_data = array_merge($this->_data, $data);
|
|
|
|
$this->_extractIdentifier(true);
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getTree
|
|
|
|
*
|
|
|
|
* getter for associated tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return instance of Doctrine_Tree, otherwise returns false
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo Part of the NestedSet Behavior plugin. Move outta here some day...
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function getTree()
|
|
|
|
{
|
2008-02-04 00:29:57 +03:00
|
|
|
return $this->_classMetadata->getTree();
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* isTree
|
|
|
|
*
|
|
|
|
* determine if table acts as tree
|
|
|
|
*
|
|
|
|
* @return mixed if tree return true, otherwise returns false
|
2008-02-04 00:29:57 +03:00
|
|
|
* @todo Part of the NestedSet Behavior plugin. Move outta here some day...
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function isTree()
|
|
|
|
{
|
2008-02-04 00:29:57 +03:00
|
|
|
return $this->_classMetadata->isTree();
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getComponentName
|
|
|
|
*
|
|
|
|
* @return void
|
2008-02-20 23:54:20 +03:00
|
|
|
* @deprecated Use getMappedClassName()
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function getComponentName()
|
|
|
|
{
|
|
|
|
return $this->_domainClassName;
|
|
|
|
}
|
2008-02-20 23:54:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the name of the class the mapper is used for.
|
|
|
|
*/
|
|
|
|
public function getMappedClassName()
|
|
|
|
{
|
|
|
|
return $this->_domainClassName;
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a string representation of this object
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return Doctrine_Lib::getTableAsString($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findBy
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @param string $value
|
|
|
|
* @param string $hydrationMode
|
|
|
|
* @return void
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
protected function findBy($fieldName, $value, $hydrationMode = null)
|
|
|
|
{
|
|
|
|
return $this->createQuery()->where($fieldName . ' = ?')->execute(array($value), $hydrationMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* findOneBy
|
|
|
|
*
|
|
|
|
* @param string $column
|
|
|
|
* @param string $value
|
|
|
|
* @param string $hydrationMode
|
|
|
|
* @return void
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
protected function findOneBy($fieldName, $value, $hydrationMode = null)
|
|
|
|
{
|
|
|
|
$results = $this->createQuery()->where($fieldName . ' = ?')->limit(1)->execute(
|
|
|
|
array($value), $hydrationMode);
|
2008-02-20 23:54:20 +03:00
|
|
|
return $hydrationMode === Doctrine::HYDRATE_ARRAY ? array_shift($results) : $results->getFirst();
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* __call
|
|
|
|
*
|
|
|
|
* Adds support for magic finders.
|
|
|
|
* findByColumnName, findByRelationAlias
|
|
|
|
* findById, findByContactId, etc.
|
|
|
|
*
|
|
|
|
* @return void
|
2008-02-20 23:54:20 +03:00
|
|
|
* @throws Doctrine_Mapper_Exception If the method called is an invalid find* method
|
|
|
|
* or no find* method at all and therefore an invalid
|
|
|
|
* method call.
|
2008-04-13 00:11:11 +04:00
|
|
|
* @todo Remove. Move to EntityRepository.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
|
|
|
public function __call($method, $arguments)
|
|
|
|
{
|
|
|
|
if (substr($method, 0, 6) == 'findBy') {
|
|
|
|
$by = substr($method, 6, strlen($method));
|
|
|
|
$method = 'findBy';
|
|
|
|
} else if (substr($method, 0, 9) == 'findOneBy') {
|
|
|
|
$by = substr($method, 9, strlen($method));
|
|
|
|
$method = 'findOneBy';
|
2008-02-20 23:54:20 +03:00
|
|
|
} else {
|
2008-03-17 16:26:34 +03:00
|
|
|
try {
|
|
|
|
throw new Exception();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getTraceAsString() . "<br/><br/>";
|
|
|
|
}
|
2008-02-20 23:54:20 +03:00
|
|
|
throw new Doctrine_Mapper_Exception("Undefined method '$method'.");
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
if (isset($by)) {
|
|
|
|
if ( ! isset($arguments[0])) {
|
2008-02-20 23:54:20 +03:00
|
|
|
throw new Doctrine_Mapper_Exception('You must specify the value to findBy.');
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$fieldName = Doctrine::tableize($by);
|
|
|
|
$hydrationMode = isset($arguments[1]) ? $arguments[1]:null;
|
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
if ($this->_classMetadata->hasField($fieldName)) {
|
2008-01-05 22:55:56 +03:00
|
|
|
return $this->$method($fieldName, $arguments[0], $hydrationMode);
|
2008-02-04 00:29:57 +03:00
|
|
|
} else if ($this->_classMetadata->hasRelation($by)) {
|
|
|
|
$relation = $this->_classMetadata->getRelation($by);
|
2008-01-05 22:55:56 +03:00
|
|
|
if ($relation['type'] === Doctrine_Relation::MANY) {
|
2008-02-20 23:54:20 +03:00
|
|
|
throw new Doctrine_Mapper_Exception('Cannot findBy many relationship.');
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
return $this->$method($relation['local'], $arguments[0], $hydrationMode);
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Mapper_Exception('Cannot find by: ' . $by . '. Invalid field or relationship alias.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-01-12 22:49:11 +03:00
|
|
|
* Saves an entity and all it's related entities.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-01-12 22:49:11 +03:00
|
|
|
* @param Doctrine_Record $record The entity to save.
|
|
|
|
* @param Doctrine_Connection $conn The connection to use. Will default to the mapper's
|
|
|
|
* connection.
|
2008-02-20 23:54:20 +03:00
|
|
|
* @throws Doctrine_Mapper_Exception If the mapper is unable to save the given entity.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
public function save(Doctrine_Record $record, Doctrine_Connection $conn = null)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
if ( ! ($record instanceof $this->_domainClassName)) {
|
|
|
|
throw new Doctrine_Mapper_Exception("Mapper of type " . $this->_domainClassName . "
|
|
|
|
can't save instances of type" . get_class($record) . ".");
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($conn === null) {
|
|
|
|
$conn = $this->_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
$state = $record->state();
|
|
|
|
if ($state === Doctrine_Record::STATE_LOCKED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$record->state(Doctrine_Record::STATE_LOCKED);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$conn->beginInternalTransaction();
|
2008-02-04 00:29:57 +03:00
|
|
|
$saveLater = $this->_saveRelated($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
$record->state($state);
|
|
|
|
|
|
|
|
if ($record->isValid()) {
|
2008-02-20 23:54:20 +03:00
|
|
|
$this->_insertOrUpdate($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
} else {
|
|
|
|
$conn->transaction->addInvalid($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
$state = $record->state();
|
|
|
|
$record->state(Doctrine_Record::STATE_LOCKED);
|
|
|
|
|
|
|
|
foreach ($saveLater as $fk) {
|
|
|
|
$alias = $fk->getAlias();
|
|
|
|
if ($record->hasReference($alias)) {
|
|
|
|
$obj = $record->$alias;
|
|
|
|
// check that the related object is not an instance of Doctrine_Null
|
|
|
|
if ( ! ($obj instanceof Doctrine_Null)) {
|
|
|
|
$obj->save($conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// save the MANY-TO-MANY associations
|
|
|
|
$this->saveAssociations($record);
|
2008-01-12 22:49:11 +03:00
|
|
|
// reset state
|
2008-01-05 22:55:56 +03:00
|
|
|
$record->state($state);
|
|
|
|
$conn->commit();
|
|
|
|
} catch (Exception $e) {
|
2008-02-15 01:41:06 +03:00
|
|
|
$conn->rollback();
|
2008-01-05 22:55:56 +03:00
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
/**
|
2008-02-20 23:54:20 +03:00
|
|
|
* Inserts or updates an entity, depending on it's state.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
2008-02-20 23:54:20 +03:00
|
|
|
* @param Doctrine_Record $record The entity to insert/update.
|
2008-01-05 22:55:56 +03:00
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
protected function _insertOrUpdate(Doctrine_Record $record)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-03-17 16:26:34 +03:00
|
|
|
$record->preSave();
|
|
|
|
$this->notifyEntityListeners($record, 'preSave', Doctrine_Event::RECORD_SAVE);
|
2008-02-20 23:54:20 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
switch ($record->state()) {
|
|
|
|
case Doctrine_Record::STATE_TDIRTY:
|
|
|
|
$this->_insert($record);
|
|
|
|
break;
|
|
|
|
case Doctrine_Record::STATE_DIRTY:
|
|
|
|
case Doctrine_Record::STATE_PROXY:
|
|
|
|
$this->_update($record);
|
|
|
|
break;
|
|
|
|
case Doctrine_Record::STATE_CLEAN:
|
|
|
|
case Doctrine_Record::STATE_TCLEAN:
|
|
|
|
// do nothing
|
|
|
|
break;
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
2008-03-17 16:26:34 +03:00
|
|
|
|
|
|
|
$record->postSave();
|
|
|
|
$this->notifyEntityListeners($record, 'postSave', Doctrine_Event::RECORD_SAVE);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
2008-02-20 23:54:20 +03:00
|
|
|
/**
|
|
|
|
* saves the given record
|
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function saveSingleRecord(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
$this->_insertOrUpdate($record);
|
|
|
|
}
|
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
/**
|
2008-02-04 00:29:57 +03:00
|
|
|
* _saveRelated
|
2008-01-05 22:55:56 +03:00
|
|
|
* saves all related records to $record
|
|
|
|
*
|
|
|
|
* @throws PDOException if something went wrong at database level
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
protected function _saveRelated(Doctrine_Record $record)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
|
|
|
$saveLater = array();
|
|
|
|
foreach ($record->getReferences() as $k => $v) {
|
|
|
|
$rel = $record->getTable()->getRelation($k);
|
|
|
|
|
|
|
|
$local = $rel->getLocal();
|
|
|
|
$foreign = $rel->getForeign();
|
|
|
|
|
|
|
|
if ($rel instanceof Doctrine_Relation_ForeignKey) {
|
|
|
|
$saveLater[$k] = $rel;
|
|
|
|
} else if ($rel instanceof Doctrine_Relation_LocalKey) {
|
|
|
|
// ONE-TO-ONE relationship
|
|
|
|
$obj = $record->get($rel->getAlias());
|
|
|
|
|
|
|
|
// Protection against infinite function recursion before attempting to save
|
|
|
|
if ($obj instanceof Doctrine_Record && $obj->isModified()) {
|
|
|
|
$obj->save($this->_conn);
|
|
|
|
|
|
|
|
/** Can this be removed?
|
|
|
|
$id = array_values($obj->identifier());
|
|
|
|
|
|
|
|
foreach ((array) $rel->getLocal() as $k => $field) {
|
|
|
|
$record->set($field, $id[$k]);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $saveLater;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* saveAssociations
|
|
|
|
*
|
|
|
|
* this method takes a diff of one-to-many / many-to-many original and
|
|
|
|
* current collections and applies the changes
|
|
|
|
*
|
|
|
|
* for example if original many-to-many related collection has records with
|
|
|
|
* primary keys 1,2 and 3 and the new collection has records with primary keys
|
|
|
|
* 3, 4 and 5, this method would first destroy the associations to 1 and 2 and then
|
|
|
|
* save new associations to 4 and 5
|
|
|
|
*
|
|
|
|
* @throws Doctrine_Connection_Exception if something went wrong at database level
|
|
|
|
* @param Doctrine_Record $record
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function saveAssociations(Doctrine_Record $record)
|
|
|
|
{
|
|
|
|
foreach ($record->getReferences() as $relationName => $relatedObject) {
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
$rel = $record->getTable()->getRelation($relationName);
|
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
if ($rel instanceof Doctrine_Relation_Association) {
|
2008-01-05 22:55:56 +03:00
|
|
|
$relatedObject->save($this->_conn);
|
|
|
|
$assocTable = $rel->getAssociationTable();
|
2008-02-04 00:29:57 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
foreach ($relatedObject->getDeleteDiff() as $r) {
|
|
|
|
$query = 'DELETE FROM ' . $assocTable->getTableName()
|
|
|
|
. ' WHERE ' . $rel->getForeign() . ' = ?'
|
|
|
|
. ' AND ' . $rel->getLocal() . ' = ?';
|
|
|
|
$this->_conn->execute($query, array($r->getIncremented(), $record->getIncremented()));
|
|
|
|
}
|
2008-02-20 23:54:20 +03:00
|
|
|
|
|
|
|
$assocMapper = $this->_conn->getMapper($assocTable->getComponentName());
|
|
|
|
foreach ($relatedObject->getInsertDiff() as $r) {
|
|
|
|
$assocRecord = $assocMapper->create();
|
2008-01-05 22:55:56 +03:00
|
|
|
$assocRecord->set($assocTable->getFieldName($rel->getForeign()), $r);
|
|
|
|
$assocRecord->set($assocTable->getFieldName($rel->getLocal()), $record);
|
2008-02-20 23:54:20 +03:00
|
|
|
$assocMapper->save($assocRecord);
|
|
|
|
//$this->saveSingleRecord($assocRecord);
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-07 13:40:27 +03:00
|
|
|
* Updates an entity.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be updated
|
|
|
|
* @return boolean whether or not the update was successful
|
|
|
|
* @todo Move to Doctrine_Table (which will become Doctrine_Mapper).
|
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
protected function _update(Doctrine_Record $record)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-03-17 16:26:34 +03:00
|
|
|
$record->preUpdate();
|
|
|
|
$this->notifyEntityListeners($record, 'preUpdate', Doctrine_Event::RECORD_UPDATE);
|
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
$table = $this->_classMetadata;
|
2008-03-17 16:26:34 +03:00
|
|
|
$this->_mappingStrategy->doUpdate($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
$record->postUpdate();
|
|
|
|
$this->notifyEntityListeners($record, 'postUpdate', Doctrine_Event::RECORD_UPDATE);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-07 13:40:27 +03:00
|
|
|
* Inserts an entity.
|
2008-01-05 22:55:56 +03:00
|
|
|
*
|
|
|
|
* @param Doctrine_Record $record record to be inserted
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-02-20 23:54:20 +03:00
|
|
|
protected function _insert(Doctrine_Record $record)
|
2008-03-17 16:26:34 +03:00
|
|
|
{
|
|
|
|
$record->preInsert();
|
|
|
|
$this->notifyEntityListeners($record, 'preInsert', Doctrine_Event::RECORD_INSERT);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
$this->_mappingStrategy->doInsert($record);
|
2008-01-05 22:55:56 +03:00
|
|
|
$this->addRecord($record);
|
2008-03-17 16:26:34 +03:00
|
|
|
|
|
|
|
$record->postInsert();
|
|
|
|
$this->notifyEntityListeners($record, 'postInsert', Doctrine_Event::RECORD_INSERT);
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
/**
|
2008-02-20 23:54:20 +03:00
|
|
|
* Deletes given entity and all it's related entities.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
2008-02-20 23:54:20 +03:00
|
|
|
* Triggered Events: onPreDelete, onDelete.
|
2008-01-12 22:49:11 +03:00
|
|
|
*
|
|
|
|
* @return boolean true on success, false on failure
|
2008-02-20 23:54:20 +03:00
|
|
|
* @throws Doctrine_Mapper_Exception
|
2008-01-12 22:49:11 +03:00
|
|
|
*/
|
|
|
|
public function delete(Doctrine_Record $record, Doctrine_Connection $conn = null)
|
|
|
|
{
|
|
|
|
if ( ! $record->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! ($record instanceof $this->_domainClassName)) {
|
|
|
|
throw new Doctrine_Mapper_Exception("Mapper of type " . $this->_domainClassName . "
|
|
|
|
can't save instances of type" . get_class($record) . ".");
|
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
if ($conn == null) {
|
|
|
|
$conn = $this->_conn;
|
|
|
|
}
|
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
$record->preDelete();
|
|
|
|
$this->notifyEntityListeners($record, 'preDelete', Doctrine_Event::RECORD_DELETE);
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2008-02-04 00:29:57 +03:00
|
|
|
$table = $this->_classMetadata;
|
2008-01-12 22:49:11 +03:00
|
|
|
|
|
|
|
$state = $record->state();
|
|
|
|
$record->state(Doctrine_Record::STATE_LOCKED);
|
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
$this->_mappingStrategy->doDelete($record);
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
$record->postDelete();
|
|
|
|
$this->notifyEntityListeners($record, 'postDelete', Doctrine_Event::RECORD_DELETE);
|
2008-01-12 22:49:11 +03:00
|
|
|
|
|
|
|
return true;
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
public function hasAttribute($key)
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case Doctrine::ATTR_LOAD_REFERENCES:
|
|
|
|
case Doctrine::ATTR_QUERY_LIMIT:
|
|
|
|
case Doctrine::ATTR_COLL_KEY:
|
|
|
|
case Doctrine::ATTR_VALIDATE:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
public function executeQuery(Doctrine_Query $query)
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
public function getTable()
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-02-04 00:29:57 +03:00
|
|
|
return $this->_classMetadata;
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
2008-02-20 23:54:20 +03:00
|
|
|
public function getClassMetadata()
|
2008-01-05 22:55:56 +03:00
|
|
|
{
|
2008-02-04 00:29:57 +03:00
|
|
|
return $this->_classMetadata;
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
|
|
|
|
public function getIdentityMap()
|
|
|
|
{
|
|
|
|
return $this->_identityMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dump()
|
|
|
|
{
|
|
|
|
var_dump($this->_invokedMethods);
|
|
|
|
}
|
|
|
|
|
2008-02-20 23:54:20 +03:00
|
|
|
public function free()
|
|
|
|
{
|
|
|
|
$this->_mappingStrategy = null;
|
|
|
|
}
|
2008-01-12 22:49:11 +03:00
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
public function getMapping()
|
2008-03-05 14:24:33 +03:00
|
|
|
{
|
|
|
|
return $this->_mappingStrategy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-20 23:54:20 +03:00
|
|
|
public function getFieldName($columnName)
|
|
|
|
{
|
|
|
|
return $this->_mappingStrategy->getFieldName($columnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFieldNames()
|
|
|
|
{
|
|
|
|
return $this->_mappingStrategy->getFieldNames();
|
|
|
|
}
|
|
|
|
|
2008-03-17 16:26:34 +03:00
|
|
|
public function getOwningClass($fieldName)
|
2008-02-20 23:54:20 +03:00
|
|
|
{
|
2008-03-17 16:26:34 +03:00
|
|
|
return $this->_mappingStrategy->getOwningClass($fieldName);
|
2008-02-20 23:54:20 +03:00
|
|
|
}
|
|
|
|
|
2008-01-12 22:49:11 +03:00
|
|
|
/* Hooks used during SQL query construction to manipulate the query. */
|
|
|
|
|
2008-02-07 13:40:27 +03:00
|
|
|
/**
|
|
|
|
* Callback that is invoked during the SQL construction process.
|
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
public function getCustomJoins()
|
|
|
|
{
|
2008-02-20 23:54:20 +03:00
|
|
|
return $this->_mappingStrategy->getCustomJoins();
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
|
|
|
|
2008-02-07 13:40:27 +03:00
|
|
|
/**
|
|
|
|
* Callback that is invoked during the SQL construction process.
|
|
|
|
*/
|
2008-01-12 22:49:11 +03:00
|
|
|
public function getCustomFields()
|
|
|
|
{
|
2008-02-20 23:54:20 +03:00
|
|
|
return $this->_mappingStrategy->getCustomFields();
|
2008-01-12 22:49:11 +03:00
|
|
|
}
|
2008-01-05 22:55:56 +03:00
|
|
|
}
|