1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 14 15 16 17
18 class NeevoRow implements ArrayAccess, Countable, IteratorAggregate {
19
20
21
22 protected $frozen;
23
24
25 protected $primary;
26
27
28 protected $data = array();
29
30
31 protected $modified = array();
32
33
34 protected $table;
35
36
37 protected $connection;
38
39
40 41 42 43 44 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 59 60 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 76 77 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 90 91
92 public function toArray(){
93 return array_merge($this->data, $this->modified);
94 }
95
96
97 98 99 100
101 public function isFrozen(){
102 return (bool) $this->frozen;
103 }
104
105
106
107
108
109 public function count(){
110 return count(array_merge($this->data, $this->modified));
111 }
112
113
114
115
116
117 public function getIterator(){
118 return new ArrayIterator(array_merge($this->data, $this->modified));
119 }
120
121
122
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
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