You have to define the correct relationship between user and role in your entity definition.
I am not sure why you use a linker table here. Do you want to actually have a many to many relationship between user and roles (every user can have several roles?).
If not you can easily move the role id into the user table and then you define a ManyToOne
relationship between user and role.
Your user table would then look like this:
-------------------------------------------
|user_id | name | email | role_id |
-------------------------------------------
| 1 | John | j@b.com | administrator |
-------------------------------------------
| 2 | Bob | b@j.com | staff |
-------------------------------------------
I would suggest to take a look at ManyToOne
with user as the owning side. You can check how to properly define your unidirectional many to one relation inside your entity definition here in the Doctrine2 documentation
After that you can simply call $user->getRole();
in your view...
EDIT
Answer to fix a one to many using a join table:
This is also described in the doctrine documentation here...
You need three tables; a user table, a role table and a user-role-linker table The user is an entity, the role is an entity the role-linker table is not an entity in your case. You should drop that entity, the linker table is only used for connecting the user and role in the database.
User table
---------------------------
|id | name | email |
---------------------------
| 1 | John | j@b.com |
---------------------------
| 2 | Bob | b@j.com |
---------------------------
Role table
-----------------
| id |
-----------------
| administrator |
-----------------
| staff |
-----------------
| guest |
-----------------
| another role |
-----------------
Linker table
--------------------------
|user_id | role_id |
--------------------------
| 1 | administrator |
--------------------------
| 2 | staff |
--------------------------
In your user entity:
/** ONE-TO-MANY UNIDIRECTIONAL, WITH JOIN TABLE ONLY WORK WITH MANY-TO-MANY ANNOTATION AND A UNIQUE CONSTRAINT
* @ORM\ManyToMany(targetEntity="Administration\Entity\Role")
* @ORM\JoinTable(name="user_role_linker",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id", unique=true)}
* )
*/
protected $roles;
/**
* Get roles.
*
* @return ArrayCollection
*/
public function getRoles()
{
return $this->roles;
}
/**
* Add a role to the user.
*
* @param Role $role
*
* @return User
*/
public function addRole(Role $role)
{
$this->roles[] = $role;
return $this;
}
/**
* Remove a role from the user
*
* @param Role $role
*
* @return User
*/
public function removeRole(Role $role)
{
$this->roles->removeElement($role);
return $this;
}
Your role entity:
/**
* An example entity that represents a role.
*
* @ORM\Entity
* @ORM\Table(name="role")
* @property string $id
*/
class Role
{
/**
* @var string
* @ORM\Id
* @ORM\Column(type="string", length=255, unique=true, nullable=false)
*/
protected $id;
/**
* Get the id.
*
* @return string
*/
public function getId()
{
return $this->id;
}
}
I think this should help you solve it...
Maybe you should add the following to your application module:
public function doctrineValidate(MvcEvent $event){
$application = $event->getParam('application');
$serviceManager = $application->getServiceManager();
$entityManager = $serviceManager->get('doctrine.entitymanager.orm_default');
$validator = new SchemaValidator($entityManager);
$errors = $validator->validateMapping();
if (count($errors) > 0) {
// Lots of errors!
var_dump($errors);
}
}
And then in bootstrap:
$eventManager->attach('dispatch', array($this, 'doctrineValidate'));
in module: Doctrine will help you by checking your entity definitions. It might tell you in advance that something is wrong in your entity definitions...