[php] 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**

This question is related to php mysql doctrine-orm

The answer is


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

you can also do it like that :

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

$tail = sizeof($ver);

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


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


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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to mysql

Implement specialization in ER diagram How to post query parameters with Axios? PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' phpMyAdmin - Error > Incorrect format parameter? Authentication plugin 'caching_sha2_password' is not supported How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Connection Java-MySql : Public Key Retrieval is not allowed How to grant all privileges to root user in MySQL 8.0 MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client

Examples related to doctrine-orm

How to sort findAll Doctrine's method? Doctrine query builder using inner join with conditions How to do left join in Doctrine? How to use a findBy method with comparative criteria The EntityManager is closed Doctrine findBy 'does not equal' How to order results with findBy() in Doctrine Select entries between dates in doctrine 2 Get single row result with Doctrine NativeQuery Count Rows in Doctrine QueryBuilder