Symfony 3.4 — how to write a Listener that is called only when writing changes to the Database?

Hi All,

how to write a Listener that is called only when recording changes in the Database.

It is necessary to intercept and modify, but only when there is Update in the database.
not in the Insert
April 19th 20 at 12:05
1 answer
April 19th 20 at 12:07
Solution
I have some reason when the Insert is also called. - itzel_Kreiger40 commented on April 19th 20 at 12:10
@itzel_Kreiger40, try to check aydishnik in update. maybe I'm wrong, but the first time you save it will not be in preUpdate. If not aydishnik, skip the event and do nothing. - vesta_Mills commented on April 19th 20 at 12:13
please don't laugh, but some data is lost somewhere after the update to the database...
for example, relacia to other tables - itzel_Kreiger40 commented on April 19th 20 at 12:16
in preUpdate, because you can just Setter-Omi to work:
$entity = $args->getObject();
$entity->setMyField('my value');


and then persisting necessary?
$em = $args->getObjectManager();
$em->persist($entity);
$em->flush();
- itzel_Kreiger40 commented on April 19th 20 at 12:19
@itzel_Kreiger40to persisteth don't need flashit also, you only need to change the data, all a flush will occur at the end of the transaction - vesta_Mills commented on April 19th 20 at 12:22
super thanks, I think I found the cause of their troubles.

I have checked Listener on the user.
If not who zalogowany. the grass wonders. the question is why???

verification looks like this:
if ($this->tokenStorage->getToken()->getUser()->getUsername()){
//any action
}
- itzel_Kreiger40 commented on April 19th 20 at 12:25
@itzel_Kreiger40where you got tokenStorage? The entity does not work autofair - vesta_Mills commented on April 19th 20 at 12:28
so I as argument has passed. and in the designer prescribed - itzel_Kreiger40 commented on April 19th 20 at 12:31
@itzel_Kreiger40, I'm writing about, and autofair looks to the designer. at least, you had to specify a property tokenStorage and define it in the constructor. show me the code. - vesta_Mills commented on April 19th 20 at 12:34
all the problem is solved, thank you so much for the help!

the problem here if the user is not saligenin, the course may not be used in the method getUsername():

if ($this->tokenStorage->getToken()->getUser()->getUsername()){
//any action
}
- itzel_Kreiger40 commented on April 19th 20 at 12:37
@itzel_Kreiger40, once again I will write: in fact to injecting is not necessary, the entity must be removed from the service container. The Symphony itself will give preUpdate user, if you need one. - vesta_Mills commented on April 19th 20 at 12:40
so I'm in essence (entity) and not injecter.
I injectal of the Listener

namespace App\EventListener;
use ...


class SearchIndexer
{

private $tokenStorage;

 function __construct(TokenStorageInterface $tokenStorage)
{
 $this->token_storage = $token_storage;
}


 public function preUpdate(LifecycleEventArgs $args)
{
 $entity = $args->getObject();

 if (!$entity instanceof Product) {
return;
}

 if ($this->tokenStorage->getToken()->getUser()->getUsername()){
 //any action
}

}
}
- itzel_Kreiger40 commented on April 19th 20 at 12:43
or something is not right? - itzel_Kreiger40 commented on April 19th 20 at 12:46
@itzel_Kreiger40, right - vesta_Mills commented on April 19th 20 at 12:49

Find more questions by tags Doctrine ORMSymfony