Statement.php 5.89 KB
Newer Older
doctrine's avatar
doctrine 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 34 35 36 37 38 39 40 41 42 43 44 45 46
<?php
require_once("Access.php");
/**
 * Doctrine_Statement
 *
 * Doctrine_Statement is a wrapper for PDOStatement with DQL support
 *
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 */
class Doctrine_Statement extends Doctrine_Access {
    /**
     * @var Doctrine_Query $query
     */
    private $query;
    /**
     * @var PDOStatement $stmt
     */
    private $stmt;
    /**
     * @var array $reserved
     */
    private $reserved = array();

    /**
     * constructor
     *
     * @param Doctrine_Query $query
     * @param PDOStatement $stmt
     */
    public function __construct(Doctrine_Query $query, PDOStatement $stmt) {
        $this->query = $query;
        $this->stmt  = $stmt;
    }
    public function set($name, $value) { }
    public function get($name) { }
    /**
     * getCollection
     * returns Doctrine_Collection object
     *
     * @parma string $name              component name
     * @param integer $index
     * @return Doctrine_Collection
     */
    private function getCollection($name) {
zYne's avatar
zYne committed
47
        $table = $this->connection->getTable($name);
doctrine's avatar
doctrine committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        switch($this->fetchModes[$name]):
            case Doctrine::FETCH_BATCH:
                $coll = new Doctrine_Collection_Batch($table);
            break;
            case Doctrine::FETCH_LAZY:
                $coll = new Doctrine_Collection_Lazy($table);
            break;
            case Doctrine::FETCH_OFFSET:
                $coll = new Doctrine_Collection_Offset($table);
            break;
            case Doctrine::FETCH_IMMEDIATE:
                $coll = new Doctrine_Collection_Immediate($table);
            break;
            case Doctrine::FETCH_LAZY_OFFSET:
                $coll = new Doctrine_Collection_LazyOffset($table);
            break;
        endswitch;

        $coll->populate($this);
        return $coll;
    }
    /**
     * execute
     * executes the dql query, populates all collections
     * and returns the root collection
     *
     * @param array $params
     * @return Doctrine_Collection
     */
    public function execute($params = array()) {
        switch(count($this->tables)):
            case 0:
                throw new DQLException();
            break;
            case 1:
                $query = $this->getQuery();

                $keys  = array_keys($this->tables);
    
                $name  = $this->tables[$keys[0]]->getComponentName();
zYne's avatar
zYne committed
88
                $stmt  = $this->connection->execute($query,$params);
doctrine's avatar
doctrine committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

                while($data = $stmt->fetch(PDO::FETCH_ASSOC)):
                    foreach($data as $key => $value):
                        $e = explode("__",$key);
                        if(count($e) > 1) {
                            $data[$e[1]] = $value;
                        } else {
                            $data[$e[0]] = $value;
                        }
                        unset($data[$key]);
                    endforeach;
                    $this->data[$name][] = $data;
                endwhile;

                return $this->getCollection($keys[0]);
            break;
            default:
                $query = $this->getQuery();

                $keys  = array_keys($this->tables);
                $root  = $keys[0];
zYne's avatar
zYne committed
110
                $stmt  = $this->connection->execute($query,$params);
doctrine's avatar
doctrine committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                
                $previd = array();

                $coll = $this->getCollection($root);

                $array = $this->parseData($stmt);

                foreach($array as $data):

                    /**
                     * remove duplicated data rows and map data into objects
                     */
                    foreach($data as $key => $row):
                        if(empty($row))
                            continue;

                        $key  = ucwords($key);
                        $name = $this->tables[$key]->getComponentName();

                        if( ! isset($previd[$name]))
                            $previd[$name] = array();


                        if($previd[$name] !== $row) {
                            $this->tables[$name]->setData($row);
                            $record = $this->tables[$name]->getRecord();

                            if($name == $root) {
                                $this->tables[$name]->setData($row);
                                $record = $this->tables[$name]->getRecord();
                                $coll->add($record);
                            } else {
                                $last = $coll->getLast();

                                if( ! $last->hasReference($name)) {
                                    $last->initReference($this->getCollection($name),$this->connectors[$name]);
                                }
                                $last->addReference($record);
                            }
                        }

                        $previd[$name] = $row;
                    endforeach;
                endforeach;

                return $coll;
        endswitch;
    }
    /**
     * parseData
     * parses the data returned by PDOStatement
     *
     * @param PDOStatement $stmt
     * @return array
     */
    public function parseData(PDOStatement $stmt) {
        $array = array();
        while($data = $stmt->fetch(PDO::FETCH_ASSOC)):
            /**
             * parse the data into two-dimensional array
             */
            foreach($data as $key => $value):
                $e = explode("__",$key);

                if(count($e) > 1) {
                    $data[$e[0]][$e[1]] = $value;
                } else {
                    $data[0][$e[0]] = $value;
                }
                unset($data[$key]);
            endforeach;
            $array[] = $data;
        endwhile;
        $stmt->closeCursor();
        return $array;
    }
}
?>