我想在我的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;
...
}
Entity SubmissionManuscriptLog
/**
* 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.
有人知道这个吗?