Php Symfony by_参考不起作用

Php Symfony by_参考不起作用,php,symfony,doctrine,Php,Symfony,Doctrine,我正在尝试创建一个简单的单域关联,其中包含两个实体: class Professional extends User { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { $this->f

我正在尝试创建一个简单的单域关联,其中包含两个实体:

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

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

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的文件中读到,这可能是一个“按参考”的问题

有什么想法吗?
谢谢。

我建议您在调用Professional上的addDegree时手动调用ProfessionalDegrees设置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;
}
您还应该在表单中使用cascade\u验证,这样您通过专业人员添加的学位也将通过将By\u reference设置为false来验证

,因为您已经在强制调用setter,所以这似乎不是问题。您的表单类型似乎也没有问题。也许您应该尝试将级联持久化添加到多对一注释中
/**
 * Add degrees
 *
 * @param \AppBundle\Entity\ProfessionalDegrees $degrees
 * @return Professional
 */
public function addDegree(\AppBundle\Entity\ProfessionalDegree $degrees)
{
    $degrees->setProfessional($this);
    $this->degrees[] = $degrees;

    return $this;
}