我试图用2个实体创建一个简单的OneToMany关联:
<?php
session_start();
$servername = "xxxxx";
$connectioninfo = array(
'Database' => 'xxxxxxxxxxxxx'
);
$conn = sqlsrv_connect($servername, $connectioninfo);
if (!$conn) {
echo 'connection failure';
die(print_r(sqlsrv_errors() , TRUE));
}
$q1 = "select top 10 *
from pointsBadgeTable
WHERE WeekNumber ='week51'
order by pointsRewarded desc";
$stmt = sqlsrv_query($conn, $q1);
if ($stmt == false) {
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors() , TRUE));
}
do {
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$result[] = $row;
}
}
while (sqlsrv_next_result($stmt));
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first
//Set content type to json
header('Content-Type: application/json');
//Echo a json object to the browser
echo json_encode($result);
?>
和
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;
}
当我尝试使用Type:
创建表单时/**
* @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;
}
使用这些DegreeType:
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'),
));
}
}
当我在控制器上处理表单时,新程度将添加到数据库中,但专业人员处于NULL状态。我在Symfony的文档(https://symfony.com/doc/current/form/form_collections.html)中读到它可能是一个'by_reference'问题
有什么想法吗? 谢谢。
答案 0 :(得分: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添加的度数也将被验证