Symfony2 (beta5) 1:M/M:1 的形式

发布于 2024-11-16 21:18:18 字数 3526 浏览 0 评论 0原文

我有实体“Creative”和实体“Question”...当我有从 Creative 到 Question 的多对多关系时,我可以轻松执行 $builder->add('questions'),它会抓取所有问题并放入将它们放入多选并插入到creative_question中。好吧,我需要有一个新的字段(位置)来创意问题,所以我必须创建一个 OneToMany / ManyToOne 关系。但是当我添加字段 ($builder->add('creativeQuestions')) 时,多选是空白的,它似乎试图查询 marketing_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 关系执行问题时,我需要的工作:

working

但是,理想情况下,我希望能够(使用 jquery)通过从下拉框中选择来添加问题,然后拖放以定位问题。 jquery 定位很简单,我只是不知道如何添加我想要的问题。如果我至少能让多选工作,那么我就可以继续前进,但我现在有点卡住了!

有人用 Symfony2 (beta5) 做到这一步了吗?

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:

working

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文