Example #1
0
 public function GetOneById($id)
 {
     $idProperty = $this->model->GetIDProperty();
     $properties = $this->model->GetProperties();
     $sql = "SELECT * FROM {$this->model->GetDbTable()} WHERE {$properties[$idProperty]['col']} = :id";
     $values = ['id' => $id];
     $types = [$properties[$idProperty]['type']];
     $results = $this->reader->fetchAll($sql, $values, $types);
     if (!empty($results)) {
         return new $this->modelClass($results[0]);
     } else {
         return false;
     }
 }
Example #2
0
 private function update(Model $model)
 {
     $idProperty = $model->GetIDProperty();
     $properties = $model->GetProperties();
     $modified = $model->GetModifiedProperties();
     if (!empty($modified)) {
         $cols = [];
         $types = [];
         $values = [];
         foreach ($modified as $property => $dbMap) {
             $cols[] = $dbMap['col'];
             $types[] = $dbMap['type'];
             $values[$dbMap['col']] = $dbMap['value'];
         }
         $sql = "UPDATE {$model->GetDbTable()} SET ";
         foreach ($cols as $col) {
             $sql .= "{$col} = :{$col},";
         }
         $sql = substr($sql, 0, -1);
         // remove trailing comma
         $sql .= " WHERE {$properties[$idProperty]['col']} = :{$properties[$idProperty]['col']}";
         $types[] = $properties[$idProperty]['type'];
         $values[$properties[$idProperty]['col']] = $properties[$idProperty]['value'];
         $this->writer->executeQuery($sql, $values, $types);
     }
 }