Doctrine says Entity "has no field or association" used in a WHERE query, but it surely does

When I execute this query:

$array_ids_items = $this->em->createQuery(
    'SELECT i.id, i.id_alt
     FROM App\Entity\ItemComunicacao i
     WHERE i.id_tarefa IS NULL AND i.classificacao.abrir_tarefa = TRUE'
)->getArrayResult();

I get the error

"Class App\Entity\ItemComunicacao has no field or association named classificacao.abrir_tarefa"

But field ItemComunicacao.comunicacao surely exists:

/**
 * @ORM\ManyToOne(targetEntity=ClassificacaoComunicacao::class)
 */
private ?ClassificacaoComunicacao $classificacao;

and so does field ClassificacaoComunicacao.abrir_tarefa:

/**
 * @ORM\Column(type="boolean", options={"default":false})
 */
private bool $abrir_tarefa = false;

So what’s the fuss about my query statement? If there is an error in it, error message is surely misguiding.

>Solution :

you forgot the join statement in your query.

You should add it to be able to request classificacao.abrir_tarefa.

It should be something like that (not tested)

$array_ids_items = $this->em->createQuery(
    'SELECT i.id, i.id_alt
     FROM App\Entity\ItemComunicacao i
     LEFT JOIN i.classificacao ic
     WHERE i.id_tarefa IS NULL AND ic.abrir_tarefa = TRUE'
)->getArrayResult();

hope it helped 🙂

Leave a Reply