Symfony by_reference不工作 [英] Symfony by_reference not working
问题描述
我试图用2个实体创建一个简单的OneToMany关联:
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'),
));
}
}
使用这些DegreeType:
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,
));
}
}
当我处理控制器上的表单时,新的学位被添加到数据库,但是专业人员为NULL。我已阅读Symfony的文档( https://symfony.com/doc/current/ form / form_collections.html )它可能是一个'by_reference'问题
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.
推荐答案
我建议你做的是在调用addDegree时手动调用ProfessionalDegrees setProfessional方法专业:
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;
}
您还应该在表单中使用cascade_validation,以便通过专业人员添加的学位也被验证
You should also use cascade_validation in your form so degrees you add through the Professional will also be validated
这篇关于Symfony by_reference不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!