我想在我的 QueryBuilder 中使用一些 MySQL 函数,例如 DATE_FORMAT。
我看到这篇文章完全不明白我应该如何实现它: SELECT DISTINCT YEAR Doctrine
class SubmissionManuscriptRepository extends EntityRepository
{
public function findLayoutDoneSubmissions( $fromDate, $endDate, $journals )
{
if( true === is_null($fromDate) )
return null;
$commQB = $this->createQueryBuilder( 'c' )
->join('c.submission_logs', 'k')
->select("DATE_FORMAT(k.log_date,'%Y-%m-%d')")
->addSelect('c.journal_id')
->addSelect('COUNT(c.journal_id) AS numArticles');
$commQB->where("k.hash_key = c.hash_key");
$commQB->andWhere("k.log_date >= '$fromDate'");
$commQB->andWhere("k.log_date <= '$endDate'");
if( $journals != null && is_array($journals) && count($journals)>0 )
$commQB->andWhere("c.journal_id in (" . implode(",", $journals) . ")");
$commQB->andWhere("k.new_status = '20'");
$commQB->orderBy("k.log_date", "ASC");
$commQB->groupBy("c.hash_key");
$commQB->addGroupBy("c.journal_id");
$commQB->addGroupBy("DATE_FORMAT(k.log_date,'%Y-%m-%d')");
return $commQB->getQuery()->getResult();
}
}
实体提交手稿
/**
* MDPI\SusyBundle\Entity\SubmissionManuscript
*
* @ORM\Entity(repositoryClass="MDPI\SusyBundle\Repository\SubmissionManuscriptRepository")
* @ORM\Table(name="submission_manuscript")
* @ORM\HasLifecycleCallbacks()
*/
class SubmissionManuscript
{
...
/**
* @ORM\OneToMany(targetEntity="SubmissionManuscriptLog", mappedBy="submission_manuscript")
*/
protected $submission_logs;
...
}
实体提交稿件日志
/**
* MDPI\SusyBundle\Entity\SubmissionManuscriptLog
*
* @ORM\Entity(repositoryClass="MDPI\SusyBundle\Repository\SubmissionManuscriptLogRepository")
* @ORM\Table(name="submission_manuscript_log")
* @ORM\HasLifecycleCallbacks()
*/
class SubmissionManuscriptLog
{
...
/**
* @ORM\ManyToOne(targetEntity="SubmissionManuscript", inversedBy="submission_logs")
* @ORM\JoinColumn(name="hash_key", referencedColumnName="hash_key")
*/
protected $submission_manuscript;
...
}
任何帮助我都会非常感激。
编辑 1
我现在可以成功添加自定义函数 DATE_FORMAT。但是现在如果我尝试使用我的 Group By,我会收到以下错误:
[Semantical Error] line 0, col 614 near '(k.logdate,'%Y-%m-%d')': Error: Cannot group by undefined identification variable.
有人知道吗?