public function getAll(array $filters = [], $count = 10, $start = 0) { $temp = new Models\Fossil(); //This along with pdo prepared statements should prevent a sql injection attack $columns = $this->converter->filterArrayToSqlColumns($filters, $temp); $whereClause = empty($columns) ? '' : 'WHERE ' . implode('=? AND ', array_keys($columns)) . '=?'; $query = $this->pdo->prepare("SELECT * FROM fossil {$whereClause} LIMIT {$start}, {$count}"); $entities = $query->execute(array_values($columns)) ? $query->fetchAll(\PDO::FETCH_ASSOC) : []; $models = $this->converter->entityArraysToModels($entities, $temp); foreach ($models as $model) { $pictures = $this->pictureRepo->getAll(['fossilId' => $model->id], 5); $model->pictures = $pictures; } return $models; }
/** * Create a new picture * * @param HttpFoundation\Request $request * @return HttpFoundation\JsonResponse|HttpFoundation\Response */ public function postIndex(HttpFoundation\Request $request) { $this->log->addDebug(print_r($request, true), ['namespace' => 'HackTheDinos\\Controllers\\Pictures', 'method' => 'postIndex', 'type' => 'request']); $path = 'uploads'; $newfilename = date('Y-m-d_His') . '.jpg'; if ($_FILES["picture"]["error"] === UPLOAD_ERR_OK) { $tmp_name = $_FILES["picture"]["tmp_name"]; move_uploaded_file($tmp_name, "uploads/{$newfilename}"); } else { return new HttpFoundation\Response($_FILES["picture"]["error"], 500); } // TODO create a model object $picture = new Models\Picture(); $picture->filepath = $newfilename; // TODO save to DB if ($this->repo->save($picture)) { $this->log->addInfo('Created new picture', ['namespace' => 'HackTheDinos\\Controllers\\Picture', 'method' => 'postIndex', 'picture' => (array) $picture]); // TODO return picture db id return new HttpFoundation\JsonResponse($picture, 201); } $this->log->addWarning('Unable to create picture', ['namespace' => 'HackTheDinos\\Controllers\\Picture', 'method' => 'postIndex', 'request' => $request->getContent(), 'picture' => (array) $picture]); return new HttpFoundation\Response('Bad Request', 400); }