我有一节课:
Article (id, code)在创建文章时,我需要在字段代码中添加id。在我的数据库中,我需要有:
(id, code)
1, ARTICLE_1
2, ARTICLE_2实际上,字段代码等于ARTICLE + ID,我尝试用以下方法来完成这个字段:
public function __construct() {
$this->code = "ARTICLE_" . $this->getId();
}但是,当我添加我的文章时,Id不会生成,函数getId什么也不返回。我试着测试postPersist()函数,但没有成功。
这方面有什么好的做法吗?
谢谢
德约
发布于 2014-10-27 10:39:04
这就是解决方案:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#lifecycle-callbacks
我将注释@ORM\HasLifecycleCallback添加到我的实体中,并且我有一个函数:
/** @ORM\PostPersist */
public function doStuffOnPostPersist()
{
$this->setCode('ARTICLE_' . $this->getId());
}有了这个解决方案,我就有了id,因为我已经坚持了。
谢谢。
发布于 2014-10-15 14:11:40
在PHP中,您不能使用"+“操作符将字符串与变量连接起来,而只能使用”。符号。
试一试:
$this->code = "ARTICLE_" . $this->getId();
https://stackoverflow.com/questions/26384631
复制相似问题