I have problem, early my entity have one role and this is sufficiently for project. But now my entity need have many role like ROLE_DEVELOPER, ROLE_COMPANY and when user doing some operation for entity add new role. And how best away for this situation, create new entity or change field role in array or what ? I have entity in table user and have one ROLE_USER and now user want to create Team, for team needed role ROLE_TEAM, and how to set new role? Then User want to create client entity and I need set ROLE_CLIENT. If I create ManyToMany relation how my user, who have many roles authentication? And in user profile I want create, if user have ROLE_TEAM - visible button for go to profile team, buit his many role, I need foreach array roles ?
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\UsersRepository")
* @ExclusionPolicy("all")
*/
class Users implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @Expose()
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Gedmo\Slug(fields={"firstName", "lastName"}, separator="-", updatable=true)
* @ORM\Column(name="name", type="string", options={"default": "Company"}, length=255, nullable=false)
* @Assert\Length(min=3, max=255)
*/
protected $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=80, nullable=true)
*/
protected $password;
/**
* @ORM\ManyToMany(targetEntity="Role")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*
* @var ArrayCollection $userRoles
*/
protected $userRoles;
/**
* Constructor
*/
public function __construct()
{
$this->userRoles = new ArrayCollection();
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return '';
}
/**
* @inheritDoc
*/
public function eraseCredentials() { }
/**
* Геттер для ролей пользователя.
*
* @return ArrayCollection A Doctrine ArrayCollection
*/
public function getUserRoles()
{
return $this->userRoles;
}
/**
* Геттер для массива ролей.
*
* @return array An array of Role objects
*/
public function getRoles()
{
return $this->getUserRoles()->toArray();
}
}
entity Role
/**
* @ORM\Entity
* @ORM\Table(name="role")
*/
class Role implements RoleInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var integer $id
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*
* @var string $name
*/
protected $name;
/**
* @ORM\Column(type="datetime", name="created_at")
*
* @var DateTime $createdAt
*/
protected $createdAt;
/**
* Геттер для id.
*
* @return integer The id.
*/
public function getId()
{
return $this->id;
}
/**
* Геттер для названия роли.
*
* @return string The name.
*/
public function getName()
{
return $this->name;
}
/**
* Сеттер для названия роли.
*
* @param string $value The name.
*/
public function setName($value)
{
$this->name = $value;
}
/**
* Геттер для даты создания роли.
*
* @return DateTime A DateTime object.
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Конструктор класса
*/
public function __construct()
{
$this->createdAt = new \DateTime();
}
/**
* Реализация метода, требуемого интерфейсом RoleInterface.
*
* @return string The role.
*/
public function getRole()
{
return $this->getName();
}
}
now I crate fixtures but have error:
Artel\ProfileBundle\Entity\Role:
role0:
name: 'ROLE_FREELANCER'
role1:
name: 'ROLE_COMPANY'
role2:
name: 'ROLE_DEVELOPER'
Artel\ProfileBundle\Entity\Users:
users0:
........
userRoles: @role0
users{1..100}:
.......
userRoles: 2x @role*
[Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections \ArrayCollection::__construct() must be of the type array, object given, called in /var/www/aog-code/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605 and defined