也许有人可以帮我变身
我正在尝试通过page_id获取与theFaqPageQuestionContent不相关的QuestionContent,并在选择框中查看它
SELECT q FROM VswSystemCmsBundle:QuestionContent q WHERE q.id NOT IN
(SELECT fq.questioncontent_id FROM VswSystemCmsBundle:FaqPageQuestionContent fq WHERE fq.faqcontentpage_id = :page_id)
转换为QueryBuilder表单,以在Sf2表单中使用它。
既然您已经弄清了要做什么,我建议您在连接到QuestionContent实体的表单上使用“实体”字段类型。
// don't forget the use statement for your repository up top
use Your\VswSystemCmsBundle\Repository\QuestionContentRepository;
// in buildForm() (this assumes you have $pageId set properly)
$builder
->add('questionContent', 'entity', array(
'class' => 'VswSystemCmsBundle:QuestionContent',
'property' => 'questionForForm',
'query_builder' => function(QuestionContentRepository $repo) use ($pageId) {
return $repo->findNotAttachedQuestions($pageId);
},
))
;
编辑:将您的QueryBuilder放在该实体的存储库中,然后从那里调用它。
// this is Your\VswSystemCmsBundle\Repository\QuestionContentRepository class
public function findNotAttachedQuestions($pageId)
{
$subQuery = $this->createQuery("
SELECT fq.questioncontent_id
FROM VswSystemCmsBundle:FaqPageQuestionContent fq
WHERE fq.faqcontentpage_id = :page_id
")
->setParameter('page_id', $pageId)
;
return $this->createQueryBuilder('q')
->where($qb->expr()->notIn('q.id', $subQuery))
;
}
请注意,我如何将上面的“属性”定义为questionForForm
?我们需要向您的QuestionContent实体添加一个getter函数,以返回问题的前30个字符。
// this is Your\VswSystemCmsBundle\Entity\QuestionContent class
public function getQuestionForForm()
{
return substr($this->getQuestion(), 0, 30);
}
现在,一切都在正确的位置分离了。您不必担心将DQL转换为Doctrine QueryBuilder实例,因为您将其保存在存储库中并在那里进行调用。您不必创建自定义数组即可为select返回数据,现在您有了一个可重用的函数,可以直接从Entity返回问题的前30个字符。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句