I wanna add a button in the sonata admin list grid depending if the object satisfies a condition or not.
I have a customize type template with this code :
{% if object.myMedhod == true %}
///if response is true add a button in sonata admin list grid
{% endif %}
my entity extends a model class where I have myMedhod()
my ModleClass:
namespace myprject/myBundle/Model;
use Doctrine\ORM\EntityManager;
class XModel
{
private entityManager;
public function __constuct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function myMethod()
{
$this->entityManager->getDoctrine()->getRepostiroy(xxx)->find(xxx)
}
}
my service:
services:
my_service:
class: myProject/myBundle/Model/XModel
arguments:
- @doctrine.orm.entity_manager
I think all in my code is right but I get a issue getting the entityManager... seems how if the construct method from the class is not called!!
the bug that throw symfony is :
Call to a member function getDoctrine() on a non-object
//////////////////////////////////////////////////////////////////////
Ok I have solved changing the approach!
in the twig template instead use the object and call to a method in the a model class,
I call now the a method in the adminClass and I pass the object so:
{% if admin.myMedhod(object) == true %}
{% endif %}
then in the service form the adminClass I added
calls:
- [setSecurityContext, [@security.context]]
- [setEntityManager , [@doctrine.orm.entity_manager]]
in the admin Class :
public function setEntityManager($entityManager) {
$this->entityManager = $entityManager;
}
public function getEntityManager() {
return $this->entityManager;
}
public function myMedhod($object){
$condition=$this->getEntityManager()->getRepository(xxx)->findOneBy(array("field"=>$object->getId()));
if(empty($condition)){
return true;
}else{
return false;
}
}
thanks for your help everybody