@@ -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 ();
0 commit comments