Overview

Packages

  • Onion::Controllers
  • Onion::Core
  • Onion::UI
  • Onion::Utils

Classes

  • Authenticator
  • Controller
  • Database
  • DatabaseResult
  • Log
  • Model
  • Onion
  • Request
  • Response
  • User
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Onion Framework - Generický model
  5:  *
  6:  * Copyright (c) 2011 Jano Gašpar (http://webstranky.net)
  7:  *
  8:  * @author    Jano Gašpar
  9:  * @copyright Copyright (c) 2011 Jano Gašpar
 10:  * @package   Onion::Core
 11:  **/
 12: class Model implements Iterator, Countable, ArrayAccess
 13: {
 14:     /**
 15:      * @var object Database
 16:      */
 17:     private $db;
 18: 
 19: 
 20:     /**
 21:      * @var string tabuľka v databáze
 22:      */
 23:     private $table;
 24: 
 25: 
 26:     /**
 27:      * @var array dáta modelu
 28:      */
 29:     private $data = array();
 30: 
 31: 
 32:     /**
 33:       * @var int
 34:       */
 35:     private $pointer;
 36: 
 37: 
 38:     /**
 39:      * Továrnička
 40:      *
 41:      * @param  string názov tabuľky s modelmi
 42:      * @param  array  pole s podmienkami na výber dát modelu
 43:      * @return object
 44:      */
 45:     public static function create($table, $where = NULL)
 46:     {
 47:         $model = new self;
 48: 
 49:         $model->pointer = 0;
 50: 
 51:         $model->db = new Database($table);
 52:         $model->table = $table;
 53: 
 54:         if (empty($where) === TRUE) {
 55:             return $model;
 56:         }
 57: 
 58:         if (is_array($where) === TRUE) {
 59:             $model->db->where($where);
 60: 
 61:         } else {
 62:             $model->db->where('id', $where);
 63:         }
 64: 
 65:         $model->db->limit(1);
 66: 
 67:         $model->data = $model->db->get_row();
 68: 
 69:         if ($model->data === FALSE) {
 70:             return FALSE;
 71:         }
 72: 
 73:         return $model;
 74:     }
 75: 
 76: 
 77:     public static function load($table, $where)
 78:     {
 79:         return self::create($table, $where);
 80:     }
 81: 
 82: 
 83:     public function data($data = NULL, $merge = FALSE)
 84:     {
 85:         if ($data === NULL) {
 86:             return $this->data;
 87:         }
 88: 
 89:         if ($merge === FALSE) {
 90:             $this->data = $data;
 91: 
 92:         } else {
 93:             foreach ($data as $key => $value) {
 94:                 $this->data[$key] = $value;
 95:             }
 96:         }
 97: 
 98:         return $this;
 99:     }
100: 
101: 
102:     /**
103:      * Uloženie modelu
104:      *
105:      * @return bool|int FALSE alebo ID modelu
106:      */
107:     public function save()
108:     {
109:         if (empty($this->data['id']) === TRUE) {
110:             $this->data['id'] = NULL;
111:             unset($this->data['id']);
112:         }
113: 
114:         return $this->db->save($this->data);
115:     }
116: 
117: 
118:     public function delete()
119:     {
120:         return $this->db->delete($this->data['id']);
121:     }
122: 
123: 
124:     /**
125:      * Metóda na vrátenie hodnoty vlastnosti modelu
126:      *
127:      * @param  string meno vlastnosti
128:      * @return mixed  hodnota vlastnosti
129:      */
130:     final public function &__get($property)
131:     {
132:         if ($property === 'db') {
133:             return $this->db;
134:         }
135: 
136:         if ($property === 'data') {
137:             return $this->data;
138:         }
139: 
140:         $value = NULL;
141:         if (isset($this->data[$property]) === TRUE) {
142:             $value = $this->data[$property];
143:         }
144: 
145:         return $value;
146:     }
147: 
148: 
149:     /**
150:      * Metóda na nastavenie hodnoty vlastnosti modelu
151:      *
152:      * @param  string meno vlastnosti
153:      * @return void
154:      */
155:     final public function __set($property, $value)
156:     {
157:         if ($property === 'data'
158:             AND (is_array($value) === TRUE
159:                 OR is_object($value) === TRUE)) {
160: 
161:             $this->data = $value;
162:             return;
163:         }
164: 
165:         $this->data[$property] = $value;
166:     }
167: 
168: 
169:     /**
170:      * Rewinds the iterator to the first element.
171:      * @return void
172:      */
173:     public function rewind()
174:     {
175:         $this->pointer = 0;
176:     }
177: 
178: 
179:     /**
180:      * Returns the key of the current element.
181:      * @return mixed
182:      */
183:     public function key()
184:     {
185:         return $this->pointer;
186:     }
187: 
188: 
189:     /**
190:      * Returns the current element.
191:      * @return mixed
192:      */
193:     public function current()
194:     {
195:         return $this->data[$this->pointer];
196:     }
197: 
198: 
199: 
200:     /**
201:      * Moves forward to next element.
202:      * @return void
203:      */
204:     public function next()
205:     {
206:         $this->pointer++;
207:     }
208: 
209: 
210: 
211:     /**
212:      * Checks if there is a current element after calls to rewind() or next().
213:      * @return bool
214:      */
215:     public function valid()
216:     {
217:         return isset($this->data[$this->pointer]);
218:     }
219: 
220: 
221: 
222:     /**
223:      * Required by the Countable interface.
224:      * @return int
225:      */
226:     public function count()
227:     {
228:         return count($this->data);
229:     }
230: 
231: 
232:     public function offsetSet($pointer, $value)
233:     {
234:         if (is_null($pointer)) {
235:             $this->data[] = $value;
236: 
237:         } else {
238:             $this->data[$pointer] = $value;
239:         }
240:     }
241: 
242: 
243:     public function offsetExists($pointer)
244:     {
245:         return isset($this->data[$pointer]);
246:     }
247: 
248: 
249:     public function offsetUnset($pointer)
250:     {
251:         unset($this->data[$pointer]);
252:     }
253: 
254: 
255:     public function offsetGet($pointer)
256:     {
257:         return $this->data[$pointer];
258:     }
259: }
260: 
Onion API documentation generated by ApiGen.
Generated using the TokenReflection library.