symfony2 doctrine findBy在arrayCollection中的id [英] symfony2 doctrine findBy id in arrayCollection
问题描述
使用Symfony2.3.4和Doctrine。
Using Symfony2.3.4 and Doctrine.
我有一个类与Student Edition有一个ManyToMany关系的类。
I have a class Student with a ManyToMany relation with a class Edition.
现在在我的StudentController中此IndexAction($ edition_id)不会列出数据库中的所有学生,但仅列出与给定版本ID相关的所有学生。
Now in my StudentController I have this IndexAction($edition_id) to list not all students in the DB but only those related to the given Edition id.
$entities = $em->getRepository('PersonBundle:Student')->findBy(array(
????????
));
我正在考虑使用$ edition_id的某种标准,但无法想出任何。
I'm thinking of some kind of criteria to use with $edition_id but can't come up with any.
提示赞赏,谢谢。
@dmnptr:
我试着,我很新,所以告诉我,如果我做错了。
@dmnptr: I'm trying that, I'm quite new to this so tell me if I did something wrong.
我把这个函数写在控制器和方法IndexAction($ edition)I添加
I put that function you wrote in the controller and in the method IndexAction($edition) I added
$students = this->findAllByEdition($edition);
现在的问题是参数$ edition来自这样一个twig模板:
now the problem is that the parameter $edition is coming from a twig template like this:
<a href="{{ path('student', { 'edition': entity}) }}"</a>
正在实体
但是当我这样做时,这个实体返回__toString()方法,这意味着一个带有版本名称的字符串,而不是版本本身就是你给我的功能。
But when I do it like this entity returns the __toString() method, meaning a string with the name of the edition, not the edition itself which is what the function you gave me uses.
现在,你碰巧知道一种从模板中获取对象本身的方法,而不是__toString()方法,谢谢...
Now, do you happen to know a way to get the object itself from the template, not the __toString() method, thanks...
推荐答案
您应该使用自定义存储库方法。通过 $ edition
作为参数,而不仅仅是一个id。这样的东西:
You should use custom repository method. Pass $edition
as a parameter, not just an id. Something like this:
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();
}
这篇关于symfony2 doctrine findBy在arrayCollection中的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!