Doctrine2生命周期回调prePersist未使用YAML配置触发

发布于 2024-12-08 12:21:19 字数 2721 浏览 0 评论 0原文

我所有的 Doctrine2 设置都是在 YAML 文件中完成的。我有一个名为 LoanAppMenuProgress 的实体类,我试图在其中执行 prePersist 函数。此 LoanAppMenuProgress 实体与另一个名为 LoanApp 的类具有 oneToOne 关系。 LoanAppMenuProgress 表上有一个外键关联,该表与数据库中的 LoanApp 表关联。

我在 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 文件中,然后确保我在持久实体中有一个公共函数。显然,我错过了一些东西。

有人有什么想法吗?

谢谢。

All my Doctrine2 setups are done within YAML files. I have an entity class named LoanAppMenuProgress where I'm trying to execute a prePersist function. This LoanAppMenuProgress entity has a oneToOne relationship with another class named LoanApp. There is a foreign key association on the LoanAppMenuProgress table associated with the LoanApp table in the DB.

I have this config for my LoanAppMenuProgress class in LoanApp.LoanAppMenuProgress.orm.yml:

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 ]

This is my LoanApp.LoanApp.orm.yml file:

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.

In my LoanAppMenuProgress Entity class, I have the following code:

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. ");
    }
}

The following code is called from within my LoanAppController class:

// ...

//Save the menuStatus changes.
echo("About to persist. ");
    $em->persist($menuStatus[0]);
echo("Done persisting.");
    $em->flush();

// ...

When I execute the code in the LoanAppController the following gets written to my screen:

"About to persist. Done persisting."

I'm missing that bit in the middle where the output should read:

"About to persist. Inside prePersist's updateMainMenuStatus function. Done persisting."

The changes ARE getting written to the database, and all the functionality of the system is working as expected with the exception of the prePersist(). I've struggled with the yml setups for quite a while so my initial assumption is that my YAML setup is incorrect.

The documentation (as far as I could understand it) mentions that I should add the lifecycleCallbacks: and prePersist: items to the yml file, and then make sure I have a public function in the persisting Entity. Obviously, I'm missing something.

Does anyone have any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2024-12-15 12:21:19

仅当您执行 INSERT 类型语句时才会调用 prePersist。此事件永远不会在更新操作时触发。
要在更新实体时执行某些操作,请使用 preUpdate。请注意,preUpdate 对相关实体可以执行的操作有更多限制。

德里克

prePersist only gets called when you are performing an INSERT type statement. This event will never fire on an UPDATE action.
To perform some action when an entity is being UPDATEd, use preUpdate. Note, that preUpdate has far more restrictions on what can be performed with the entity in question.

Derrick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文