将MySQL查询转换为DQL
这个问题是我所问问题的后续问题(并得到了有效的答案) 如何将其转换为DQL?连接上的文档让我有点困惑将MySQL查询转换为DQL,mysql,doctrine-orm,dql,Mysql,Doctrine Orm,Dql,这个问题是我所问问题的后续问题(并得到了有效的答案) 如何将其转换为DQL?连接上的文档让我有点困惑 编辑: 我正在使用Symfony2的条令,有以下实体: 问题: /** * @ORM\Entity * @ORM\Table(name="Question", indexes={@ORM\Index(name="id_idx", columns={"id"})}) */ class Question { /** * @ORM\Id * @ORM\Column
编辑: 我正在使用Symfony2的条令,有以下实体: 问题:
/**
* @ORM\Entity
* @ORM\Table(name="Question", indexes={@ORM\Index(name="id_idx", columns={"id"})})
*/
class Question
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string question
*
* @ORM\Column(name="question", type="string", length=255)
*/
private $question;
/**
* @var array scores
*
* @ORM\OneToMany(targetEntity="Score", mappedBy="question")
*/
private $scores;
// getters and setters
}
分数:
/**
* @ORM\Entity
* @ORM\Table(name="Score", indexes={@ORM\Index(name="id_idx", columns={"id"})})
*/
class Score
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var integer $question
*
* @ORM\ManyToOne(targetEntity="Question", inversedBy="scores")
* @ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
private $question;
/**
* @var float score
*
* @ORM\Column(name="score", type="float")
*/
private $score;
// getters and setters
}
我使用了以下查询:
$query = $em->createQuery('SELECT q AS question, AVG(s.score) AS average FROM CMSBundle:Question q JOIN q.scores s GROUP BY q.id ORDER BY q.id ASC');
$questions = $query->getResult();
但是,对于该查询,$questions包含0个元素。我也没有收到任何错误(至少PhpStorm在其调试器中找不到任何错误)
对于为什么我的查询没有得到任何反馈,我有点不知所措。任何帮助都将不胜感激。我记得就在上周遇到了这个问题。我花了很长时间研究如何做到这一点,并设法提出了以下DQL。向问题的存储库类添加新方法 我不得不调整我自己的代码来匹配这个问题,所以不能保证它能工作,但请尝试一下
<?php
namespace Acme\CMSBundle\Entity;
use Doctrine\ORM\EntityRepository;
class QuestionRepository extends EntityRepository
{
/**
* @return array
*/
public function findAverageScoresPerQuestion()
{
$dql = <<<SQL
SELECT
q question,
AVG(s.score) average
FROM
Acme\CMSBundle\Entity\Question q,
Acme\CMSBundle\Entity\Score s
WHERE
s.question = q.id
GROUP BY q.id
ORDER BY q.id ASC
SQL;
$q = $this->_em->createQuery($dql);
return $q->getResult();
}
}
在questions.html.twig中
<table>
<thead>
<tr>
<th>Question
<th>Average Score
</tr>
</thead>
<tbody>
{% for q in questions %}
<tr>
<td>{{ q.question.question }}
<td>{{ q.average }}
</tr>
{% else %}
<tr>
<td colspan="2">No results found
</tr>
{% endfor %}
</tbody>
</table>
问题:
平均分
{问题中q的百分比%}
{{q.question.question}
{{q.average}}
{%else%}
未找到任何结果
{%endfor%}
您最好编辑此内容,并将实际SQL输入到该问题中。您还需要知道您的条令类和字段的名称,以及您希望数据如何输出。由于大多数DQL仅以“纯”方式水合,对象是唯一的,但您想要混合水合,对象和平均值,还是只需要id和平均值供以后使用?您会发现它类似于从问题q中选择q,平均(s.score)通过q.id加入q.score组,这将给你一个。编辑了更多的信息。CMSBundle:Question
通常给类加斜杠。一般来说看起来不错,我自己也没用过别名。
<table>
<thead>
<tr>
<th>Question
<th>Average Score
</tr>
</thead>
<tbody>
{% for q in questions %}
<tr>
<td>{{ q.question.question }}
<td>{{ q.average }}
</tr>
{% else %}
<tr>
<td colspan="2">No results found
</tr>
{% endfor %}
</tbody>
</table>