public function save(Models\Picture &$picture) { $modelArray = $this->converter->modelToEntityArray($picture); //Prevent someone from setting a different ID for a preexisting entry. if (isset($modelArray['id'])) { unset($modelArray['id']); } $keys = array_keys($modelArray); $vals = array_values($modelArray); if (isset($picture->id)) { $query = $this->pdo->prepare('UPDATE picture SET ' . implode('=?, ', $keys) . '=? WHERE id=? LIMIT 1'); $vals[] = $picture->id; return $query->execute($vals); } else { $query = $this->pdo->prepare('INSERT INTO picture (' . implode(',', $keys) . ') VALUES (' . implode(',', array_fill(0, count($vals), '?')) . ')'); if ($query->execute($vals)) { //Refetch to populate everything properly. $refetched = $this->getAll(['id' => $this->pdo->lastInsertId()], 1); $picture = $refetched[0]; return true; } } return false; }