, (*1)
DCSRoleProviderORMBundle
This bundle provides the provider implementation for DCSRoleCoreBundle.
This provider is built in on Doctrine ORM and performs a mapping of the Role class., (*2)
To manage the roles through your User class this bundle provides the UserRoleCollection trait., (*3)
Installation
Prerequisites
This bundle requires DCSRoleCoreBundle., (*4)
Require the bundle
Run the following command:, (*5)
$ composer require dcs/role-provider-orm-bundle "~1.0@dev"
Composer will install the bundle to your project's vendor/dcs/role-provider-orm-bundle
directory., (*6)
Enable the bundle
Enable the bundle in the kernel:, (*7)
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new DCS\Role\Provider\ORMBundle\DCSRoleProviderORMBundle(),
// ...
);
}
Create your Role class
You must provide a concrete Role class. You must extend the abstract model DCS\Role\Provider\ORMBundle\Model\Role
and create the appropriate mapping., (*8)
Annotations
<?php
// src/AcmeBundle/Entity/Role.php
namespace AcmeBundle\Entity;
use DCS\Role\Provider\ORMBundle\Model\Role as RoleBase;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="role")
*/
class Role extends RoleBase
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
Yaml
# src/AcmeBundle/Resources/config/doctrine/Role.orm.yml
AcmeBundle\Entity\Role:
type: entity
table: role
id:
id:
type: integer
generator:
strategy: AUTO
Xml
<?xml version="1.0" encoding="utf-8"?>
<!-- src/AcmeBundle/Resources/config/doctrine/Role.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AcmeBundle\Entity\Role" table="role">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
</entity>
</doctrine-mapping>
Update your User class
In this step you'll need to update your User class to make it compatible with the Role class created in the previous step. For convenience, we will use the UserRoleCollection trait., (*9)
Annotations
<?php
// src/AcmeBundle/Entity/User.php
namespace AcmeBundle\Entity;
use DCS\User\CoreBundle\Model\User as UserBase;
use DCS\Role\Provider\ORMBundle\Model\UserRoleCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* ... your mapping entity
*/
class User extends UserBase
{
use UserRoleCollection;
//... other mapping fields
/**
* @ORM\ManyToMany(targetEntity="AcmeBundle\Entity\Role")
* @ORM\JoinTable(name="user_role",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* }
* )
*/
protected $roles;
}
Yaml
# src/AcmeBundle/Resources/config/doctrine/User.orm.yml
AcmeBundle\Entity\User:
//... your mapping entity
manyToMany:
roles:
targetEntity: AcmeBundle\Entity\Role
joinTable:
name: user_role
joinColumns:
user_id:
referencedColumnName: id
inverseJoinColumns:
role_id:
referencedColumnName: id
Xml
<?xml version="1.0" encoding="utf-8"?>
<!-- src/AcmeBundle/Resources/config/doctrine/User.orm.xml -->
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AcmeBundle\Entity\User">
<!-- your mapping entity -->
<many-to-many field="roles" target-entity="AcmeBundle\Entity\Role">
<join-table name="user_role">
<join-columns>
<join-column name="user_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column name="role_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>
Now that you have properly enabled this bundle, the next step is to configure it to work with the specific needs of your application., (*10)
Add the following configuration to your config.yml
., (*11)
dcs_role_provider_orm:
model_class: AcmeBundle\Entity\Role
The following lines provide the configuration for the DCSRoleCoreBundle., (*12)
dcs_role_core:
provider: dcs_role.provider.orm
Reporting an issue or a feature request
Issues and feature requests are tracked in the Github issue tracker., (*13)