S
S
Stanislav Shendakov2020-06-18 07:35:32
symfony
Stanislav Shendakov, 2020-06-18 07:35:32

How to get user after post creation?

Good afternoon.

The work is done under Easy admin. The point is simple. After creating the news (record in the database), you need to update some data in the table. I do it like this:

/**
 * @ORM\Entity(repositoryClass=OrgNewsRepository::class)
 * @ORM\HasLifecycleCallbacks()
 * @Vich\Uploadable
 */
class OrgNews
{
...
/**
     * @ORM\PrePersist
     */
     public function setValuesAfterCreate()
     {
        $this->updatedAt = new \DateTime('now');
     }
...
}

In addition to updating the updatedAt field (I'm aware that this can be done at the database level), you also need to change the AuthorID field, substituting the ID of the current user. Those. you need to get the data via getUser. I tried by examples, in various ways, through services, through the implementation of Security, I got out to get a Container, but it still doesn’t work. When I try to pass any service through the constructor I get: Too few arguments to function App\Entity\OrgNews::__construct(), 0 passed in C:\inetpub\wwwroot\portal_plus\vendor\easycorp\easyadmin-bundle\src\Controller\AdminControllerTrait .php on line 622 and exactly 1 expected. I feel that everything should be simple. Anyone share a working solution?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg, 2020-06-18
@shindax

Create an Event listener .
Here is an example of how to pass security token storage.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question