duanma8207 2017-01-18 09:46
浏览 30
已采纳

Symfony by_reference不起作用

Im trying to create a simple OneToMany association with 2 entities:

class Professional extends User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        $this->followers = new \Doctrine\Common\Collections\ArrayCollection();
        $this->following = new \Doctrine\Common\Collections\ArrayCollection();
        $this->degrees = new \Doctrine\Common\Collections\ArrayCollection();
        $this->experiences = new \Doctrine\Common\Collections\ArrayCollection();
    }
...
/**
     * @ORM\OneToMany(targetEntity="ProfessionalDegree", mappedBy="professional", cascade={"persist"})
     */
    private $degrees;
...
**
     * Add degrees
     *
     * @param \AppBundle\Entity\ProfessionalDegrees $degrees
     * @return Professional
     */
    public function addDegree(\AppBundle\Entity\ProfessionalDegree $degrees)
    {
        $this->degrees[] = $degrees;

        return $this;
    }

    /**
     * Remove degrees
     *
     * @param \AppBundle\Entity\ProfessionalDegrees $degrees
     */
    public function removeDegree(\AppBundle\Entity\ProfessionalDegree $degrees)
    {
        $this->degrees->removeElement($degrees);
    }

    /**
     * Get degrees
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getDegrees()
    {
        return $this->degrees;
    }

And

/**
 * @ORM\Entity
 * @ORM\Table(name="professional_degree")
 */
class ProfessionalDegree
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    public function __construct()
    {
    }
...

/**
     * @ORM\ManyToOne(targetEntity="Professional", inversedBy="degrees")
     * @ORM\JoinColumn(name="professional_id", referencedColumnName="id")
     */
    private $professional;

...

/**
     * Set professional
     *
     * @param \AppBundle\Entity\Professional $professional
     * @return ProfessionalDegree
     */
    public function setProfessional(\AppBundle\Entity\Professional $professional = null)
    {
        $this->professional = $professional;

        return $this;
    }

    /**
     * Get professional
     *
     * @return \AppBundle\Entity\Professional 
     */
    public function getProfessional()
    {
        return $this->professional;
    }

When im trying to create the form using the Type:

class ProfessionalType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    { 
     $builder
        ->add($builder->create('degrees', CollectionType::class, array(
            'entry_type'   => DegreeType::class,
            'allow_add'    => true,
            'allow_delete' => true,
            'by_reference' => false,
        )));
    }

    public function getParent()
{
    return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}

public function getBlockPrefix()
{
    return 'professional_registration_form';
}

public function getName()
{
    return $this->getBlockPrefix();
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => array('ProfessionalRegistration'),
        ));
}
}

With these DegreeType:

class DegreeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {  
        $builder
        ->add('title')
        ->add('place')
        ->add('date_in', DateType::class, array(
            'widget' => 'single_text'
            ))
        ->add('date_out', DateType::class, array(
            'widget' => 'single_text'
            ));
    }

    public function getBlockPrefix()
    {
        return 'degree_registration_form';
    }

    public function getName()
    {
        return $this->getBlockPrefix();
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => ProfessionalDegree::class,
        ));
    }
}

When i handle the form on the controller, the new degree is added to the database but with the professional at NULL. I've read in Symfony's doc (https://symfony.com/doc/current/form/form_collections.html) that it might be a 'by_reference' problem

Any idea? Thank you.

  • 写回答

1条回答 默认 最新

  • douchuanchai2793 2017-01-18 14:11
    关注

    What I suggest you to do is to manually call the ProfessionalDegrees setProfessional method when calling addDegree on Professional:

    /**
     * Add degrees
     *
     * @param \AppBundle\Entity\ProfessionalDegrees $degrees
     * @return Professional
     */
    public function addDegree(\AppBundle\Entity\ProfessionalDegree $degrees)
    {
        $degrees->setProfessional($this);
        $this->degrees[] = $degrees;
    
        return $this;
    }
    

    You should also use cascade_validation in your form so degrees you add through the Professional will also be validated

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度