Ben_hawk
Ben_hawk

Reputation: 2486

Symfony Entity on first save

In Symfony using doctrine, whats the best way to run a certain function just before an entity is saved/persisted to the database. I only want it to be run once in its lifetime so I guess the construction function is no good.

Upvotes: 0

Views: 829

Answers (2)

Mirage
Mirage

Reputation: 31548

or

You can either call that function just before the $em->persist($entity); function in the controller.

But PrePersist is the cleaner way but for testing and small stuff , you can put before the save function

Upvotes: 0

AlterPHP
AlterPHP

Reputation: 12727

Use LifecycleCallback named PrePersist as explained in the doc : http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks

Upvotes: 1

Related Questions