TypiCMS
TypiCMS copied to clipboard
AbstractRepository->getAllBy more than one 'where'
I needed more than one condition in getAllBy method and I made changes like this:
public function getAllBy(array $conditions = array(), array $with = array('translations'), $all = false)
{
$query = $this->make($with);
if (! $all) {
// Take only online and translated items
$query = $query->whereHasOnlineTranslation();
}
if(is_array($conditions) && count($conditions))
{
foreach($conditions as $key => $value)
{
$query->where($key, $value);
}
}
$models = $query->get();
return $models;
}
And then in any Controller:
$conditions = array(
'category_id' => $category->id,
'type' => $type
);
$data = $this->repository->getAllBy($conditions, $relatedModels, false);
What do you think about this?
Fine, but don't forget to update interface and cache decorator + docblocks.