【问题标题】:Query DQL using oneToMany in WHERE在 WHERE 中使用 oneToMany 查询 DQL
【发布时间】:2019-06-30 01:26:55
【问题描述】:

这个查询不起作用:

public function getByUnidadeAnoSemestre(int $unidadeId, int $ano, int $semestre) : array
{
    $query = $this->em->createQuery(
        'SELECT s,u,p FROM ' . 
            Servico::class . ' s ' .
        'INNER JOIN ' . 
            's.unidade u ' . 
        'INNER JOIN ' . 
            's.precificacoes p ' .
        'WHERE ' . 
            'u.id = :unidadeId ' . 
            'AND p.anoLetivo = :ano ' . 
            'AND p.semestre = :semestre'
    )->setParameters([
        "unidadeId" => $unidadeId,
        "anoLetivo" => $ano,
        "semestre" => $semestre
    ]);

    return $query->getResult();

}

Service类中的映射如下:

/**
* @OneToMany(targetEntity="Precificacao", mappedBy="precificacoes", cascade={"persist"})
* @var Collection 
*/
private $precificacoes;

/**
* @ManyToOne(targetEntity="Unidade", inversedBy="id", cascade={"persist"})
* @JoinColumn(name="unidadeId", referencedColumnName="id")
* @var Unidade 
*/
private $unidade;

Slim 错误信息是:

“关联类型必须是 *_TO_ONE 或 MANY_TO_MANY 之一”

而php控制台中的错误是:

PHP 通知:未定义索引:D:\Projetos\FinanceiroEscolarApi\vendor\doctrine\orm\lib\Doctrine\ORM\Query\SqlWalker.php 中的 precificacoes 在第 946 行

你能帮帮我吗?

【问题讨论】:

    标签: php doctrine dql


    【解决方案1】:

    我能够找出问题所在。是Service和Precification之间的一对多双向映射。

    Servico 类映射

    /**
    * @OneToMany(targetEntity="Precificacao", mappedBy="servico", cascade={"persist"})
    * @var Collection 
    */
    private $precificacoes;
    

    Precificacao 类映射

    /**
     * @ManyToOne(targetEntity="Servico", inversedBy="precificacoes", cascade={"persist"})
     * @JoinColumn(name="servicoId", referencedColumnName="id")
     * @var Servico
     */
    private $precificacoes;
    

    然后我更正了查询

    public function getByUnidadeAnoSemestre(int $unidadeId, int $ano, int $semestre) : array
    {
        $query = $this->em->createQuery(
            'SELECT s,u,p FROM ' . 
                Servico::class . ' s ' .
            'INNER JOIN ' . 
                's.unidade u ' . 
            'INNER JOIN ' . 
                's.precificacoes p ' .
            'WHERE ' . 
                'u.id = :unidadeId ' . 
            'AND p.anoLetivo = :ano ' . 
                'AND p.semestre = :semestre'
            )->setParameters([
                "unidadeId" => $unidadeId,
                "ano" => $ano,
                "semestre" => $semestre
            ]);
    
        return $query->getResult();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多