Reputation: 1276
I have simple entity called Post.
/**
* Class Post
* @package App\Entity
*
* @ORM\Entity(repositoryClass="App\Repository\Blog\PostRepository")
* @ORM\Table(name="posts")
* @ORM\HasLifecycleCallbacks()
*
*/
class Post
{
/**
* @var \DateTime
* @ORM\Column(type="datetime", options={"default":"CURRENT_TIMESTAMP"})
*/
protected $createdDateTime;
/**
* @ORM\PrePersist()
*/
public function testEvent()
{
$this->createdDateTime = '2018-11-11';
//dump(123); exit; this is not working either
}
}
Now I am using Nelmio Alice Bundle to generate some database fixtures and when I am done with that I loop through them to persist what I have into the database.
foreach($this->fixtures as $fixture) {
$this->entityManager->persist($fixture);
if($counter === self::BATCH_SIZE['MEDIUM']) {
$this->entityManager->flush();
$this->entityManager->clear();
}
counter++;
}
As you can probably guess, nothing happens in terms of events. I think it's not being emitted for some reason.
What may be important - I am doing that from functional test level. Am I right in thinking that this is the reason why I can get that to work?
Upvotes: 1
Views: 799
Reputation: 1276
So the problem seems to be in the fact that Symfony is not catching and handling Doctrine's LifeCycle events correctly. It was supposed to call a method of my choice on PrePersist event - setter for $createdDateTime in this case.
I am running that in my functional test so maybe that is the issue here? I do not think I have called persist() before, which would cause that problem. My script also works as expected when correct data is provided.
I believe it's Symfony's fault because when I have created my own event subscriber for PrePersist event I was able to actually handle that event.
It's not a solution but workaround - create your own event listener or subscriber and take care of that there.
Upvotes: 1