我有3个班级:
Class Question
{
/**
* @ORM\OneToMany(targetEntity="Answer", mappedBy="question")
*/
private $answers;
...
public function getAverage()
{
$sum = 0;
foreach ($this->answers as $answer)
$sum += $answer->getGrade();
return $sum / $this->answers->count();
}
}
class Answer
{
/**
* @ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
*/
private $question;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
private $user;
private $grade;
...
}
class User
{
...
private $username;
}
我希望能够像这样创建Question :: getAverage($ username):
public function getAverage($username)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("user.username", "$username"));
$answers = $this->answer->matching($criteria);
$sum = 0;
foreach ($answer as $answer)
$sum += $answer->getGrade();
return $sum / $answers->getGrade();
}
但是我收到以下错误:“无法识别的字段:user.username”。
有人能帮帮我吗?我一直在寻找符合标准的联合,但我没有找到! (我知道我可以通过在foreach语句中使用if条件来过滤我的答案,但这不是我想做的方式。)
感谢。
文森特。
答案 0 :(得分:0)