问题
I'm trying since several days to find out how to ask to easyadmin to store the author_id
(or user_id
to be specific), but I can't find by myself.
Basicly, by using the createListQueryBuilder
function in a specific controller to override the easyadmin controller, I found the way to display for each logged user, the articles they created.
Now I'm locked since several days as i dont know how to tell to easyadmin to store the currently logged author_id
(or user_id),the solution is to get the author_id with $this->getUser()->getId()
that's ok, but where or how can i link this with my author_id
before persisting it to the database
For now if i use this code below the author can add manually its author_id but that's not safe:
{ property: 'user', type: 'entity', type_options: { class: 'AppBundle\Entity\User' ,multiple: false,expanded: true} }
I would like to use an input hidden or any other way that could be more safe
Thank you any anybody could help me
EDIT:it was the hell to find the solution but i finaly found the way to display ONLY THE CURRENT LOGGED USER(who can be a basic admin or contributor if you prefer) or THE FULL LIST OF REGISTERED USERS IN DATABASE if the current logged user as a specific role (in this case that's the website main admin)
here is the solution:
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\User;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations\Route;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AlterPHP\EasyAdminExtensionBundle\EasyAdminExtensionBundle;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
/**
* Article controller.
*
* @Route("articles")
*/
class ArticlesController extends BaseAdminController
{
/**
* @Route("/", name="easyadmin")
*/
public function indexAction(Request $request)
{
return parent::indexAction($request);
}
public function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
if(!$this->getUser()->hasRole('ROLE_DIRECTION_ADMIN'))
{
$response = parent::createListQueryBuilder('Articles', $sortDirection, $sortField, $dqlFilter); // TODO: Change the autogenerated stub
$response->where('entity.userId = :userId')->setParameter('userId', $this->getUser()->getId());
return $response;
}
$response = parent::createListQueryBuilder('Articles', $sortDirection, $sortField, $dqlFilter); // TODO: Change the autogenerated stub
return $response;
}
public function createEntityFormBuilder($entity, $view)
{
$formBuilder = parent::createEntityFormBuilder($entity, $view);
// Here I overwrite field to be disabled
if(!$this->getUser()->hasRole('ROLE_DIRECTION_ADMIN'))
{
$formBuilder->add('user', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.id = :id')->setParameter('id',$this->getUser()->getId());
},
));
return $formBuilder;
}
$formBuilder->add('user', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'asc');
},
));
return $formBuilder;
}
}
1/i created a custom ArticlesController and added 1 method to display the logged user article list if they have a low level role,and display all the website articles if the user role is high
2/ i created a second method which add the currently logged user_id field to the easyadmin formbuilder if the user has a low level role(a contributor for example) OR a full list of users in case the currently logged user is the website main admin
来源:https://stackoverflow.com/questions/51711266/symfony-easyadmin-new-articles-are-submitted-and-stored-but-not-the-author-id