Masoud Motallebipour
Masoud Motallebipour

Reputation: 414

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. 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.

Upvotes: 0

Views: 10147

Answers (3)

Fernando León
Fernando León

Reputation: 514

I solved this:

global $kernel;
$em = $kernel->getContainer()->get('doctrine')->getManager();
$role = $em->getRepository('BackendBundle:user_types')->findOneBy(array(
            'id'    => 10
        ));

Upvotes: 1

S.Thiongane
S.Thiongane

Reputation: 6905

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() works in controllers where you extend the Controller class a subclass of ContainerAware which contains the getDoctrine() method.

In other terms, this method works only on objects of class container or its subclasses, like this: $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. You would better use listners to do such stuffs

Upvotes: 2

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

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. It will allow you to access the appropriate permissions of a given user from the User instance itself.

Upvotes: 2

Related Questions