Skip to content

Commit 9995978

Browse files
committed
Merge branch '3.0-beta' of https://github.com/simple-mvc-framework/framework into 3.0-db-orm
2 parents 508afd3 + 6598044 commit 9995978

File tree

2 files changed

+82
-28
lines changed

2 files changed

+82
-28
lines changed

app/Core/BaseModel.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,14 @@ public function insert($data, $skipValidation = null)
390390
if ($data !== false) {
391391
$data = $this->trigger('beforeInsert', array('method' => 'insert', 'fields' => $data));
392392

393-
$result = $this->db->insert($this->table(), $this->prepareData($data));
393+
// Prepare the Data
394+
$data = $this->prepareData($data);
395+
396+
// Get the parameter Types.
397+
$paramTypes = self::getParamTypes($data);
398+
399+
// Execute the INSERT.
400+
$result = $this->db->insert($this->table(), $data, $paramTypes);
394401

395402
if($result !== false) {
396403
$this->trigger('afterInsert', array('id' => $result, 'fields' => $data, 'method' => 'insert'));
@@ -423,9 +430,11 @@ public function replace($data, $skipValidation = null)
423430

424431
// Will be false if it didn't validate.
425432
if ($data !== false) {
433+
$data = $this->prepareData($data);
434+
426435
$paramTypes = self::getParamTypes($data);
427436

428-
$result = $this->db->replace($this->table(), $this->prepareData($data), $paramTypes);
437+
$result = $this->db->replace($this->table(), $data, $paramTypes);
429438

430439
if($result !== false) {
431440
$this->trigger('afterInsert', array('id' => $id, 'fields' => $data, 'method' => 'replace'));
@@ -460,7 +469,16 @@ public function update($id, $data, $skipValidation = null)
460469
if ($data !== false) {
461470
$data = $this->trigger('beforeUpdate', array('id' => $id, 'method' =>'update', 'fields' => $data));
462471

463-
$result = $this->db->update($this->table(), $this->prepareData($data), array($this->primaryKey => $id));
472+
// Prepare the Data.
473+
$data = $this->prepareData($data);
474+
475+
// Calculate the parameter Types.
476+
$paramTypes = self::getParamTypes($data);
477+
478+
$paramTypes[$this->primaryKey] = is_integer($id) ? PDO::PARAM_INT : PDO::PARAM_STR;
479+
480+
// Execute the UPDATE.
481+
$result = $this->db->update($this->table(), $data, array($this->primaryKey => $id), $paramTypes);
464482

465483
$result = $this->trigger('afterUpdate', array('id' => $id, 'method' => 'update', 'fields' => $data, 'result' => $result));
466484
}
@@ -513,29 +531,24 @@ public function updateMany($ids, $data, $skipValidation = null)
513531
//
514532
$this->db->connect();
515533

516-
// Prepare the SET command and parameter types.
517-
$set = array();
534+
// Prepare the SET command and parameter Types.
535+
$setFields = array();
518536

519537
$paramTypes = array();
520538

521-
foreach ($data as $columnName => $value) {
522-
$set[] = $columnName .' = :' .$columnName;
539+
foreach ($data as $field => $value) {
540+
$setFields[] = $field .' = :' .$field;
523541

524-
if (is_integer($value)) {
525-
$paramTypes[$columnName] = PDO::PARAM_INT;
526-
}
527-
else {
528-
$paramTypes[$columnName] = PDO::PARAM_STR;
529-
}
542+
$paramTypes[$field] = is_integer($value) ? PDO::PARAM_INT : PDO::PARAM_STR;
530543
}
531544

532545
// Prepare the parameters.
533-
$params = array_merge($data, array('ids' => $ids));
546+
$params['values'] = $ids;
534547

535-
$paramTypes['ids'] = Connection::PARAM_INT_ARRAY;
548+
$paramTypes['values'] = Connection::PARAM_INT_ARRAY;
536549

537550
// Prepare the SQL Statement.
538-
$sql = "UPDATE " .$this->table() ." SET " . implode(', ', $set) ." WHERE " .$this->primaryKey ." IN (:ids)";
551+
$sql = "UPDATE " .$this->table() ." SET " . implode(', ', $setFields) ." WHERE " .$this->primaryKey ." IN (:values)";
539552

540553
$result = $this->db->executeUpdate($sql, $params, $paramTypes);
541554

@@ -584,7 +597,14 @@ public function updateBy()
584597

585598
// Will be false if it didn't validate.
586599
if (($data = $this->validate($data)) !== false) {
587-
$result = $this->db->update($this->table(), $this->prepareData($data), $this->wheres());
600+
// Prepare the Data.
601+
$data = $this->prepareData($data);
602+
603+
// Calculate the parameter Types.
604+
$paramTypes = self::getParamTypes($data);
605+
606+
// Execute the UPDATE.
607+
$result = $this->db->update($this->table(), $data, $this->wheres(), $paramTypes);
588608

589609
$this->trigger('afterUpdate', array('method' => 'updateBy', 'fields' => $data, 'result' => $result));
590610
}
@@ -616,7 +636,14 @@ public function updateAll($data, $skipValidation = null)
616636

617637
// Will be false if it didn't validate.
618638
if ($data !== false) {
619-
$result = $this->db->update($this->table(), $this->prepareData($data), true);
639+
// Prepare the Data.
640+
$data = $this->prepareData($data);
641+
642+
// Calculate the parameter Types.
643+
$paramTypes = self::getParamTypes($data);
644+
645+
// Execute the UPDATE.
646+
$result = $this->db->update($this->table(), $data, array(1 => 1), $paramTypes);
620647

621648
$this->trigger('afterUpdate', array('method' => 'updateAll', 'fields' => $data, 'result' => $result));
622649
}
@@ -1461,14 +1488,20 @@ protected function parseSelectOrder()
14611488
return $result;
14621489
}
14631490

1464-
protected static function getParamTypes($params)
1491+
protected static function getParamTypes(array $params)
14651492
{
1466-
$result[] = array();
1493+
$result = array();
14671494

14681495
foreach ($params as $key => $value) {
14691496
if (is_integer($value)) {
14701497
$result[$key] = PDO::PARAM_INT;
14711498
}
1499+
else if (is_bool($value)) {
1500+
$result[$key] = PDO::PARAM_BOOL;
1501+
}
1502+
else if($value === null) {
1503+
$result[$key] = PDO::PARAM_NULL;
1504+
}
14721505
else {
14731506
$result[$key] = PDO::PARAM_STR;
14741507
}

app/Modules/Demo/Controllers/Models.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,31 @@ public function index()
8484
$message .= '<pre>'.var_export($retval, true).'</pre><br>';
8585

8686
//
87-
$members2 = $this->model->findAll();
87+
$members = $this->model->findAll();
8888

8989
$message .= '<b>$this->model->findAll();</b>';
90-
$message .= '<pre>'.var_export($members2, true).'</pre><br>';
90+
$message .= '<pre>'.var_export($members, true).'</pre><br>';
91+
92+
//
93+
$userInfo = array(
94+
'password' => 'testing',
95+
'email' => 'modified@novaframwork.dev'
96+
);
97+
98+
$message .= '<b>$userInfo</b>';
99+
$message .= '<pre>'.var_export($userInfo, true).'</pre><br>';
100+
101+
//
102+
$retval = $this->model->updateBy('username', 'virgil', $userInfo);
103+
104+
$message .= '<b>$this->model->updateBy(\'username\', \'virgil\', $userInfo);</b>';
105+
$message .= '<pre>'.var_export($retval, true).'</pre><br>';
106+
107+
//
108+
$members = $this->model->findAll();
109+
110+
$message .= '<b>$this->model->findAll();</b>';
111+
$message .= '<pre>'.var_export($members, true).'</pre><br>';
91112

92113
//
93114
$retval = $this->model->deleteBy('username', 'virgil');
@@ -96,16 +117,16 @@ public function index()
96117
$message .= '<pre>'.var_export($retval, true).'</pre><br>';
97118

98119
//
99-
$members3 = $this->model->order('desc')->findAll();
120+
$members = $this->model->order('desc')->findAll();
100121

101122
$message .= '<b>$this->model->order(\'desc\')->findAll();</b>';
102-
$message .= '<pre>'.var_export($members3, true).'</pre><br>';
123+
$message .= '<pre>'.var_export($members, true).'</pre><br>';
103124

104125
//
105-
$members4 = $this->model->orderBy('username', 'desc')->limit(2, 0)->findAll();
126+
$members = $this->model->orderBy('username', 'desc')->limit(2, 0)->findAll();
106127

107128
$message .= '<b>$this->model->orderBy(\'username\', \'desc\')->limit(2, 0)->findAll();</b>';
108-
$message .= '<pre>'.var_export($members4, true).'</pre><br>';
129+
$message .= '<pre>'.var_export($members, true).'</pre><br>';
109130

110131
//
111132
$result = $this->model->findBy('username', 'marcus');
@@ -119,10 +140,10 @@ public function index()
119140
$message .= '<b>$this->model->find(3);</b><pre>'.var_export($result, true).'</pre><br>';
120141

121142
//
122-
$members5 = $this->model->orderBy('username', 'desc')->findMany(array(1, 3));
143+
$members = $this->model->orderBy('username', 'desc')->findMany(array(1, 3));
123144

124145
$message .= '<b>$this->model->orderBy(\'username\', \'desc\')->findMany(array(1, 3));</b>';
125-
$message .= '<pre>'.var_export($members5, true).'</pre><br>';
146+
$message .= '<pre>'.var_export($members, true).'</pre><br>';
126147

127148
// Setup the View variables.
128149
$this->title(__d('demo', 'Base Model Demo'));

0 commit comments

Comments
 (0)