18.
Event Hooks
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
11 Comments
(Note for some reason I can't see the entire post disqus is being weird)
So I am having a bit of a complicated problem (It is related I assure you), I had a thought what if I had a base entity that implemented a UpdatedByUser trait for all the entities, then I a global event within easy admin to update this field when ever an admin user updates a table.
Pretty cool, well, auditing has been my favourite things of all time, also being able to blame the person has merit too.
I am getting the
`Call to a member function setLastUpdatedBy() on array
`error, and I am not sure why nor do I understand it.
So I created a trait:
Then in my entities I simply used the trait - awesome.
So I come to the event:
And try it then BOOM..
I get an error I simply do NOT understand, Yes I did some hunting around just to try and understand it. Ideas?
Hey Peter,
The answer probably is very simple: onPreUpdate() is called on every entity, so first of all you need to make sure you call setLastUpdatedBy() on the correct entity. The easiest solution is to add a check before calling setLastUpdatedBy() to make sure we should handle the entity:
Instead of "method_exists()" you may use "$entity instanceof YourEntityNameHere", but if you want to handle more than one entity - I'd recommend to create an interface with "setLastUpdatedBy()" declared and implement it in any entity that has setLastUpdatedBy(). Then you can check as "$entity instanceof YourCustomInterfaceNameHere".
Cheers!
I see what your saying, but when I add the trait to the entity, it includes the set/getLastUpdatedBy methods, so using an interface would seem redundant. The ttrait methods work without any problems, but for some reason when this event fires it dosn't see the method. I am probably giving myself a place to look, I guess asking here was my hope that I could short circuit some debugging, since you guys use Symfony more than I do :)
And to add more this since I have been debugging, even if I do
`dd($entity->getId());
`it still reports back with the "Call to a member function getId() on array" error. Now every Entity has an ID, it's set directly in the entity not through a trait.
Lucky I have no hair left to pull out. :)
Update 45 min later, I discovered that PRE_EDIT gives an array, where as PRE_UPDATE gives an object in the subject! well problem solved as to why events where not firing.
Thanks for your help.
Hey Peter,
Nope, it won't be redundant! Because Trait and interface are DIFFERENT things :) Interfaces require you to have the declared methods in it when traits just give you some implementation -so you can use both to make your code even more cool.
Glad you got it working, great catch! Though, checking if your entity is instance of some class or interface in your listener is still a good idea and probably the best practice! It will prevent possible odd cases like this one later.
Cheers!
Hey Victor
Yeah I understand that about interfaces and its a great way to enforce that methods are implemented, but there is no implementation requirements for the trait as it's independent. Maybe I don't understand what your saying.
Note: I modelled the trait after TimeStampable Trait from the Gedmo package.
as for the code here is the final bits:
Hey Peter,
Yeah, code looks great! About interfaces - yes, it's up to you. I'm just saying that you along the trait you use in your entities you can also create and implement an interface e.g. BlamableInterface. Yes, traits are independent, but that interface won't make those traits dependent - it just helps you to identify (group) similar entities and gives you confident that some necessary methods exist on the entity - I'd say your code will be more robust because it's perfect case here when you first check if method implements an interface with "instanceof", and then if so - call some specific methods on the object. But yeah, it's not required and you can bypass it with method_exists(). I just think using interfaces here would make code clearer.
Cheers!
Ahh, now I understand. Yeah your right it would make it clearer if in 12 months I go back to it and think "what the hell was I thinking" where as I could simple look at the interface line and get clarity on the code I am reading.
Thanks for spending the time to give me your thoughts.
Haha, yeah, but I'd also leave some comments just in case, because you know, 12 months is loooong ;)
Glad you agree!
Cheers!
Hi Team !
I struggle on two issues based on events, collection and doctrine uniqueConstraints
> uniqueConstraints is detected only when trying to insert in the DB which cause an exception.
So tried to catch the persist with an EventSubscriber.
I catch the prePersist event, but from here
> how can I cancel the persist and return a flash message ?
Thanks for reading me !
Hey Kaizoku!
> uniqueConstraints is detected only when trying to insert in the DB which cause an exception.
Hmm, so you have a UniqueConstraints annotation above your entity? In that case, you *also* need to add some Symfony validation, so that form validation fails. This will prevent the entity from being saved. How? With https://symfony.com/doc/cur... - configure this to check for the same unique fields as your UniqueConstraints.
I think this should fully solve your issue :).
Cheers!
W00T ! This works like a charm.
Ryan you're Chuck Noris rockstar ninja !
"Houston: no signs of life"
Start the conversation!