开发者

Doctrine2 lifecycleCallbacks prePersist not firing w/YAML config

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: 开发者_如何学Cid

  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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜