I'm on Symfony 2.8. In my Work entity I've got:
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\WorkClaim", mappedBy="claimedWork", cascade={"persist"})
*/
private $claims;
/**
* @param WorkClaim $claim
*/
public function addClaim(WorkClaim $claim)
{
$claim->setClaimedWork($this);
$this->claims->add($claim);
}
/**
* @param WorkClaim $claim
*/
public function removeClaim(WorkClaim $claim) { /* to be done */ }
(and getter/setter and of course $this->claims = new ArrayCollection(); in the Constructor)
In my WorkClaim entity:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Work", inversedBy="claims")
*/
private $claimedWork;
(and getter/setter)
My WorkType class has this in the form builder:
->add('claims', CollectionType::class, array(
'entry_type' => WorkClaimType::class,
'allow_add' => true,
'by_reference' => false,
))
WorkClaimType has just some typical fields (EntityType to choose country, two DateType fields and NumberType).
No matter what I do, the addClaim() is called, but the new WorkClaim entities are added with NULL claimed_work_id. What did I miss?
Do I really have to persist every WorkClaim separately in Work controller on createAction()? And if so, how to correctly iterate through them? Trying to call getClaims() on form data gives me no records.