I am very new to EasyAdmin bundle. I was trying to load user specific content in the list of a logged in user.
So here is my composer.json
file:
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"easycorp/easyadmin-bundle": "^2.3",
"sensio/framework-extra-bundle": "^5.1",
"symfony/asset": "5.0.*",
"symfony/console": "5.0.*",
"symfony/dotenv": "5.0.*",
"symfony/expression-language": "5.0.*",
"symfony/flex": "^1.3.1",
"symfony/form": "5.0.*",
"symfony/framework-bundle": "5.0.*",
"symfony/http-client": "5.0.*",
"symfony/intl": "5.0.*",
"symfony/mailer": "5.0.*",
"symfony/monolog-bundle": "^3.1",
"symfony/notifier": "5.0.*",
"symfony/orm-pack": "*",
"symfony/process": "5.0.*",
"symfony/security-bundle": "5.0.*",
"symfony/serializer-pack": "*",
"symfony/string": "5.0.*",
"symfony/translation": "5.0.*",
"symfony/twig-pack": "*",
"symfony/validator": "5.0.*",
"symfony/web-link": "5.0.*",
"symfony/yaml": "5.0.*"
},
I have a Project Requirements entity which has a many to many relationship with the User entity like the following:
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="projectRequirementsHasUserProjectRequirements")
* @ORM\JoinTable(name="project_requirements_has_user",
* joinColumns={
* @ORM\JoinColumn(name="project_requirements_has_user_project_requirements_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="project_requirements_has_user_user_id", referencedColumnName="id")
* }
* )
*/
private $projectRequirementsHasUserUser;
/**
* Constructor
*/
public function __construct()
{
$this->projectRequirementsHasUserUser = new \Doctrine\Common\Collections\ArrayCollection();
}
So when adding requirements I can assign user like this:
You can see that the first requirements has assigned to 2 users:
Now I want to show all the requirements which has assigned to the logged in user.
I came to know that I need to override the default EasyAdminController, so I configured my entity in config/packages/easy_admin.yaml
:
# Project Planning
ProjectRequirements:
controller: App\Controller\ProjectRequirementsController
class: App\Entity\ProjectRequirements
And then create a controller but don't know how to write the filter or query to select only the requirements which has assigned to the logged in user:
<?php
namespace App\Controller;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
class ProjectRequirementsController extends EasyAdminController {
protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
/** @var QueryBuilder $result */
$result = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
return $result;
}
}
Any help is highly appreciable.
After searching a lot about 2 days finally I understand how EasyAdmin and Symfony QueryBuilder
works. It was nice to find the solution by myself. So here is what I came up with the solution:
# App\Controller\ProjectRequirementsController.php
protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
/** @var QueryBuilder $result */
$result = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
# Getting data User wise
$result->leftJoin('entity.projectRequirementsHasUserUser', 'user')
->andWhere('user.id = :user')
->setParameter('user', $this->getUser());
return $result;
}
Hope this helps someone in the future.