我所有的 Doctrine2 设置都是在 YAML 文件中完成的。我有一个名为 LoanAppMenuProgress 的实体类,我试图在其中执行 prePersist 函数。此 LoanAppMenuProgress 实体与另一个名为 LoanApp 的类具有 oneToOne 关系。与数据库中的 LoanApp 表关联的 LoanAppMenuProgress 表上有一个外键关联。
我在 LoanApp.LoanAppMenuProgress.orm.yml 中有我的 LoanAppMenuProgress 类的这个配置:
LoanEv\LoanAppBundle\Entity\LoanApp\LoanAppMenuProgress:
type: entity
repositoryClass: LoanEv\LoanAppBundle\Repository\LoanApp\LoanAppMenuProgress
table: loan_app_menu_progress
id:
id:
type: integer
generator: { strategy: auto }
### This is the OWNING side of the relationship
oneToOne:
loan_app:
targetEntity: LoanApp
inversedBy: loanapp_menu
joinColumn:
name: loan_id
referencedColumnName: id
fields:
loan_id:
type: integer
menu_id2:
type: integer
menu_id3:
type: integer
menu_id4:
type: integer
lifecycleCallbacks:
prePersist: [ updateMainMenuStatus ]
这是我的 LoanApp.LoanApp.orm.yml 文件:
LoanEv\LoanAppBundle\Entity\LoanApp\LoanApp:
type: entity
repositoryClass: LoanEv\LoanAppBundle\Repository\LoanApp\LoanAppRepository
table: loan_app
id:
id:
type: integer
generator: { strategy: auto }
## This is the INVERSE side of the relationship.
oneToOne:
loanapp_menu:
targetEntity: LoanAppMenuProgress
mappedBy: loan_app
fields:
bank_id:
type: integer
# etc.
在我的 LoanAppMenuProgress 实体类中,我有以下代码:
namespace LoanEv\LoanAppBundle\Entity\LoanApp;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Util\Debug;
/**
* LoanEv\LoanAppBundle\Entity\LoanApp\LoanAppMenuProgress
*/
class LoanAppMenuProgress
{
private $id;
private $loan_id;
/**
* @var LoanEv\LoanAppBundle\Entity\LoanApp\LoanApp
*/
private $loan_app;
private $menu_id2 = 0;
private $menu_id3 = 0;
private $menu_id4 = 0;
// ...
public function updateMainMenuStatus()
{
echo("Inside prePersist's updateMainMenuStatus function. ");
}
}
从我的 LoanAppController 类中调用以下代码:
// ...
//Save the menuStatus changes.
echo("About to persist. ");
$em->persist($menuStatus[0]);
echo("Done persisting.");
$em->flush();
// ...
当我在 LoanAppController 中执行代码时,以下内容会写入我的屏幕:
“即将坚持。坚持完成。”
我在输出应该读取的中间缺少那个位:
“即将持久化。在 prePersist 的 updateMainMenuStatus 函数中。完成持久化。”
更改正在写入数据库,系统的所有功能都按预期工作,但 prePersist() 除外。我已经为 yml 设置苦苦挣扎了很长一段时间,所以我最初的假设是我的 YAML 设置不正确。
文档(据我所知)提到我应该将生命周期回调:和 prePersist:项目添加到 yml 文件中,然后确保我在持久化实体中有一个公共函数。显然,我错过了一些东西。
有没有人有任何想法?
谢谢。