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

undefined method in controller from entity symfony

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

ClientAdmin.php喜欢Sonata Documentation的建议去做。 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;
}

错误:

解决方案: table "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();
    }
}

}

你的代码完全错误。

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

这意味着您调用了 class ClientAdminController(即 $this)

的 setMoney 方法

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