Snopzer
Snopzer

Reputation: 1692

Symfony Equal to and Not Equal to Criteria on Entity

i want to perform a Find operation on Entity Results.

$stores = c2gr($this, 'EducateToolsBundle:Stores')->findBy(['selectOption' => true, 'center_type_id' => 9])

i want to do center_type_id != 9

Upvotes: 1

Views: 7285

Answers (2)

Timurib
Timurib

Reputation: 2743

Just use a DQL or QueryBuilder.

$repository
    ->createQueryBuilder('s')
    ->where('s.selectOption = :selectOption')
    ->andWhere('s.center_type_id <> :center_type_id')
    ->setParameters([
        'selectOption'   => true
        'center_type_id' => 9,
    ])
    ->getQuery()
    ->getResult();

Upvotes: 3

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10890

You should be able to achieve that by using Criteria class. Something like this should work:

use Doctrine\Common\Collections\Criteria;

$criteria = Criteria::create();

$criteria->where($criteria->expr()->neq('center_type_id', 9));
$criteria->andWhere($criteria->expr()->eq('selectOption', true));

$entityRepository = $this->getDoctrine()->getRepository('C2EducateToolsBundle:Stores');

$result = $entityRepository->matching($criteria);

NOTE: above will work if you are in class which extends Symfony\Bundle\FrameworkBundle\Controller\Controller since it is using getDoctrine method. If you are not in such class you should either inject EntityManager or get it from service container.

For more details you should check this answer

Upvotes: 3

Related Questions