Reputation: 12939
I have number of blog posts in my database and im using doctrine criteria to filter them. If I write criteria that matches some of the posts everything works perfect.
Imagine that there are 3 users (User 1 , User 2, User 3) and there are some posts marked as private from User 1 and user 2.
This is my php code :
$criteria = Criteria::create();
$criteria->Where($criteria->expr()->eq('private', 1);
$criteria->andWhere($criteria->expr()->eq('author', $author));
$posts=$service->findPostBy($criteria);
return array(
'page' => $page,
'posts'=>$posts,
'filter' => $form->createView()
);
/*findPostBy calls Doctrine matching function*/
public function matching(Criteria $criteria)
{
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
return new ArrayCollection($persister->loadCriteria($criteria));
}
If i ask for User 1 private posts it builds functional query and return posts i want. But if I ask for User 3 private posts It returns all posts in my database without any filtering. How can I tell if my criteria works if they dont return empty result in case of no matches.
Upvotes: 0
Views: 1293
Reputation: 8276
Try this:
$criteria = Criteria::create()
->where(Criteria::expr()->eq('private', 1);
->andWhere(Criteria::expr()->eq('author', $author));
I'm not sure why you are using criteria in the first place for your example though. You could eliminate the Criteria altogether and accomplish what you want in one line:
$posts $service->findPostBy(array('private' => 1, 'author' => $author);
Upvotes: 1