Neevo Public API
  • Namespace
  • Class
  • Tree

Namespaces

  • Neevo
    • Nette
  • None
  • PHP

Classes

  • Neevo
  • NeevoBaseStmt
  • NeevoCacheFile
  • NeevoCacheMemcache
  • NeevoCacheMemory
  • NeevoCacheSession
  • NeevoConnection
  • NeevoDriverMySQL
  • NeevoDriverMySQLi
  • NeevoDriverPgSQL
  • NeevoDriverSQLite2
  • NeevoDriverSQLite3
  • NeevoLiteral
  • NeevoLoader
  • NeevoObserverMap
  • NeevoParser
  • NeevoResult
  • NeevoResultIterator
  • NeevoRow
  • NeevoStmt

Interfaces

  • INeevoCache
  • INeevoDriver
  • INeevoObservable
  • INeevoObserver

Exceptions

  • NeevoDriverException
  • NeevoException
  • NeevoImplementationException
  1 <?php
  2 /**
  3  * Neevo - Tiny database layer for PHP. (http://neevo.smasty.net)
  4  *
  5  * This source file is subject to the MIT license that is bundled
  6  * with this package in the file license.txt.
  7  *
  8  * Copyright (c) 2011 Martin Srank (http://smasty.net)
  9  *
 10  */
 11 
 12 
 13 /**
 14  * Representation of a row in a result.
 15  * @author Martin Srank
 16  * @package Neevo
 17  */
 18 class NeevoRow implements ArrayAccess, Countable, IteratorAggregate {
 19 
 20 
 21     /** @var bool */
 22     protected $frozen;
 23 
 24     /** @var string */
 25     protected $primary;
 26 
 27     /** @var array */
 28     protected $data = array();
 29 
 30     /** @var array */
 31     protected $modified = array();
 32 
 33     /** @var string */
 34     protected $table;
 35 
 36     /** @var NeevoConnection */
 37     protected $connection;
 38 
 39 
 40     /**
 41      * Create a row instance.
 42      * @param array $data
 43      * @param NeevoResult $result
 44      * @return void
 45      */
 46     public function __construct(array $data, NeevoResult $result){
 47         $this->data = $data;
 48         $this->connection = $result->getConnection();
 49         $this->table = $result->getTable();
 50         $this->primary = $result->getPrimaryKey();
 51 
 52         if(!$this->table || !$this->primary || !isset($this->data[$this->primary]))
 53             $this->frozen = true;
 54     }
 55 
 56 
 57     /**
 58      * Update corresponding database row if available.
 59      * @throws NeevoException
 60      * @return int Number of affected rows.
 61      */
 62     public function update(){
 63         if($this->frozen)
 64             throw new NeevoException('Update disabled - cannot get primary key or table.');
 65 
 66         if(!empty($this->modified)){
 67             return NeevoStmt::createUpdate($this->connection, $this->table, $this->modified)
 68                     ->where($this->primary, $this->data[$this->primary])->limit(1)->affectedRows();
 69         }
 70         return 0;
 71     }
 72 
 73 
 74     /**
 75      * Delete corresponding database row if available.
 76      * @throws NeevoException
 77      * @return int Number of affected rows.
 78      */
 79     public function delete(){
 80         if($this->frozen)
 81             throw new NeevoException('Delete disabled - cannot get primary key or table.');
 82 
 83         return NeevoStmt::createDelete($this->connection, $this->table)
 84                 ->where($this->primary, $this->data[$this->primary])->limit(1)->affectedRows();
 85     }
 86 
 87 
 88     /**
 89      * Return values as an array.
 90      * @return array
 91      */
 92     public function toArray(){
 93         return array_merge($this->data, $this->modified);
 94     }
 95 
 96 
 97     /**
 98      * If row is not able to update it's state.
 99      * @return bool
100      */
101     public function isFrozen(){
102         return (bool) $this->frozen;
103     }
104 
105 
106     /*  ************  Implementation of Countable  ************  */
107 
108 
109     public function count(){
110         return count(array_merge($this->data, $this->modified));
111     }
112 
113 
114     /*  ************  Implementation of IteratorAggregate  ************  */
115 
116 
117     public function getIterator(){
118         return new ArrayIterator(array_merge($this->data, $this->modified));
119     }
120 
121 
122     /*  ************  Magic methods  ************  */
123 
124 
125     public function __get($name){
126         return array_key_exists($name, $this->modified)
127                 ? $this->modified[$name] : (isset($this->data[$name])
128                     ? $this->data[$name] : null);
129     }
130 
131 
132     public function __set($name, $value){
133             $this->modified[$name] = $value;
134     }
135 
136 
137     public function __isset($name){
138         return isset($this->data[$name]) || isset($this->modified[$name]);
139     }
140 
141 
142     public function __unset($name){
143         $this->modified[$name] = null;
144     }
145 
146 
147     /*  ************  Implementation of ArrayAccess  ************  */
148 
149 
150     public function offsetGet($offset){
151         return $this->__get($offset);
152     }
153 
154 
155     public function offsetSet($offset, $value){
156         $this->__set($offset, $value);
157     }
158 
159 
160     public function offsetExists($offset){
161         return $this->__isset($offset);
162     }
163 
164 
165     public function offsetUnset($offset){
166         $this->__unset($offset);
167     }
168 
169 
170 }
171 
Neevo Public API API documentation generated by ApiGen 2.8.0