Reputation: 15
I have two entities one is Role and other is User , I want to build forms and reports to add and show each role with their users , and to create a user with one Role so its User:Role (One-To-Many), I managed to add role to a user via Doctrine 2 but I cannot show users fro each role below is my code
<?php
/**
* Description of Role
* @Entity
* @Table=(name"Roles")
* @author alaaqashou
*/
class Role {
//put your code here
/**
*
* @var integer $id
* @Column(name="id", type="integer",nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @Column(length=100,nullable=false,unique=true)
* @var type
*/
private $name;
/**
* @OneToMany(targetEntity="User" ,mappedBy="Role")
* @var type
*/
private $users;
public function __construct() {
$this->users=new \Doctrine\Common\Collections\ArrayCollection();
}
public function getUsers() {
return $this->users;
}
public function setUsers($user) {
$this->users->add($user);
}
}
/**
* Description of User
*@Entity
* @Tabel(name="Users")
* @author alaaqashou
*/
class User {
//put your code here
/**
*
* @var integer $id
* @Column(name="id", type="integer",nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
*
* @Column(length=255,nullable=false,unique=true)
*
*
* @var type
*
*/
private $role;
function __construct() {
$this->created=new \DateTime(date("Y-m-d H:i:s"));
}
public function getRole() {
return $this->role;
}
public function setRole($role) {
$this->role = $role;
}
}
I got the Notice: Undefined index: Role error when I try to do the following
my Service
public function listAllRole()
{
return $this->em->getRepository('sihha\Entity\Role')->findAll();
}
$roles=$this->roleService->listAllRole();
$users=$roles[0]->getUsers();
// I even tried $users=$roles[0]->getUsers()->toArray();
$user=$users[0];
Please Help!!!
Upvotes: 0
Views: 219
Reputation: 10356
I think your problem lies in the annotations. Try replacing mappedBy='Role'
width mappedBy='role'
(i.e. type "role" in lowercase).
I just tried mappedBy="Table"
in one of my entities and it gave me the same result you seem to be having.
Upvotes: 1