【问题标题】:Symfony by_reference not workingSymfony by_reference 不起作用
【发布时间】:2017-01-18 09:46:42
【问题描述】:

我正在尝试使用 2 个实体创建一个简单的 OneToMany 关联:

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;
    }

/**
 * @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;
    }

当我尝试使用类型创建表单时:

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'),
        ));
}
}

使用这些 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,
        ));
    }
}

当我在控制器上处理表格时,新学位被添加到数据库中,但专业为 NULL。我在 Symfony 的文档 (https://symfony.com/doc/current/form/form_collections.html) 中读到这可能是一个“by_reference”问题

有什么想法吗? 谢谢。

【问题讨论】:

  • 通过将by_reference 设置为false,您已经强制调用setter,所以这似乎不是问题。您的表单类型似乎也没有任何问题。也许您应该尝试将级联持久添加到多对一注释

标签: php symfony doctrine


【解决方案1】:

我建议你在Professional上调用addDegree时手动调用ProfessionalDegrees的setProfessional方法:

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

    return $this;
}

您还应该在表单中使用 cascade_validation,这样您通过 Professional 添加的学位也将得到验证

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-15
    • 2013-10-09
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    相关资源
    最近更新 更多