Hello I try to create an entity when another linked entity is created via a postPersist method but I find myself making this error someone knows why? I can not find the reason.
In ClientAdmin.php like the Sonata Documentation advice to do. Sonata Doc
public function postPersist($client)
{
if ($client instanceof Client )
{
$money = new Money();
$money->setClient($client);
$money->setSurname($client->getSurname());
$money->setFirstname($client->getFirstname());
}
}
Client.php :
/**
* @ORM\OneToOne(targetEntity="Money", mappedBy="client", cascade={"persist", "remove"})
*/
protected $money;
/**
* Set money
*
* @param \AppBundle\Entity\Money $money
*
* @return Client
*/
public function setMoney(\AppBundle\Entity\Money $money )
{
$this->money = $money;
}
/**
* Get money
*
* @return \AppBundle\Entity\Money
*/
public function getMoney()
{
return $this->money;
}
The error :
Solution : Working but nothing is create is the table "Money" so i'm supposed it because I don't persist and flush it but I can't do it in it . :/
Working on Symfony 3.3 with SonataAdmin 3.19
Thanks in advance !
Edit : Solution found :
public function postPersist($client)
{
$em = $this->getConfigurationPool()->getContainer()->get('doctrine.orm.entity_manager');
if ($client instanceof Client )
{
$money = new Money();
$money->setClient($client);
$money->setSurname($client->getSurname());
$money->setFirstname($client->getFirstname());
$em->persist($money);
$em->flush();
}
}
}
your code is totally wrong.
$this->setMoney(new Money()); }
this means you call setMoney method of the class ClientAdminController(which is $this)
but ClientAdminController does not have the method setMoney(Money). You have to call it on a Client instance.