Record.php 8.06 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  $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_Resource_Record
 *
 * @package     Doctrine
26 27
 * @subpackage  Resource
 * @author      Jonathan H. Wage <jwage@mac.com>
zYne's avatar
zYne committed
28 29 30 31 32
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version     $Revision$
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
33
class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Countable, IteratorAggregate
34
{
35 36
    protected $_data = array();
    protected $_model = null;
37
    protected $_changes = array();
38
    
39
    public function __construct($model)
Jonathan.Wage's avatar
Jonathan.Wage committed
40
    {
41 42
        $this->_model = $model;
        
43
        $this->initialize();
44 45
    }
    
46 47 48 49 50
    public function clearChanges()
    {
        $this->_changes = array();
    }
    
51 52
    public function initialize($loadRelations = true)
    {
Jonathan.Wage's avatar
Jonathan.Wage committed
53 54
        $schema = $this->getTable()->getSchema();
        $relations = $schema['relations'];
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        
        if (isset($schema['columns'])) {
            $columns = $schema['columns'];
        
            foreach ($columns as $column) {
                if (!isset($this->_data[$column['name']]) || $this->_data[$column['name']]) {
                    $this->_data[$column['name']] = null;
                }
            }
        }
    }
    
    public function getConfig($key = null)
    {
        return Doctrine_Resource_Client::getInstance()->getConfig($key);
Jonathan.Wage's avatar
Jonathan.Wage committed
70 71
    }
    
72
    public function get($key)
73
    {
74
        if (!isset($key)) {
75 76 77 78 79 80 81 82 83 84 85
            return;
        }
        
        if (!isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
            $this->_data[$key] = $this->createRelation($key);
        }
        
        if (!array_key_exists($key, $this->_data)) {
            throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
        }
        
86
        return $this->_data[$key];
87 88
    }

89
    public function set($key, $value)
90
    {
91
        if (!isset($key)) {
92 93 94 95 96 97 98 99 100 101 102 103
            return;
        }
        
        if (!isset($this->_data[$key]) && $this->getTable()->hasRelation($key)) {
            $this->_data[$key] = $this->createRelation($key);
        }
        
        if (!array_key_exists($key, $this->_data)) {
            throw new Doctrine_Resource_Exception('Unknown property / related component: '.$key);
        }
        
        if ($this->_data[$key] != $value && !$value instanceof Doctrine_Resource_Record && !$value instanceof Doctrine_Resource_Collection) {
104 105 106 107
            $this->_changes[$key] = $value;
        }
        
        $this->_data[$key] = $value;
108 109
    }
    
110 111 112 113 114 115
    public function createRelation($key)
    {
        $relation = $this->getTable()->getRelation($key);
        $class = $relation['class'];
        
        if ($relation['type'] === Doctrine_Relation::ONE) {
116
            $return = new $class();
117 118 119 120 121 122 123
        } else {
            $return = new Doctrine_Resource_Collection($class);
        }
        
        return $return;
    }
    
124 125
    public function count()
    {
126
        return count($this->_data);
127 128 129 130
    }
    
    public function getIterator()
    {
131
        return new ArrayIterator($this->_data);
132 133
    }
    
134 135
    public function getChanges()
    {
136 137 138 139 140 141 142 143 144 145 146 147
        global $gotten;
        
        if (!$gotten) {
            $gotten = array();
        }
        
        $md5Hash = $this->getMd5Hash();
        
        if (!in_array($md5Hash, $gotten)) {
            $gotten[] = $md5Hash;
        }
        
148 149 150
        $array = array();
        
        foreach ($this->_data as $key => $value) {
Jonathan.Wage's avatar
Jonathan.Wage committed
151
            if ($this->getTable()->hasRelation($key)) {
152
                
Jonathan.Wage's avatar
Jonathan.Wage committed
153
                $relation = $this->getTable()->getRelation($key);
154
                
155 156 157
                if ($value instanceof Doctrine_Resource_Record) {
                    if ($value->hasChanges() && !in_array($value->getMd5Hash(), $gotten)) {
                        $array[$key] = $value->getChanges();
158
                    }
159 160 161
                } else if($value instanceof Doctrine_Resource_Collection) {
                    foreach ($value as $key2 => $record) {
                        if ($record->hasChanges() && !in_array($record->getMd5Hash(), $gotten)) {
162 163 164 165
                            $array[$key][$record->getModel() . '_' .$key2] = $record->getChanges();
                        }
                    }
                }
Jonathan.Wage's avatar
Jonathan.Wage committed
166
            } else if ($this->getTable()->hasColumn($key)) {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                if (isset($this->_changes[$key])) {
                    $array[$key] = $value;
                }
            }
        }
        
        $identifier = $this->identifier();
        
        $array = array_merge($identifier, $array);
        
        return $array;
    }
    
    public function hasChanges()
    {
        return !empty($this->_changes) ? true:false;
    }
    
185 186
    public function save()
    {
187
        $request = new Doctrine_Resource_Request();
188
        $request->set('action', 'save');
189
        $request->set('model', $this->getModel());
190
        $request->set('identifier', $this->identifier());
191
        $request->set('data', $this->getChanges());
192
        
193
        $response = $request->execute();
194
        
195
        $this->fromArray($response);
196 197
    }
    
198 199 200
    public function delete()
    {
        $request = new Doctrine_Resource_Request();
201
        $request->set('action', 'delete');
202 203 204 205 206 207
        $request->set('model', $this->getModel());
        $request->set('identifier', $this->identifier());
        
        $response = $request->execute();
    }
    
Jonathan.Wage's avatar
Jonathan.Wage committed
208 209
    public function getTable()
    {
210 211 212
        $model = $this->_model;
        
        return Doctrine_Resource_Client::getInstance()->getTable($model);
Jonathan.Wage's avatar
Jonathan.Wage committed
213 214
    }
    
215 216 217
    public function getModel()
    {
        return $this->_model;
218 219
    }
    
220 221 222 223
    public function identifier()
    {
        $identifier = array();
        
Jonathan.Wage's avatar
Jonathan.Wage committed
224 225 226 227 228
        $schema = $this->getTable()->getSchema();
        $columns = $schema['columns'];
        
        if (isset($columns) && is_array($columns)) {
            foreach ($columns as $name => $column) {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                if ($column['primary'] == true) {
                    $identifier[$name] = $this->_data[$name];
                }
            }
        }
        
        return $identifier;
    }
    
    public function exists()
    {
        $identifier = $this->identifier();
        
        foreach ($identifier as $key => $value) {
            if (!$value) {
                return false;
            }
        }
        
        return true;
    }
    
251
    public function toArray($deep = false)
252
    {
253
        $data = array();
254
        
255
        foreach ($this->_data as $key => $value) {
256
            if ($deep && $this->getTable()->hasRelation($key)) {
257 258 259
                $data[$key] = $this->$key->toArray($deep);
            } else if ($this->getTable()->hasColumn($key)) {
                $data[$key]  = $value;
260 261 262
            }
        }
        
263 264 265 266 267 268 269 270 271 272 273 274
        return $data;
    }
    
    public function fromArray(array $array)
    {
        foreach ($array as $key => $value) {
            if ($this->getTable()->hasRelation($key) && is_array($value)) {
                $this->$key->fromArray($value);
            } else if ($this->getTable()->hasColumn($key)) {
                $this->$key = $value;
            }
        }
275
    }
276 277 278 279 280 281
    
    public function getMd5Hash()
    {
        return md5(serialize($this->_data));
    }
}