I have entity "Creative" and Entity "Question"... When I had a ManyToMany relation from Creative to Question, I could easily do $builder->add('questions'), and it would grab all the questions and put them in a multiselect and insert into creative_question. Well I need to have a new field (position) to creative_question, so I had to create a OneToMany / ManyToOne relation. But when I add the field ($builder->add('creativeQuestions')), the multi-select is blank and it seems to be trying to query creative_question to populate it.. which is wrong. I need 开发者_开发知识库to populate with Questions and insert those into creative_question.
Anyhow, here's my code:
## 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!');
}
[...]
Again, what I need works when I remove CreativeQuestion and just do Question with a ManyToMany relation:
But, ideally I'd like to have the ability to (with jquery) add questions by selecting from a dropdown box, then drag/drop for positioning of the questions. The jquery positioning is easy, I just don't know how to go about adding the questions how I want to. If I can at least get the multiselect to work, then I can move forward, but I'm kind of stuck right now!
Anyone get this far yet with Symfony2 (beta5)?
精彩评论