dongxi3209 2015-05-31 04:13
浏览 35
已采纳

以Symfony2形式分​​配和更新实体属性

Is there anyway I can assign an entity and edit one of it's properties in a single Symfony2 form?

Example:

Tasks have many Activities.

When I create an Activity I assign a Task through an Entity form field. I also want to update the Task remainingEstimateInSeconds when the entity is created.

Activity Entity:

<?php

namespace AppBundle\Entity;

/**
 * Activity
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\ActivityRepository")
 */
class Activity
{

    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Assert\NotNull()
     * @ORM\ManyToOne(targetEntity="Task")
     */
    private $task;
}

Task Entity:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Task
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\TaskRepository")
 */
class Task
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Assert\NotBlank()
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(name="remainingEstimateInSeconds", type="integer", length=255, nullable=true)
     */
    private $remainingEstimateInSeconds;
}

Activity Form:

My current implementation is using a non-mapped field. The remaining estimate is set back onto the task in the controller which isn't the best implementation.

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotNull;

class ActivityType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description');
        $builder->add('task');
        $builder->add('remainingEstimateInSeconds', 'integer', array(
            'mapped' => false,
            'constraints' => array(
                new NotNull()
            )
        ));
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'AppBundle\Entity\Activity',
            )
        );
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'activity';
    }
}

I also tried using property_path => "task.remainingEstimateInSeconds" but the duplicate property_path was throwing some kind of error.

  • 写回答

1条回答 默认 最新

  • duanao6704 2015-05-31 12:04
    关注

    What about using a non-mapped field with POST_SET_DATA and POST_SUBMIT listeners on the main form to prepopulate the field/update the object?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加