1: <?php
2:
3: 4: 5: 6: 7:
8: class Model extends Eloquent {
9:
10: protected $metaData = null;
11: protected $primaryKey = "id";
12: protected $guarded = array('id');
13:
14: 15: 16: 17: 18:
19: public static function getInstance()
20: {
21: $a = func_get_args();
22: $i = func_num_args();
23:
24: $tmpInstance = new static;
25:
26: if (method_exists($tmpInstance, $f='getInstance'.$i)) {
27: return call_user_func_array(array($tmpInstance,$f),$a);
28: }
29: }
30:
31: 32: 33: 34: 35:
36: private static function getInstance1($tableName = null)
37: {
38: $model = new Model(array(), $tableName);
39: return $model;
40: }
41:
42: 43: 44: 45: 46:
47: private static function getInstance2($attributes, $tableName = null)
48: {
49: $model = new Model($attributes, $tableName);
50: return $model;
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: function __construct()
62: {
63: $a = func_get_args();
64: $i = func_num_args();
65: if (method_exists($this,$f='__construct'.$i)) {
66: call_user_func_array(array($this,$f),$a);
67: }
68: }
69:
70: private function __construct1(array $attributes = array())
71: {
72: parent::__construct($attributes);
73: }
74:
75: private function __construct2(array $attributes = array(), $table = null)
76: {
77: $this->table = $table;
78: $this->setTable($table);
79: $this->setMetaData($table);
80: $this->setGuarded(array($this->metaData['table']['pk_name']));
81: parent::__construct($attributes);
82: }
83:
84:
85: 86: 87: 88: 89:
90: public function getTable()
91: {
92: if (isset($this->table))
93: return $this->table;
94:
95: return null;
96: }
97:
98: 99: 100: 101: 102: 103:
104: public function setHasOne($table, $customKey)
105: {
106: return $this->hasOne($table, $customKey);
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function setHasMany($table, $customKey)
116: {
117: return $this->hasMany($table, $customKey);
118: }
119:
120: 121: 122: 123: 124: 125:
126: public function setBelongsTo($table, $customKey)
127: {
128: return $this->belongsTo($table, $customKey);
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138:
139: public function setBelongsToMany($table, $pivotTable, $remoteId, $localId)
140: {
141: return $this->belongsToMany($table, $pivotTable, $remoteId, $localId);
142: }
143:
144: public function setGuarded($guardedA)
145: {
146: $this->guarded = $guardedA;
147: }
148:
149: public function getA()
150: {
151:
152: }
153:
154: 155: 156:
157: public function insertRec()
158: {
159: $fields = $this->metaData['fields_array'];
160:
161: $updateA = array();
162: foreach ($fields as $field)
163: {
164: if ($this->isFillable($field['name']))
165: {
166: $updateA[$field['name']] = Input::get($field['name']);
167: }
168: }
169:
170: $id = DB::table($this->table)->insertGetId($updateA);
171:
172: return $id;
173: }
174:
175: 176: 177: 178: 179:
180: private function __editGet($fields)
181: {
182: $updateA = array();
183: foreach ($fields as $field)
184: {
185: if ($this->isFillable($field['name']))
186: {
187: $updateA[$field['name']] = Input::get($field['name']);
188: }
189: }
190: return $updateA;
191: }
192:
193: 194: 195: 196: 197: 198:
199: private function __editJson($fields, $json)
200: {
201: die;
202:
203: $updateA = array();
204:
205: $json = iconv("windows-1250", "UTF-8", $json);
206: $input = json_decode($json);
207: foreach ($fields as $field)
208: {
209: if ($this->isFillable($field['name']) && isset($input[$field['name']]))
210: {
211: $updateA[$field['name']] = $input[$field['name']];
212: }
213: }
214: return $updateA;
215: }
216:
217: 218: 219: 220: 221: 222:
223: public function editRec($pkValue, $json = null)
224: {
225:
226: $pkName = $this->metaData['table']['pk_name'];
227:
228: $fields = $this->metaData['fields_array'];
229:
230: if (empty($json))
231: {
232: $updateA = $this->__editGet($fields);
233: }
234: else
235: {
236: $updateA = $this->__editJson($fields, $json);
237: }
238:
239:
240: DB::table($this->table)->where($pkName, '=', $pkValue)->update($updateA);
241:
242: return $this;
243: }
244:
245: 246: 247: 248: 249:
250: public function setTable($tableName)
251: {
252: $this->table = $tableName;
253: }
254:
255: 256: 257: 258: 259:
260: public function setMetaData($tableName)
261: {
262: $this->metaData = Table::getTableMeta($tableName);
263: }
264:
265: 266: 267: 268: 269:
270: public function getMetaData()
271: {
272: return $this->metaData;
273: }
274:
275: }
276:
277: ?>
278: