Skip to content

Commit f6c8166

Browse files
committed
Development
1 parent ab9df5c commit f6c8166

File tree

6 files changed

+157
-105
lines changed

6 files changed

+157
-105
lines changed

.idea/workspace.xml

+89-74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
echo '<hr/>';
3838
}
3939

40-
var_dump(User::find(3)->delete());
40+
$user = User::find(7);
41+
$user->picture = 'https://scontent-vie1-1.xx.fbcdn.net/v/t1.0-9/18157157_1118743538230524_7021930993496921834_n.jpg?oh=3ee6cd7a3bbd195a59bec5920d46816d&oe=59AFD988';
42+
$user->save();
43+
44+
4145

4246
?>
4347
<style>
1.01 KB
Binary file not shown.

triton/temp/e051569ca7573678d55b730c3ff753eb.php

-1
This file was deleted.

triton/triton/Triton.php

+63-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Triton
1010
{
1111

12-
private $variables = ['data' => [], 'type' => 'single'], $relation;
12+
private $variables = ['data' => ['triton' => ['insert' => true]], 'type' => 'single'], $relation;
1313
protected static $db, $table, $id = 'id', $dataManipulation;
1414

1515
public function setType($type)
@@ -92,10 +92,12 @@ public static function find($id, $columns = '*', $type = 'object')
9292
if(!isset($triton[$file_name])) {
9393
require $file;
9494
}
95+
9596
$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;
99101
}
100102
else
101103
{
@@ -107,8 +109,9 @@ public static function find($id, $columns = '*', $type = 'object')
107109
{
108110
$model = new $called_class;
109111
$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;
112115
}
113116
else
114117
{
@@ -126,7 +129,8 @@ public static function find($id, $columns = '*', $type = 'object')
126129
{
127130
$model = new $called_class;
128131
$model->setType('single');
129-
$model->setData($result[0]);
132+
$model->setData($result[0]->variables['data']);
133+
$model->noInsert();
130134
return $model;
131135
}
132136
else
@@ -190,7 +194,11 @@ public function __get($name)
190194

191195
public function __set($name, $value)
192196
{
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+
}
194202
}
195203

196204
function __toString()
@@ -267,36 +275,63 @@ public static function all($columns = '*', $type = 'object')
267275

268276
public static function add($data)
269277
{
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);
287279
$column = '';
288280
foreach (array_keys($data) as $columnname)
289281
{
290282
$column .= $columnname . ' = ?, ';
291283
}
292284
$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);
294286
$result = $insert->execute(array_values($data));
295287
if($result !== false)
296288
{
297-
return $GLOBALS['_neptune']['databases'][static::$db]->lastInsertId();
289+
return $db->lastInsertId();
298290
}
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;
300335
}
301336

302337

triton/triton/TritonWhere.php

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ public function execute($columns = '*', $type = 'object')
101101
$selectWhere->execute($bind);
102102

103103
$result = ($type == 'object') ? $selectWhere->fetchAll(PDO::FETCH_CLASS, $this->called) : $selectWhere->fetchAll(PDO::FETCH_ASSOC);
104-
var_dump($result);
105104
file_put_contents($file, '<?php $triton[\'where\'][\'execute\'][\'' . $file_name . '\'] = \'' . serialize($result) . '\';');
106105
if ($type == 'object')
107106
{

0 commit comments

Comments
 (0)