88class Model extends Database
99{
1010 private $ stmt ;
11- private $ dados ;
11+ private $ data ;
1212 private $ sql ;
13+ private $ where ;
14+ private $ fields ;
1315 public $ count ;
1416
1517 public function __construct ()
@@ -33,53 +35,63 @@ private function setPrimaryKey()
3335 }
3436 }
3537
36- private function param ($ dados = null )
38+ private function param ($ data = null )
3739 {
38- if (empty ($ dados )) {
39- $ dados = $ this ->dados ['conditions ' ];
40+ if (empty ($ data )) {
41+ $ data = $ this ->dados ['conditions ' ];
4042 }
4143
42- foreach ($ dados as $ k => $ v ) {
44+ foreach ($ data as $ k => $ v ) {
4345 $ tipo = (is_int ($ v )) ? PDO ::PARAM_INT : PDO ::PARAM_STR ;
4446 $ this ->stmt ->bindValue (": {$ k }" , $ v , $ tipo );
4547 }
4648 }
4749
48- private function fields ($ dados = null )
50+ private function fields ($ data = null )
4951 {
50- if (empty ($ dados )) {
51- return implode (', ' , $ this ->dados ['fields ' ]);
52+ if (empty ($ data ) && array_key_exists ( ' fields ' , $ this -> dados )) {
53+ return implode (', ' , $ this ->dados ['fields ' ]);
5254 }
5355
54- foreach ($ dados as $ k => $ v ) {
55- $ fields [] = $ k ;
56+ if ( ! empty ($ data )) {
57+ foreach ($ data as $ k => $ v ) {
58+ $ fields [] = $ k ;
59+ }
60+ return implode (', ' , $ fields );
5661 }
57- $ fields = implode (', ' , $ fields );
5862
59- return $ fields ;
63+ return ' * ' ;
6064 }
6165
6266 private function conditions ($ separator )
6367 {
68+ $ param = [];
6469 foreach ($ this ->dados ['conditions ' ] as $ k => $ v ) {
65- $ where [] = "{$ k } = : {$ k }" ;
70+ $ param [] = "{$ k } = : {$ k }" ;
6671 }
6772
68- return 'WHERE ' .implode ($ separator , $ where );
73+ return implode ($ separator , $ param );
74+ }
75+
76+ private function where ()
77+ {
78+ return $ this ->where = (array_key_exists ('conditions ' , $ this ->dados ))
79+ ? 'WHERE ' . self ::conditions (' AND ' )
80+ : '' ;
6981 }
7082
7183 private function find ()
7284 {
73- $ fields = (isset ($ this ->dados ['fields ' ])) ? self ::fields () : '* ' ;
74- $ where = (isset ($ this ->dados ['conditions ' ])) ? self ::conditions (' AND ' ) : '' ;
75- $ sql = "SELECT {$ fields } FROM {$ this ->table } {$ where }" ;
85+ $ sql = "SELECT " .self ::fields ()." FROM {$ this ->table } " .self ::where ();
86+
7687 $ this ->stmt = $ this ->conn ->prepare ($ sql );
7788
78- if ( ! empty ($ where )) {
89+ if ( ! empty ($ this -> where )) {
7990 self ::param ();
8091 }
8192
82- return $ this ->stmt ->execute ();
93+ $ this ->stmt ->execute ();
94+ return $ this ;
8395 }
8496
8597 private function values ()
@@ -91,27 +103,43 @@ private function values()
91103 return implode (', ' , $ values );
92104 }
93105
94- private function insert ()
106+ private function insertQueryString ()
95107 {
96108 $ fields = self ::fields ($ this ->dados );
97109 $ values = self ::values ();
98- $ sql = "INSERT INTO {$ this ->table } ( {$ fields }) VALUES ( {$ values }) " ;
99110
100- return $ sql ;
111+ return "INSERT INTO {$ this ->table } ( {$ fields }) VALUES ( {$ values }) " ;
112+ }
113+
114+ private function updateWhere ($ data )
115+ {
116+ $ this ->dados ['conditions ' ] = [$ this ->pk => $ data [$ this ->pk ]];
117+ $ where = 'WHERE ' .self ::conditions ('' );
118+ unset($ data [$ this ->pk ]);
119+
120+ return $ where ;
101121 }
102122
103- public function findAll ( $ dados = null )
123+ private function updateQueryString ( $ data )
104124 {
105- $ this ->dados = $ dados ;
106- self ::find ();
107- return $ this ->stmt ->fetchAll (PDO ::FETCH_ASSOC );
125+ $ this ->dados ['conditions ' ] = $ data ;
126+ $ fields = self ::conditions (', ' );
127+
128+ return "UPDATE {$ this ->table } SET {$ fields } {$ this ->where }" ;
108129 }
109130
110- public function findOne ( $ dados = null )
131+ public function findAll ( $ data = null )
111132 {
112- $ this ->dados ['conditions ' ] = $ dados ;
113- self ::find ();
114- return $ this ->stmt ->fetch (PDO ::FETCH_ASSOC );
133+ $ this ->dados = $ data ;
134+ return $ this ->find ()
135+ ->stmt ->fetchAll (PDO ::FETCH_ASSOC );
136+ }
137+
138+ public function findOne ($ data )
139+ {
140+ $ this ->dados ['conditions ' ] = $ data ;
141+ return $ this ->find ()
142+ ->stmt ->fetch (PDO ::FETCH_ASSOC );
115143 }
116144
117145 public function findById ($ id )
@@ -123,49 +151,48 @@ public function query($sql)
123151 {
124152 $ this ->stmt = $ this ->conn ->prepare ($ sql );
125153 $ this ->stmt ->execute ();
126- $ result = $ this ->stmt ->fetchAll (PDO ::FETCH_ASSOC );
154+ $ result = $ this ->stmt ->fetchAll (PDO ::FETCH_ASSOC );
127155 $ this ->count = count ($ result );
128156
129157 return $ result ;
130158 }
131159
132- public function save ($ dados )
160+ public function save ($ data )
133161 {
134- if (isset ( $ dados [ $ this ->pk ] )) {
135- $ this ->find ([$ this ->pk => $ dados [$ this ->pk ]]);
162+ if (array_key_exists ( $ this ->pk , $ data )) {
163+ $ this ->count = $ this -> findOne ([$ this ->pk => $ data [$ this ->pk ]]);
136164 }
137165
138- if ($ this ->count > 0 ) {
139- return $ this ->update ($ dados );
166+ if (! empty ( $ this ->count ) ) {
167+ return $ this ->update ($ data );
140168 }
141169
142- return $ this ->create ($ dados );
170+ return $ this ->create ($ data );
143171 }
144172
145- public function update ($ dados )
173+ public function update ($ data )
146174 {
147- $ param = $ dados ;
148-
149- $ this ->dados ['conditions ' ] = [$ this ->pk => $ dados [$ this ->pk ]];
150- $ where = self ::conditions ('' );
151- unset($ dados [$ this ->pk ]);
152- $ this ->dados ['conditions ' ] = $ dados ;
153- $ fields = str_replace ('WHERE ' , '' , self ::conditions (', ' ));
175+ if ( ! array_key_exists ($ this ->pk , $ data )) {
176+ return false ;
177+ }
154178
155- $ sql = "UPDATE {$ this ->table } SET {$ fields } {$ where }" ;
156- $ this ->stmt = $ this ->conn ->prepare ($ sql );
179+ $ param = $ data ;
180+ $ this ->where = self ::updateWhere ($ data );
181+ $ this ->stmt = $ this ->conn ->prepare (self ::updateQueryString ($ data ));
157182 self ::param ($ param );
158183 $ this ->stmt ->execute ();
159184 $ this ->count = $ this ->stmt ->rowCount ();
160185 }
161186
162- public function create ($ dados )
187+ public function create ($ data )
163188 {
164- $ this ->dados = $ dados ;
189+ $ this ->dados = $ data ;
190+
191+ $ this ->stmt = $ this ->conn ->prepare (self ::insertQueryString ());
192+ self ::param ($ data );
165193
166- $ this ->stmt = $ this ->conn ->prepare (self ::insert ());
167- self ::param ($ dados );
168194 $ this ->stmt ->execute ();
169195 $ this ->count = $ this ->stmt ->rowCount ();
170196 }
197+
171198}
0 commit comments