@@ -17,12 +17,26 @@ public function save()
1717 $ tableName = $ this ->tableName ();
1818 $ attributes = $ this ->attributes ();
1919 $ params = array_map (fn ($ attr ) => ": $ attr " , $ attributes );
20- $ statement = self ::prepare ("INSERT INTO $ tableName ( " .implode (', ' , $ attributes ).")
21- VALUES( " .implode (', ' , $ params ).") "
22- );
2320
24- foreach ($ attributes as $ attribute ) {
25- $ statement ->bindValue (": $ attribute " , $ this ->{$ attribute });
21+ if ($ this ->id ) {
22+ $ query_string = '' ;
23+ foreach ($ attributes as $ attribute ) {
24+ $ query_string .= "$ attribute=: $ attribute, " ;
25+ }
26+ $ query_string = rtrim ($ query_string , ', ' );
27+ $ statement = self ::prepare ("UPDATE $ tableName SET $ query_string WHERE id= $ this ->id ; " );
28+
29+ foreach ($ attributes as $ attribute ) {
30+ $ statement ->bindValue (": $ attribute " , $ this ->{$ attribute });
31+ }
32+ } else {
33+ $ statement = self ::prepare ("INSERT INTO $ tableName ( " .implode (', ' , $ attributes ).")
34+ VALUES( " .implode (', ' , $ params ).") "
35+ );
36+
37+ foreach ($ attributes as $ attribute ) {
38+ $ statement ->bindValue (": $ attribute " , $ this ->{$ attribute });
39+ }
2640 }
2741
2842 $ statement ->execute ();
@@ -34,7 +48,7 @@ public function prepare($sql)
3448 return Application::$ app ->db ->pdo ->prepare ($ sql );
3549 }
3650
37- public function findOne ($ where )
51+ public function get ($ where )
3852 {
3953 $ tableName = static ::tableName ();
4054 $ attributes = array_keys ($ where );
0 commit comments