Reputation: 63
I need some help to understand what I'm doing wrong, I've tried to search one answer here, but nothing work.
here my problem..
I have two entities with a one-to-one relationship:
...
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;
...
}
and
...
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;
...
}
here the 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';
}
}
That create the form for me with the FarmInfo and the DairyInfo in the same form as I want, but when I persist, FarmInfo persist ok and the DairyInfo persist all fields but the farmInfoId is always null:
+---------------+--------------------+------------+
| peakCowMilked | cowsMilkedInWinter | farmInfoId |
+---------------+--------------------+------------+
| 11 | 11 | NULL |
+---------------+--------------------+------------+
What do I have to do to symfony get this Id automatically ?
Thank you all for advance.
Upvotes: 3
Views: 4666
Reputation: 48865
You didn't show your methods but I am fairly certain you have:
class FarmInfo
{
setDairyInfo($dairyInfo)
{
$this->dairyInfo = $dairyInfo;
}
}
The problem is that nothing is setting farmInfo in your dairyInfo class. So modify setDairyInfo to:
setDairyInfo($dairyInfo)
{
$this->dairyInfo = $dairyInfo;
$dairyInfo->setFarmInfo($this);
}
And you should be good to go.
Upvotes: 8