我有实体“创意”和实体“问题”...当我有从Creative到Question的ManyToMany关系时,我可以轻松地执行$ builder-> add('questions'),它会抓住所有问题并将它们放在多选中并插入到creative_question中。好吧,我需要为creative_question设置一个新的字段(位置),所以我必须创建一个OneToMany / ManyToOne关系。但是当我添加字段($ builder-> add('creativeQuestions'))时,多选是空白的,它似乎试图查询creative_question来填充它..这是错误的。我需要填充问题并将其插入到creative_question中。
无论如何,这是我的代码:
## Creative.php
[...]
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Offer", cascade={"persist"})
*/
protected $offer;
/**
* @ORM\OneToMany(targetEntity="CreativeQuestion", mappedBy="creative", cascade={"persist"})
*/
protected $creativeQuestions;
[...]
## CreativeQuestion.php
[...]
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Creative", cascade={"persist"})
*/
protected $creative;
/**
* @ORM\ManyToOne(targetEntity="Question", cascade={"persist"})
*/
protected $question;
/**
* @ORM\Column(type="integer")
*/
protected $pos;
[...]
## CreativeType.php
[...]
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('name')
->add('title')
->add('description')
->add('body')
->add('script')
->add('creativeQuestions') // how do i populate list with "Questions" then insert into creative_question?
->add('active');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'JStout\MainBundle\Entity\Creative'
);
}
[...]
## In My Controller:
/**
* @Extra\Route("/offer/{offerId}/creative", name="admin_offer_creative")
* @Extra\Route("/offer/{offerId}/creative/{creativeId}", name="admin_offer_creative_edit")
* @Extra\Template()
*/
public function creativeAction($offerId = null, $creativeId = null)
{
// Get Offer
$offer = $this->_getObject('Offer', $offerId);
if (null === $offer->getId()) throw new NotFoundHttpException('The page you requested does not exist!');
// Get Creative
$creative = $this->_getObject('Creative', $creativeId);
// Set offer to creative
$creative->setOffer($offer);
// Get form and handler
$form = $this->get('form.factory')->create(new Form\CreativeType(), $creative);
$formHandler = $this->get('form.handler')->create(new Form\CreativeHandler(), $form);
[...]
}
protected function _getObject($entityName, $id = null)
{
// Find object
if (null !== $id) {
if (!$object = $this->get('doctrine')->getEntityManager()->find('ZGOffersMainBundle:' . $entityName, $id)) {
throw new NotFoundHttpException('The page you requested does not exist!');
}
return $object;
}
// Initialize new object
$entityName = 'JStout\MainBundle\Entity\\' . $entityName;
if (class_exists($entityName)) {
return new $entityName();
}
throw new NotFoundHttpException('The page you requested does not exist!');
}
[...]
同样,当我删除CreativeQuestion并且只使用ManyToMany关系进行提问时,我需要的是什么:
但是,理想情况下,我希望能够(通过jquery)通过从下拉框中选择添加问题,然后拖放以定位问题。 jquery的定位很简单,我只是不知道如何添加我想要的问题。如果我至少可以让多重选择工作,那么我可以前进,但我现在有点卡住了!
任何人都可以通过Symfony2(beta5)获得这个目标吗?