| 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.org>. |
| 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.org |
| 30 | * @since 1.0 |
| 31 | * @version $Revision$ |
| 32 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 33 | */ |
| 34 | class Doctrine_Hydrator_RecordDriver 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 | |
| 57 | public function initRelated($record, $name) |
| 58 | { |
| 59 | return true; |
| 60 | /* |
| 61 | if ( ! is_array($record)) { |
| 62 | $record[$name]; |
| 63 | return true; |
| 64 | } |
| 65 | return false; |
| 66 | */ |
| 67 | } |
| 68 | |
| 69 | public function registerCollection(Doctrine_Collection $coll) |
| 70 | { |
| 71 | $this->_collections[] = $coll; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * isIdentifiable |
| 76 | * returns whether or not a given data row is identifiable (it contains |
| 77 | * all primary key fields specified in the second argument) |
| 78 | * |
| 79 | * @param array $row |
| 80 | * @param Doctrine_Table $table |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public function isIdentifiable(array $row, Doctrine_Table $table) |
| 84 | { |
| 85 | $primaryKeys = $table->getIdentifierColumnNames(); |
| 86 | |
| 87 | if (is_array($primaryKeys)) { |
| 88 | foreach ($primaryKeys as $id) { |
| 89 | if ( ! isset($row[$id])) { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | } else { |
| 94 | if ( ! isset($row[$primaryKeys])) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | public function getNullPointer() |
| 102 | { |
| 103 | return self::$_null; |
| 104 | } |
| 105 | |
| 106 | public function getElement(array $data, $component) |
| 107 | { |
| 108 | if ( ! isset($this->_tables[$component])) { |
| 109 | $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component); |
| 110 | $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false); |
| 111 | } |
| 112 | |
| 113 | $this->_tables[$component]->setData($data); |
| 114 | $record = $this->_tables[$component]->getRecord(); |
| 115 | |
| 116 | if ( ! isset($this->_records[$record->getOid()]) ) { |
| 117 | $record->clearRelated(); |
| 118 | $this->_records[$record->getOid()] = $record; |
| 119 | } |
| 120 | |
| 121 | return $record; |
| 122 | } |
| 123 | |
| 124 | public function flush() |
| 125 | { |
| 126 | // take snapshots from all initialized collections |
| 127 | foreach ($this->_collections as $key => $coll) { |
| 128 | $coll->takeSnapshot(); |
| 129 | } |
| 130 | foreach ($this->_tables as $table) { |
| 131 | $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true); |
| 132 | } |
| 133 | } |
| 134 | } |