Example #1
0
 public function save()
 {
     $updateData = [];
     foreach (static::$schema as $key => $value) {
         if ($key != 'id') {
             $updateData[$key] = self::parseToSql($value['type'], $this->{$key});
         }
     }
     $execParams = array_values($updateData);
     // exists
     if ($this->id) {
         $rq = "UPDATE " . self::getTableName() . " SET ";
         // create field assoc
         $tmp = [];
         foreach ($updateData as $key => $value) {
             $tmp[] = "{$key} = ?";
         }
         $rq .= implode(', ', $tmp);
         $rq .= ' WHERE id = ?';
         $execParams[] = $this->id;
         $saveQ = new Query($rq);
         $saveQ->execute($execParams);
     } else {
         $rq = "INSERT INTO " . self::getTableName() . " (" . implode(', ', array_keys($updateData)) . ") " . "VALUES (" . implode(', ', array_fill(0, count($updateData), '?')) . ")";
         $saveQ = new Query($rq);
         $saveQ->execute($execParams);
         $this->id = intval(Query::getConnexion()->lastInsertId());
     }
 }