2007-05-29 21:36:15 +04: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
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Hydrate_Record
|
|
|
|
* defines a record fetching strategy for Doctrine_Hydrate
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Hydrate
|
2007-05-29 21:36:15 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
*/
|
2007-10-08 00:58:08 +04:00
|
|
|
class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
|
2007-05-29 21:36:15 +04:00
|
|
|
{
|
|
|
|
protected $_collections = array();
|
|
|
|
|
|
|
|
protected $_records = array();
|
2007-05-30 14:38:32 +04:00
|
|
|
|
|
|
|
protected $_tables = array();
|
2007-05-29 21:36:15 +04:00
|
|
|
|
|
|
|
public function getElementCollection($component)
|
|
|
|
{
|
|
|
|
$coll = new Doctrine_Collection($component);
|
|
|
|
$this->_collections[] = $coll;
|
|
|
|
|
|
|
|
return $coll;
|
|
|
|
}
|
2007-10-22 20:38:03 +04:00
|
|
|
|
|
|
|
public function getLastKey($coll)
|
2007-06-05 02:38:39 +04:00
|
|
|
{
|
2007-10-22 20:38:03 +04:00
|
|
|
$coll->end();
|
|
|
|
|
|
|
|
return $coll->key();
|
2007-06-05 02:38:39 +04:00
|
|
|
}
|
2007-11-19 13:00:44 +03:00
|
|
|
|
2007-06-05 02:38:39 +04:00
|
|
|
public function initRelated($record, $name)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! is_array($record)) {
|
2007-06-05 02:38:39 +04:00
|
|
|
$record[$name];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-19 13:00:44 +03:00
|
|
|
|
2007-05-31 21:45:07 +04:00
|
|
|
public function registerCollection(Doctrine_Collection $coll)
|
2007-05-29 22:59:14 +04:00
|
|
|
{
|
|
|
|
$this->_collections[] = $coll;
|
2007-05-30 14:38:32 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-05-30 14:38:32 +04:00
|
|
|
/**
|
|
|
|
* isIdentifiable
|
|
|
|
* returns whether or not a given data row is identifiable (it contains
|
|
|
|
* all primary key fields specified in the second argument)
|
|
|
|
*
|
|
|
|
* @param array $row
|
|
|
|
* @param Doctrine_Table $table
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isIdentifiable(array $row, Doctrine_Table $table)
|
|
|
|
{
|
2007-11-18 19:06:37 +03:00
|
|
|
$primaryKeys = $table->getIdentifierColumnNames();
|
2007-05-29 22:59:14 +04:00
|
|
|
|
2007-05-30 14:38:32 +04:00
|
|
|
if (is_array($primaryKeys)) {
|
|
|
|
foreach ($primaryKeys as $id) {
|
|
|
|
if ( ! isset($row[$id])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( ! isset($row[$primaryKeys])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2007-05-29 22:59:14 +04:00
|
|
|
}
|
2007-11-19 13:00:44 +03:00
|
|
|
|
2007-06-07 21:04:56 +04:00
|
|
|
public function getNullPointer()
|
|
|
|
{
|
|
|
|
return self::$_null;
|
|
|
|
}
|
2007-11-19 13:00:44 +03:00
|
|
|
|
2007-05-29 21:36:15 +04:00
|
|
|
public function getElement(array $data, $component)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->_tables[$component])) {
|
2007-05-30 14:38:32 +04:00
|
|
|
$this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
|
2007-05-31 21:45:07 +04:00
|
|
|
$this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
|
2007-05-30 14:38:32 +04:00
|
|
|
}
|
2007-11-18 19:06:37 +03:00
|
|
|
|
2007-05-30 14:38:32 +04:00
|
|
|
$this->_tables[$component]->setData($data);
|
|
|
|
$record = $this->_tables[$component]->getRecord();
|
2007-09-04 05:23:02 +04:00
|
|
|
|
|
|
|
if ( ! isset($this->_records[$record->getOid()]) ) {
|
|
|
|
$record->clearRelated();
|
|
|
|
$this->_records[$record->getOid()] = $record;
|
|
|
|
}
|
2007-05-29 21:36:15 +04:00
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
2007-11-19 13:00:44 +03:00
|
|
|
|
2007-05-29 21:36:15 +04:00
|
|
|
public function flush()
|
|
|
|
{
|
|
|
|
// take snapshots from all initialized collections
|
2007-07-21 20:35:04 +04:00
|
|
|
foreach ($this->_collections as $key => $coll) {
|
2007-05-29 22:59:14 +04:00
|
|
|
$coll->takeSnapshot();
|
2007-05-29 21:36:15 +04:00
|
|
|
}
|
2007-05-30 14:38:32 +04:00
|
|
|
foreach ($this->_tables as $table) {
|
|
|
|
$table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
|
2007-05-29 21:36:15 +04:00
|
|
|
}
|
|
|
|
}
|
2007-10-08 00:58:08 +04:00
|
|
|
}
|