Skip to content

Commit af46b4a

Browse files
committed
refactoring
1 parent c3e9536 commit af46b4a

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ Delete
6868
```
6969
Read
7070
```php
71-
$this->Users->find(); // Select all data
72-
$this->Users->find(['id' => 1]); // Select data with id = 1
71+
$this->Users->findAll(); // fetchAll
72+
$this->Users->findOne(['email' => 'arojunior@gmail.com']);
73+
$this->Users->findById($id);
7374
```
7475
Row count (select/insert/update/delete)
7576
```php
@@ -116,7 +117,7 @@ class Example extends AppModel
116117

117118
public function getAll()
118119
{
119-
return $this->find();
120+
return $this->findAll();
120121
}
121122
}
122123
```

app/controller/UsersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function index()
1414

1515
public function get()
1616
{
17-
$result = $this->Users->findAll();
17+
$result = $this->Users->findById('banyar@yahoo.com');
1818

1919
return ['list' => $this->Helper->toJson($result)];
2020
}

app/model/Users.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
class Users extends AppModel implements UsersInterface
99
{
1010
protected $table = 't_user';
11-
protected $pk = 'user_id';
12-
13-
public function findAll()
14-
{
15-
return $this->find();
16-
}
11+
protected $pk = 'email';
1712

1813
public function store($data)
1914
{

app/model/contracts/UsersInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
interface UsersInterface
66
{
7-
public function findAll();
8-
97
public function store($data);
108

119
public function remove($id);

core/model/Model.php

Lines changed: 56 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@ class Model extends Database
1010
private $stmt;
1111
private $dados;
1212
private $sql;
13-
public $count;
13+
public $count;
14+
15+
public function __construct()
16+
{
17+
parent::__construct();
18+
self::setTable();
19+
self::setPrivateKey();
20+
}
21+
22+
private function setTable()
23+
{
24+
if (!isset($this->table)) {
25+
$this->table = strtolower(get_class($this));
26+
}
27+
}
28+
29+
private function setPrivateKey()
30+
{
31+
if (!isset($this->pk)) {
32+
$this->pk = 'id';
33+
}
34+
}
1435

1536
private function param($dados = null)
1637
{
@@ -47,41 +68,18 @@ private function conditions($separator)
4768
return 'WHERE '.implode($separator, $where);
4869
}
4970

50-
private function table()
51-
{
52-
return (isset($this->table)) ? $this->table : strtolower(get_class($this));
53-
}
54-
55-
public function find($dados = null)
71+
private function find()
5672
{
57-
$this->dados = $dados;
58-
5973
$fields = (isset($this->dados['fields'])) ? self::fields() : '*';
60-
$table = self::table();
6174
$where = (isset($this->dados['conditions'])) ? self::conditions(' AND ') : '';
62-
$sql = "SELECT {$fields} FROM {$table} {$where}";
75+
$sql = "SELECT {$fields} FROM {$this->table} {$where}";
6376
$this->stmt = $this->conn->prepare($sql);
6477

6578
if (!empty($where)) {
6679
self::param();
6780
}
6881

69-
$this->stmt->execute();
70-
71-
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
72-
$this->count = count($result);
73-
74-
return $result;
75-
}
76-
77-
public function query($sql)
78-
{
79-
$this->stmt = $this->conn->prepare($sql);
80-
$this->stmt->execute();
81-
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
82-
$this->count = count($result);
83-
84-
return $result;
82+
return $this->stmt->execute();
8583
}
8684

8785
private function values()
@@ -97,18 +95,42 @@ private function insert()
9795
{
9896
$fields = self::fields($this->dados);
9997
$values = self::values();
100-
$table = self::table();
101-
$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
98+
$sql = "INSERT INTO {$this->table} ({$fields}) VALUES ({$values})";
10299

103100
return $sql;
104101
}
105102

106-
public function save($dados)
103+
public function findAll($dados = null)
107104
{
108-
if (!isset($this->pk)) {
109-
$this->pk = 'id';
110-
}
105+
$this->dados = $dados;
106+
self::find();
107+
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
108+
}
111109

110+
public function findOne($dados = null)
111+
{
112+
$this->dados['conditions'] = $dados;
113+
self::find();
114+
return $this->stmt->fetch(PDO::FETCH_ASSOC);
115+
}
116+
117+
public function findById($id)
118+
{
119+
return self::findOne([$this->pk => $id]);
120+
}
121+
122+
public function query($sql)
123+
{
124+
$this->stmt = $this->conn->prepare($sql);
125+
$this->stmt->execute();
126+
$result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
127+
$this->count = count($result);
128+
129+
return $result;
130+
}
131+
132+
public function save($dados)
133+
{
112134
if (isset($dados[$this->pk])) {
113135
$this->find([$this->pk => $dados[$this->pk]]);
114136
}
@@ -123,19 +145,14 @@ public function save($dados)
123145
public function update($dados)
124146
{
125147
$param = $dados;
126-
$table = self::table();
127-
128-
if (!isset($this->pk)) {
129-
$this->pk = 'id';
130-
}
131148

132149
$this->dados['conditions'] = [$this->pk => $dados[$this->pk]];
133150
$where = self::conditions('');
134151
unset($dados[$this->pk]);
135152
$this->dados['conditions'] = $dados;
136153
$fields = str_replace('WHERE', '', self::conditions(','));
137154

138-
$sql = "UPDATE {$table} SET {$fields} {$where}";
155+
$sql = "UPDATE {$this->table} SET {$fields} {$where}";
139156
$this->stmt = $this->conn->prepare($sql);
140157
self::param($param);
141158
$this->stmt->execute();

index.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
require 'constants.php';
77
require __DIR__ . '/vendor/autoload.php';
88

9-
require_once CORE_MODEL;
10-
require_once CORE_CONTROLLER;
11-
129
use SimpleORM\app\controller;
1310

1411
$uri = $_SERVER['REQUEST_URI'];
@@ -40,16 +37,14 @@
4037
/*
4138
* call current class/method
4239
*/
43-
$controller_file = APP_CONTROLLER . $controller . PHP;
44-
4540
try {
46-
require $controller_file;
4741
$load_class = 'SimpleORM\app\controller\\' . $controller;
4842
$class = new $load_class();
4943
$set = $class->$method($the_request);
5044
} catch(Exception $e) {
5145
echo 'No '.$controller.' found for this route', $e->getMessage(), "\n";
5246
}
47+
5348
/*
5449
* Declare all variables if passed in return
5550
*/

0 commit comments

Comments
 (0)