3
我需要一些幫助來了解我做錯了什麼,我試着在這裏搜索一個答案,但沒有任何工作。Symfony2一對一的關係形式
在這裏我的問題..
我有兩個實體有一個一對一的關係:
...
class FarmInfo
{
...
/**
* @var integer
*
* @ORM\Column(name="areaCovered", type="integer", nullable=true)
*/
private $areaCovered;
/**
* @var integer
*
* @ORM\Column(name="totalIrrigatedArea", type="integer", nullable=true)
*/
private $totalIrrigatedArea;
/**
* @var integer
*
* @ORM\Column(name="totalStaff", type="integer", nullable=true)
*/
private $totalStaff;
/**
* @var DairyInfo
* @ORM\OneToOne(targetEntity="DairyInfo", mappedBy="farmInfo", cascade={"persist","remove"})
*/
private $dairyInfo;
...
}
和
...
class DairyInfo
{
...
/**
* @var FarmInfo
*
* @ORM\OneToOne(targetEntity="FarmInfo", inversedBy="dairyInfo", cascade={"persist"})
* @ORM\JoinColumn(name="farmInfoId", referencedColumnName="id")
*
*/
private $farmInfo;
/**
* @var integer
*
* @ORM\Column(name="peakCowMilked", type="integer", nullable=true)
*/
private $peakCowMilked;
/**
* @var integer
*
* @ORM\Column(name="cowsMilkedInWinter", type="smallint", nullable=true)
*/
private $cowsMilkedInWinter;
...
}
這裏formTypes:
class FarmInfoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('areaCovered');
$builder->add('totalIrrigatedArea');
$builder->add('totalStaff');
$builder->add('dairyInfo', new DairyInfoType());
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FOO\CoreBundle\Entity\FarmInfo'
));
}
/**
* @return string
*/
public function getName()
{
return 'foo_corebundle_farminfo';
}
}
class DairyInfoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('peakCowMilked');
$builder->add('cowsMilkedInWinter');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FOO\CoreBundle\Entity\DairyInfo'
));
}
/**
* @return string
*/
public function getName()
{
return 'foo_corebundle_dairyinfo';
}
}
創建表單我與FarmInfo和DairyInfo以相同的形式,我想,但我堅持,FarmInfo堅持OK和DairyInfo堅持所有領域,但farmInfoId總是空:
+---------------+--------------------+------------+
| peakCowMilked | cowsMilkedInWinter | farmInfoId |
+---------------+--------------------+------------+
| 11 | 11 | NULL |
+---------------+--------------------+------------+
我有什麼做的到symfony自動獲取此ID?
謝謝大家的提前。
非常感謝!這是問題!我在嘗試一切,並且是如此簡單的事情。 – Bruno
我不能給你答案,因爲我沒有足夠的聲望,但你幫了我很多! – Bruno
沒問題。這實際上是一個非常普遍的問題。只是不容易谷歌的東西。 – Cerad