9
9
class Triton
10
10
{
11
11
12
- private $ variables = ['data ' => [], 'type ' => 'single ' ], $ relation ;
12
+ private $ variables = ['data ' => [' triton ' => [ ' insert ' => true ] ], 'type ' => 'single ' ], $ relation ;
13
13
protected static $ db , $ table , $ id = 'id ' , $ dataManipulation ;
14
14
15
15
public function setType ($ type )
@@ -92,10 +92,12 @@ public static function find($id, $columns = '*', $type = 'object')
92
92
if (!isset ($ triton [$ file_name ])) {
93
93
require $ file ;
94
94
}
95
+
95
96
$ model = new $ called_class ;
96
- $ model ->setType ('multi ' );
97
- $ model ->setData (unserialize ($ triton ['find ' ][$ file_name ]));
98
- return $ model ->variables ['data ' ];
97
+ $ model ->setType ('single ' );
98
+ $ model ->setData (unserialize ($ triton ['find ' ][$ file_name ])[0 ]->variables ['data ' ]);
99
+ $ model ->noInsert ();
100
+ return $ model ;
99
101
}
100
102
else
101
103
{
@@ -107,8 +109,9 @@ public static function find($id, $columns = '*', $type = 'object')
107
109
{
108
110
$ model = new $ called_class ;
109
111
$ model ->setType ('single ' );
110
- $ model ->setData ($ result [0 ]);
111
- return $ model ->variables ['data ' ];
112
+ $ model ->setData ($ result [0 ]->variables ['data ' ]);
113
+ $ model ->noInsert ();
114
+ return $ model ;
112
115
}
113
116
else
114
117
{
@@ -126,7 +129,8 @@ public static function find($id, $columns = '*', $type = 'object')
126
129
{
127
130
$ model = new $ called_class ;
128
131
$ model ->setType ('single ' );
129
- $ model ->setData ($ result [0 ]);
132
+ $ model ->setData ($ result [0 ]->variables ['data ' ]);
133
+ $ model ->noInsert ();
130
134
return $ model ;
131
135
}
132
136
else
@@ -190,7 +194,11 @@ public function __get($name)
190
194
191
195
public function __set ($ name , $ value )
192
196
{
193
- $ this ->variables ['data ' ][$ name ] = $ value ;
197
+ if ($ this ->variables ['type ' ] == 'single ' )
198
+ {
199
+ $ this ->variables ['data ' ][$ name ] = $ value ;
200
+ $ this ->variables ['changed ' ][$ name ] = $ name ;
201
+ }
194
202
}
195
203
196
204
function __toString ()
@@ -267,36 +275,63 @@ public static function all($columns = '*', $type = 'object')
267
275
268
276
public static function add ($ data )
269
277
{
270
- static ::$ dataManipulation ['add ' ][] = $ data ;
271
- }
272
-
273
- public function save ()
274
- {
275
- error_reporting (E_ALL );
276
-
277
- }
278
-
279
- public function __destruct ()
280
- {
281
- $ GLOBALS ['_neptune ' ]['databases ' ] = null ;
282
- /*
283
- if (empty($GLOBALS['_neptune']['databases']))
284
- {
285
- require __DIR__ . '/../config/start.triton.php';
286
- }
278
+ $ db = self ::connectDatabase (static ::$ db );
287
279
$ column = '' ;
288
280
foreach (array_keys ($ data ) as $ columnname )
289
281
{
290
282
$ column .= $ columnname . ' = ?, ' ;
291
283
}
292
284
$ column = rtrim ($ column , ', ' );
293
- $insert = $GLOBALS['_neptune']['databases'][static::$db] ->prepare('INSERT INTO ' . static::$table . ' SET ' . $column);
285
+ $ insert = $ db ->prepare ('INSERT INTO ' . static ::$ table . ' SET ' . $ column );
294
286
$ result = $ insert ->execute (array_values ($ data ));
295
287
if ($ result !== false )
296
288
{
297
- return $GLOBALS['_neptune']['databases'][static::$db] ->lastInsertId();
289
+ return $ db ->lastInsertId ();
298
290
}
299
- */
291
+ }
292
+
293
+ private function noInsert ()
294
+ {
295
+ $ this ->variables ['data ' ]['triton ' ]['insert ' ] = false ;
296
+ }
297
+
298
+
299
+ public function save ()
300
+ {
301
+ $ db = self ::connectDatabase (static ::$ db );
302
+ $ column = '' ;
303
+ $ data = [];
304
+ if ($ this ->variables ['type ' ] == 'single ' ) {
305
+ if ($ this ->variables ['data ' ]['triton ' ]['insert ' ]) {
306
+ foreach ($ this ->variables ['changed ' ] as $ columnname ) {
307
+ $ column .= $ columnname . ' = ?, ' ;
308
+ $ data [] = $ this ->variables ['data ' ][$ columnname ];
309
+ }
310
+ $ column = rtrim ($ column , ', ' );
311
+ $ insert = $ db ->prepare ('INSERT INTO ' . static ::$ table . ' SET ' . $ column );
312
+ $ result = $ insert ->execute (array_values ($ data ));
313
+ if ($ result !== false ) {
314
+ return $ db ->lastInsertId ();
315
+ }
316
+ } else {
317
+ foreach ($ this ->variables ['changed ' ] as $ columnname ) {
318
+ $ column .= $ columnname . ' = ?, ' ;
319
+ $ data [] = $ this ->variables ['data ' ][$ columnname ];
320
+ }
321
+ $ column = rtrim ($ column , ', ' );
322
+ $ insert = $ db ->prepare ('UPDATE ' . static ::$ table . ' SET ' . $ column );
323
+ $ result = $ insert ->execute (array_values ($ data ));
324
+ if ($ result !== false ) {
325
+ return $ db ->lastInsertId ();
326
+ }
327
+ }
328
+ return false ;
329
+ }
330
+ }
331
+
332
+ public function __destruct ()
333
+ {
334
+ $ GLOBALS ['_neptune ' ]['databases ' ] = null ;
300
335
}
301
336
302
337
0 commit comments