Record.php 3.83 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 27 28 29 30 31 32 33
<?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
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 */
34
class Doctrine_Hydrate_Record extends Doctrine_Object
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;
    }
zYne's avatar
zYne committed
49 50 51 52 53 54 55 56 57 58 59 60 61
    public function search(Doctrine_Record $record, Doctrine_Collection $coll)
    {
        return array_search($record, $coll->getData(), true);
    }
    public function initRelated($record, $name)
    {
    	if ( ! is_array($record)) {
            $record[$name];

            return true;
        }
        return false;
    }
zYne's avatar
zYne committed
62
    public function registerCollection(Doctrine_Collection $coll)
zYne's avatar
zYne committed
63 64
    {
        $this->_collections[] = $coll;
zYne's avatar
zYne committed
65 66 67 68 69 70 71 72 73 74 75 76 77
    }
    /**
     * 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)
    {
    	$primaryKeys = $table->getIdentifier();
zYne's avatar
zYne committed
78

zYne's avatar
zYne committed
79 80 81 82 83 84 85 86 87 88 89 90
        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
91
    }
92 93 94 95
    public function getNullPointer() 
    {
        return self::$_null;
    }
zYne's avatar
zYne committed
96 97
    public function getElement(array $data, $component)
    {
zYne's avatar
zYne committed
98 99
    	if ( ! isset($this->_tables[$component])) {
            $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
zYne's avatar
zYne committed
100
            $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
zYne's avatar
zYne committed
101 102 103
        }
        $this->_tables[$component]->setData($data);
        $record = $this->_tables[$component]->getRecord();
zYne's avatar
zYne committed
104 105 106 107 108 109 110
        $this->_records[] = $record;

        return $record;
    }
    public function flush()
    {
        // take snapshots from all initialized collections
zYne's avatar
zYne committed
111 112
        foreach (array_unique($this->_collections) as $key => $coll) {
            $coll->takeSnapshot();
zYne's avatar
zYne committed
113
        }
zYne's avatar
zYne committed
114 115
        foreach ($this->_tables as $table) {
            $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
zYne's avatar
zYne committed
116 117 118
        }
    }
}