67

I have entity for Doctrine:

<?php
/**
 * @Entity
 * @Table(name="orders")
 */
class Orders {
    /** @Id @Column(name="OID",type="integer") @GeneratedValue */
    private $id;
    /** @Column(name="Product",type="string")*/
        private $product;
    /** @Column(name="RegCode",type="string")*/
        private $reg_code;
    /** @Column(name="OrderEmail",type="string")*/
    private $email;
}

I need make query like this:

select * from `orders` where `OrderEmail`='[email protected]' and `Product` LIKE 'My Products%'

I try handle query without like:

$em->getRepository("Orders")->findByEmailAndProduct($uname,$product);

But it make error. Why? Can I do this query without DQL? I want make this query use magic methods findBy**

1
  • 1
    What error? But there is no way to do this using findBy* because Doctrine dont know what field you want to compare using LIKE`
    – azat
    Nov 17, 2011 at 9:21

7 Answers 7

170

This is not possible with the magic find methods. Try using the query builder:

$result = $em->getRepository("Orders")->createQueryBuilder('o')
   ->where('o.OrderEmail = :email')
   ->andWhere('o.Product LIKE :product')
   ->setParameter('email', '[email protected]')
   ->setParameter('product', 'My Products%')
   ->getQuery()
   ->getResult();
3
  • 1
    Thanks for example. Can I make group by use QueryBuilder?
    – Eugene
    Nov 17, 2011 at 9:51
  • 1
    I use doctrine with Zend Framework. Thaks for link on Symfony site. Maybe I find more information on it.
    – Eugene
    Nov 17, 2011 at 10:03
  • 1
    Yes you can use groupBy and a lot of other helper methods Nov 17, 2011 at 10:41
30

You can use the createQuery method (direct in the controller) :

$query = $em->createQuery("SELECT o FROM AcmeCodeBundle:Orders o WHERE o.OrderMail =  :ordermail and o.Product like :searchterm")
->setParameter('searchterm', '%'.$searchterm.'%')
->setParameter('ordermail', '[email protected]');

You need to change AcmeCodeBundle to match your bundle name

Or even better - create a repository class for the entity and create a method in there - this will make it reusable

2
  • 1
    I know how make this query on DQL. Maybe you know how make it without DQL and why "findByEmailAndProduct" doesn't work.
    – Eugene
    Nov 17, 2011 at 9:29
  • @Evgeniy updated - you would need to test it as im not sure that the findby method uses like
    – Manse
    Nov 17, 2011 at 9:35
4

This is not possible with the magic methods, however you can achieve this using DQL (Doctrine Query Language). In your example, assuming you have entity named Orders with Product property, just go ahead and do the following:

$dql_query = $em->createQuery("
    SELECT o FROM AcmeCodeBundle:Orders o
    WHERE 
      o.OrderEmail = '[email protected]' AND
      o.Product LIKE 'My Products%'
");
$orders = $dql_query->getResult();

Should do exactly what you need.

3
  • 2
    This code snippet creates the ability of SQL injection, if you like to build the query yourself, rather use the answer by @ManseUK. But that's not what the OP wanted anyway..
    – Dennis98
    Jan 19, 2018 at 15:41
  • 1
    @Dennis98 does every ->createQuery( ) create the ability of a SQL injection? Or is there something else wrong here?
    – Omar Trkzi
    Jun 22, 2022 at 11:09
  • 1
    @OmarTrkzi, there should have been placeholders instead of actual parameter values, which you then fill up using AbstractQuery::setParameter().
    – iloo
    Apr 13 at 11:26
3

You can also consider using DBAL:

$qb = $em->getRepository("Orders")->createQueryBuilder('o');
$result = $qb->where('o.OrderEmail = :email')
   ->andWhere($qb->expr()->like('o.Product', ':product'))
   ->setParameter('email', '[email protected]')
   ->setParameter('product', 'My Products%')
   ->getQuery()
   ->getResult();
1

The right answer to the question how to use LIKE in Doctrine would be using the Expression "Contains":

$expressionBuilder = Criteria::expr();

$expression = $expressionBuilder->contains('fieldName', 'value');

$collection->matching(new Criteria($expression));
1
  • Note: there is also $expressionBuilder->startsWith() and $expressionBuilder->endsWith(), to cover 'foo%' and '%foo' cases.
    – iloo
    Sep 6 at 16:01
0

Actually you just need to tell doctrine who's your repository class, if you don't, doctrine uses default repo instead of yours.

@ORM\Entity(repositoryClass="Company\NameOfBundle\Repository\NameOfRepository")
-4

you can also do it like that :

$ver = $em->getRepository('GedDocumentBundle:version')->search($val);

$tail = sizeof($ver);
1
  • 1
    Please, don't use sizeof, it's a bad practice. And EntityRepository->search() is not existing, use ->find() at least. Mar 22, 2022 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.