Criteria query with expression contains in the field of a ManyToOne entity

1

Considering the two entities below:

/**
 * @ORM\Table(name="cliente")
 * @ORM\Entity
 */
class Cliente {
    /**
     * @ORM\Column(name="id_cliente", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\Column(type="string", length=50)
     */
    private $nome;
    // getters, setters, etc...
}

/**
 * @ORM\Table(name="pedido")
 * @ORM\Entity
 */
class Pedido {
    /**
     * @ORM\Column(name="id_pedido", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\ManyToOne(targetEntity="Cliente")
     * @ORM\JoinColumn(name="id_cliente", referencedColumnName="id_cliente")
     */
    private $cliente;
    // getters, setters, etc...
}

Is it possible to do the following search?

$criteria = Criteria::create()->where(Criteria::expr()->contains('cliente.nome', 'Felippe'));
$pedidos = $this->entityManager->getRepository('Admin\Models\Pedido')
        ->matching($criteria);

The following error is being returned:

  

Unrecognized field: client.name

I developed a utility class package to handle the searches of the jQuery DataTables plugin, and for this I created a GenericDAO.

    
asked by anonymous 27.08.2015 / 13:28

1 answer

1

As far as I understand, you need a collection of orders whose name of the customer that placed the orders is "Felippe", correct?

I would do this using a DQL, even:

<?php

namespace Admin\Repositories; // ou outro namespace à sua escolha

use Doctrine\ORM\EntityRepository;

class PedidoRepository extends EntityRepository

public function findByClienteNome($nome)
{
    return $this
        ->getEntityManager()
        ->createQuery('
            SELECT p
            FROM Admin\Models\Pedido p
            JOIN p.cliente c
            WHERE c.name = :name')
        ->setParameter('name', $nome)
        ->getResult();
}

Hence the usage would look like this:

$pedidos = $this
    ->getEntityManager()
    ->getRepository('Admin\Models\Pedido')
    ->findByClienteNome('Felippe');

By the way, you need to discriminate in your Pedido model which is the repository of Pedido objects:

<?php

namespace Admin\Models;

/**
 * @ORM\Entity(repositoryClass="Admin\Models\PedidoRepository")
 */
class Pedido
{
    // etc...
}
    
31.08.2015 / 14:40