1 |
<?php
|
2 |
/*
|
3 |
* $Id$
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Hydrate_Record
|
24 |
* defines a record fetching strategy for Doctrine_Hydrate
|
25 |
*
|
26 |
* @package Doctrine
|
27 |
* @subpackage Hydrate
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
29 |
* @link www.phpdoctrine.com
|
30 |
* @since 1.0
|
31 |
* @version $Revision$
|
32 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
33 |
*/
|
34 |
class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
|
35 |
{
|
36 |
protected $_collections = array();
|
37 |
|
38 |
protected $_records = array();
|
39 |
|
40 |
protected $_tables = array();
|
41 |
|
42 |
public function getElementCollection($component)
|
43 |
{
|
44 |
$coll = new Doctrine_Collection($component);
|
45 |
$this->_collections[] = $coll;
|
46 |
|
47 |
return $coll;
|
48 |
}
|
49 |
|
50 |
public function getLastKey($coll)
|
51 |
{
|
52 |
$coll->end();
|
53 |
|
54 |
return $coll->key();
|
55 |
}
|
56 |
public function initRelated($record, $name)
|
57 |
{
|
58 |
if ( ! is_array($record)) {
|
59 |
$record[$name];
|
60 |
|
61 |
return true;
|
62 |
}
|
63 |
return false;
|
64 |
}
|
65 |
public function registerCollection(Doctrine_Collection $coll)
|
66 |
{
|
67 |
$this->_collections[] = $coll;
|
68 |
}
|
69 |
|
70 |
/**
|
71 |
* isIdentifiable
|
72 |
* returns whether or not a given data row is identifiable (it contains
|
73 |
* all primary key fields specified in the second argument)
|
74 |
*
|
75 |
* @param array $row
|
76 |
* @param Doctrine_Table $table
|
77 |
* @return boolean
|
78 |
*/
|
79 |
public function isIdentifiable(array $row, Doctrine_Table $table)
|
80 |
{
|
81 |
$primaryKeys = $table->getIdentifier();
|
82 |
|
83 |
if (is_array($primaryKeys)) {
|
84 |
foreach ($primaryKeys as $id) {
|
85 |
if ( ! isset($row[$id])) {
|
86 |
return false;
|
87 |
}
|
88 |
}
|
89 |
} else {
|
90 |
if ( ! isset($row[$primaryKeys])) {
|
91 |
return false;
|
92 |
}
|
93 |
}
|
94 |
return true;
|
95 |
}
|
96 |
public function getNullPointer()
|
97 |
{
|
98 |
return self::$_null;
|
99 |
}
|
100 |
public function getElement(array $data, $component)
|
101 |
{
|
102 |
if ( ! isset($this->_tables[$component])) {
|
103 |
$this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
|
104 |
$this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
|
105 |
}
|
106 |
$this->_tables[$component]->setData($data);
|
107 |
$record = $this->_tables[$component]->getRecord();
|
108 |
|
109 |
if ( ! isset($this->_records[$record->getOid()]) ) {
|
110 |
$record->clearRelated();
|
111 |
$this->_records[$record->getOid()] = $record;
|
112 |
}
|
113 |
|
114 |
return $record;
|
115 |
}
|
116 |
public function flush()
|
117 |
{
|
118 |
// take snapshots from all initialized collections
|
119 |
foreach ($this->_collections as $key => $coll) {
|
120 |
$coll->takeSnapshot();
|
121 |
}
|
122 |
foreach ($this->_tables as $table) {
|
123 |
$table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
|
124 |
}
|
125 |
}
|
126 |
}
|