Khandaker Toihidul Islam
Software Engineer
Backend : PHP | Mysql | Symfony | CakePhp | Wordpress | Drupal
Frontend : Node js | Vue js | React Js | Javascript | HTML | CSS | Bootstrap
Tools , Server : Nginx | Apache | Git | Jenkins | Jira | Asana | Silenium | Spira

Custom list view and search of Easy Admin

Config easy_admin.yml

      Student:
        controller: App\Controller\StudentController

Custom list: It shows only the owner,s data

protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null){
	/** @var QueryBuilder $result */
	$result = parent::createListQueryBuilder($entityClass, 
        $sortDirection, $sortField, $dqlFilter);
	if(method_exists($entityClass,'getUserId') && $this->getUser()->getRoles()[0] !== 'ROLE_ADMIN'){
	     $result->andWhere('entity.userId = :user');
	     $result->setParameter('user', $this->getUser());
	}
	 return $result;
}

Custom search : It shows only the owner,s data

protected function createSearchQueryBuilder($entityClass, $searchQuery, array $searchableFields, $sortField = null, $sortDirection = null, $dqlFilter = null){
	/** @var QueryBuilder $result */
	$result = parent::createSearchQueryBuilder($entityClass, $searchQuery, $searchableFields, $sortField, $sortDirection, $dqlFilter);
	if(method_exists($entityClass,'getUserId') && $this->getUser()->getRoles()[0] !== 'ROLE_ADMIN'){
		$result->andWhere('entity.userId = :user');
		$result->setParameter('user', $this->getUser());
	}
	return $result;
}

More details https://symfony.com/doc/master/bundles/EasyAdminBundle/book/complex-dynamic-backends.html#admincontroller-properties-and-methods

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *