1

使用 Symfony2.3.​​4 和 Doctrine。

我有一个班级学生与班级版的多对多关系。

现在在我的 StudentController 中,我有这个 IndexAction($edition_id) 来列出数据库中的所有学生,但只列出与给定版本 ID 相关的学生。

$entities = $em->getRepository('PersonBundle:Student')->findBy(array(
    ????????
));

我正在考虑与 $edition_id 一起使用的某种标准,但无法提出任何标准。

提示赞赏,谢谢。

@dmnptr:我正在尝试,我对此很陌生,所以如果我做错了什么,请告诉我。

我把你写的那个函数放在控制器和我添加的方法 IndexAction($edition) 中

$students = this->findAllByEdition($edition);

现在的问题是参数 $edition 来自这样的树枝模板:

<a href="{{ path('student', { 'edition': entity}) }}"</a>

是持有 Edition 对象的实体。

但是当我这样做时,这个实体返回 __toString() 方法,这意味着一个带有版本名称的字符串,而不是版本本身,这是你给我的函数使用的。

现在,您是否碰巧知道从模板中获取对象本身的方法,而不是 __toString() 方法,谢谢...

4

1 回答 1

4

您应该使用自定义存储库方法。作为参数传递$edition,而不仅仅是 id。像这样的东西:

public function findAllByEdition($edition)
{
    $em = $this->getEntityManager();

    $queryText  = "SELECT s FROM PersonBundle:Student s ";
    $queryText .= "WHERE :edition MEMBER OF s.editions";

    $query = $em->createQuery($queryText);
    $query->setParameter('edition', $edition)

    return $query->getResult();
}
于 2014-03-28T21:35:42.620 回答