来自实体symfony的控制器中的未定义方法

时间:2017-07-13 10:36:32

标签: php symfony sonata-admin

你好我尝试通过postPersist方法创建另一个链接实体时创建一个实体,但我发现自己犯了这个错误有人知道为什么?我找不到原因。

ClientAdmin.php 中,像Sonata文档建议一样。 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;
}

错误:

解决方案:  工作,但没有什么是创造是表"金钱"所以我认为这是因为我不会坚持并冲洗它,但我无法做到这一点。 :/

使用SonataAdmin 3.19处理Symfony 3.3

提前致谢!

修改:找到解决方案:

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();
    }
}

}

1 个答案:

答案 0 :(得分:1)

你的代码是完全错误的。

$this->setMoney(new Money()); }  

这意味着你调用ClientAdminController类的setMoney方法(这是$ this)

但ClientAdminController没有setMoney(Money)方法。您必须在客户端实例上调用它。