[英]How to query another entity in entity class in symfony2 using doctrine
I'm trying to query using entity manager in a entity class file but I'm getting this error: 我正在尝试在实体类文件中使用实体管理器进行查询,但出现此错误:
FatalErrorException: Error: Call to undefined method Acme\MasoudBundle\Entity\User::getDoctrine() in /var/www/test/src/Acme/MasoudBundle/Entity/User.php line 192
my entity class is : 我的实体类是:
namespace Acme\MasoudBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* @inheritDoc
*/
public function getRoles()
{
$em = $this->getDoctrine()->getManager();
$Permission= $em->getRepository('MasoudBundle:Permission')->find(1);
$this->permissions[]=$Permission->permission;
return $this->permissions;
}
}
I want to have a permission and authentication system like this, can you help me please? 我想要一个这样的许可和身份验证系统,您能帮我吗? there are 5 tables, a user table, a group table, a permission table, and a group_permission and a user_group table.
有5个表,一个用户表,一个组表,一个权限表以及一个group_permission和一个user_group表。 so After user logins, I want to check which user is for which group, and get the groups permission.
所以在用户登录后,我想检查哪个用户属于哪个组,并获得组权限。 how can I do that?
我怎样才能做到这一点? please help me as much as you have time.
有空的时候请帮我。
Your entity should not know about other entities and the Entity Manager because of the separation of concerns . 由于关注点分离,您的实体不应该了解其他实体和实体管理器 。
Why don't you simply map your User
to the appropriate Role(s) (instances of Permission
entity in your case) using Doctrine Entity Relationships/Associations . 为什么不简单地使用Doctrine Entity Relationships / Associations将
User
映射到适当的Role(在您的情况下为Permission
实体的实例)。 It will allow you to access the appropriate permissions of a given user from the User
instance itself. 它将允许您从
User
实例本身访问给定用户的适当权限。
In this line: 在这一行:
$em = $this->getDoctrine()->getManager();
$this
refers to the current class, the User Entity
that does not have a method called getDoctrine()
. $this
引用当前类,即没有名为getDoctrine()
的方法的User Entity
。 $this->getDoctrine()
works in controllers where you extend the Controller
class a subclass of ContainerAware
which contains the getDoctrine()
method. $this->getDoctrine()
在控制器中工作,在该控制器中,您将Controller
类扩展为ContainerAware
的子类,其中包含getDoctrine()
方法。
In other terms, this method works only on objects of class container or its subclasses, like this: $controller->getDoctrine()->getManager()
. $controller->getDoctrine()->getManager()
,此方法仅适用于类容器或其子类的对象,如下所示: $controller->getDoctrine()->getManager()
。
Besides, you don't want to have an EntityManager inside your entity classes, that's not a good way of doing things. 此外,您不希望在实体类中使用EntityManager,但这不是一种好方法。 You would better use listners to do such stuffs
您最好使用列表器执行此类操作
I solved this: 我解决了这个问题:
global $kernel;
$em = $kernel->getContainer()->get('doctrine')->getManager();
$role = $em->getRepository('BackendBundle:user_types')->findOneBy(array(
'id' => 10
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.