Eugene
Eugene

Reputation: 1730

Doctrine and LIKE query

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**

Upvotes: 69

Views: 165088

Answers (7)

Michael Saban
Michael Saban

Reputation: 39

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

Upvotes: 2

MAZux
MAZux

Reputation: 971

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();

Upvotes: 3

Marco
Marco

Reputation: 2892

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

Upvotes: 0

elcukro
elcukro

Reputation: 41

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.

Upvotes: 4

Patrik Ken
Patrik Ken

Reputation: 71

you can also do it like that :

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

$tail = sizeof($ver);

Upvotes: -4

Manse
Manse

Reputation: 38147

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

Upvotes: 32

Kristian Zondervan
Kristian Zondervan

Reputation: 2954

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();

Upvotes: 173

Related Questions