TypiCMS icon indicating copy to clipboard operation
TypiCMS copied to clipboard

AbstractRepository->getAllBy more than one 'where'

Open elvendor opened this issue 11 years ago • 1 comments

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?

elvendor avatar Jun 22 '14 13:06 elvendor

Fine, but don't forget to update interface and cache decorator + docblocks.

sdebacker avatar Jun 22 '14 15:06 sdebacker