Doctrine and LIKE query

后端 未结 5 1804
滥情空心
滥情空心 2021-02-06 20:44

I have entity for Doctrine:



        
相关标签:
5条回答
  • 2021-02-06 20:53

    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', 'some@email.com');
    

    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

    0 讨论(0)
  • 2021-02-06 21:00

    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', 'some@mail.com')
       ->setParameter('product', 'My Products%')
       ->getQuery()
       ->getResult();
    
    0 讨论(0)
  • 2021-02-06 21:06

    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")

    0 讨论(0)
  • 2021-02-06 21:08

    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 = 'some@mail.com' AND
          o.Product LIKE 'My Products%'
    ");
    $orders = $dql_query->getResult();
    

    Should do exactly what you need.

    0 讨论(0)
  • 2021-02-06 21:10

    you can also do it like that :

    $ver = $em->getRepository('GedDocumentBundle:version')->search($val);
    
    $tail = sizeof($ver);
    
    0 讨论(0)
提交回复
热议问题