Doctrine 说 Entity "has no field or association" used in a WHERE query,但确实如此

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

当我执行这个查询时:

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

我收到错误

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

但是字段ItemComunicacao.comunicacao确实存在:

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

字段也是如此 ClassificacaoComunicacao.abrir_tarefa:

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

那我的查询语句有什么大惊小怪的?如果其中有错误,错误信息肯定是误导。

您忘记了查询中的连接语句。

您应该添加它才能请求 classificacao.abrir_tarefa。

应该是这样的(未测试)

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

希望对您有所帮助:)