How to use a findBy method with comparative criteria
This is an example using the Expr() Class - I needed this too some days ago and it took me some time to find out what is the exact syntax and way of usage:
/**
* fetches Products that are more expansive than the given price
*
* @param int $price
* @return array
*/
public function findProductsExpensiveThan($price)
{
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb->select(array('p'))
->from('YourProductBundle:Product', 'p')
->where(
$qb->expr()->gt('p.price', $price)
)
->orderBy('p.price', 'DESC')
->getQuery();
return $q->getResult();
}
Symfony LIKE criteria in Findby()
Can I process a LIKE search on the top of current filters in the findBy() ?
The answer is no. Doctrine findBy method does not allow you to use LIKE. You will have to use DQL to do this.
Doctrine findBy 'does not equal'
There is no built-in method that allows what you intend to do.
You have to add a method to your repository, like this:
public function getWhatYouWant()
{
$qb = $this->createQueryBuilder('u');
$qb->where('u.id != :identifier')
->setParameter('identifier', 1);
return $qb->getQuery()
->getResult();
}
Hope this helps.
setting method parameters dynamically from array (working with FindBy method in Doctrine)
If you need to send several (dynamic amount) of parameters to a method, you can use the call_user_func_array()
like this:
$arguments = [
['status'=>1],
['dateCreated'=>'DESC']
];
$queryInstance = Pluto::registry('doctrine')
->getRepository(\Application\Entity\Post::class);
$posts = call_user_func_array(
[$queryInstance, 'findBy'],
$arguments
);
Read more here: http://php.net/manual/en/function.call-user-func-array.php
How do I use a complex criteria inside a doctrine 2 entity's repository?
You'll need to write your own query (probably using DQL) if you want something that specific. I believe the built in "findBy" methods are more for just grabbing objects quickly if you have less specific criteria. I don't know your entity names or where they are stored. Could be something like this as a function in your Festival Repository.
public function findActiveFestivals($start, $end)
{
$qb = $this->_em->createQueryBuilder();
$qb->select('f')
->from('Festival', 'f')
->where('f.start >= :start')
->andWhere('f.end <= :end')
->setParameters(array('start' => $start, 'end' => $end));
return $qb->getQuery()->getArrayResult();
}
Symfony Equal to and Not Equal to Criteria on Entity
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();
Related Topics
Grabbing the Href Attribute of an a Element
Laravel, Sync() - How to Sync an Array and Also Pass Additional Pivot Fields
Php How to Determine the First and Last Iteration in a Foreach Loop
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
How to Remove Back Slashes from Json Output in PHP
How to Get Only Date from Datetime in Codeigniter
Remove License Check from Script Code in Codeigniter
Finding the Longest Gap Between Multiple Intervals
How Remove the Backslash ("\") in the Json Response Using PHP
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses
Assign Jquery Value to PHP Variable Inside Ajax Sussess
Build a Tree from a Flat Array in PHP
How to Insert Special Character in MySQL Via PHP and Display on HTML Page
How to Calculate the Difference Between Two Dates Using PHP