Record.php 3.97 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?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
27
 * @subpackage  Hydrate
zYne's avatar
zYne committed
28 29 30 31 32 33
 * @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>
 */
34
class Doctrine_Hydrate_Record extends Doctrine_Locator_Injectable
zYne's avatar
zYne committed
35 36 37 38
{
    protected $_collections = array();
    
    protected $_records = array();
zYne's avatar
zYne committed
39 40
    
    protected $_tables = array();
zYne's avatar
zYne committed
41 42 43 44 45 46 47 48

    public function getElementCollection($component)
    {
        $coll = new Doctrine_Collection($component);
        $this->_collections[] = $coll;

        return $coll;
    }
49 50

    public function getLastKey($coll) 
zYne's avatar
zYne committed
51
    {
52 53 54
        $coll->end();
        
        return $coll->key();
zYne's avatar
zYne committed
55
    }
romanb's avatar
romanb committed
56
    
zYne's avatar
zYne committed
57 58
    public function initRelated($record, $name)
    {
59
        if ( ! is_array($record)) {
zYne's avatar
zYne committed
60 61 62 63 64 65
            $record[$name];

            return true;
        }
        return false;
    }
romanb's avatar
romanb committed
66
    
zYne's avatar
zYne committed
67
    public function registerCollection(Doctrine_Collection $coll)
zYne's avatar
zYne committed
68 69
    {
        $this->_collections[] = $coll;
zYne's avatar
zYne committed
70
    }
71

zYne's avatar
zYne committed
72 73 74 75 76 77 78 79 80 81 82
    /**
     * 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)
    {
83
        $primaryKeys = $table->getIdentifierColumnNames();
zYne's avatar
zYne committed
84

zYne's avatar
zYne committed
85 86 87 88 89 90 91 92 93 94 95 96
        if (is_array($primaryKeys)) {
            foreach ($primaryKeys as $id) {
                if ( ! isset($row[$id])) {
                    return false;
                }
            }
        } else {
            if ( ! isset($row[$primaryKeys])) {
                return false;
            }
        }
        return true;
zYne's avatar
zYne committed
97
    }
romanb's avatar
romanb committed
98
    
99 100 101 102
    public function getNullPointer() 
    {
        return self::$_null;
    }
romanb's avatar
romanb committed
103
    
zYne's avatar
zYne committed
104 105
    public function getElement(array $data, $component)
    {
106
        if ( ! isset($this->_tables[$component])) {
zYne's avatar
zYne committed
107
            $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
zYne's avatar
zYne committed
108
            $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
zYne's avatar
zYne committed
109
        }
110
        
zYne's avatar
zYne committed
111 112
        $this->_tables[$component]->setData($data);
        $record = $this->_tables[$component]->getRecord();
113 114 115 116 117

        if ( ! isset($this->_records[$record->getOid()]) ) {
            $record->clearRelated();
            $this->_records[$record->getOid()] = $record;
        }
zYne's avatar
zYne committed
118 119 120

        return $record;
    }
romanb's avatar
romanb committed
121
    
zYne's avatar
zYne committed
122 123 124
    public function flush()
    {
        // take snapshots from all initialized collections
125
        foreach ($this->_collections as $key => $coll) {
zYne's avatar
zYne committed
126
            $coll->takeSnapshot();
zYne's avatar
zYne committed
127
        }
zYne's avatar
zYne committed
128 129
        foreach ($this->_tables as $table) {
            $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
zYne's avatar
zYne committed
130 131
        }
    }
132
}