dcs/role-provider-orm-bundle

DCSRoleProviderORMBundle 提供使用 Doctrine ORM 管理用户角色的功能

dev-master / 1.0.x-dev 2016-12-31 09:33 UTC

This package is not auto-updated.

Last update: 2024-09-26 01:56:21 UTC


README

Build Status Coverage Status

DCSRoleProviderORMBundle

此包为 DCSRoleCoreBundle 提供 provider 实现。此 provider 基于 Doctrine ORM 构建,并对 Role 类进行映射。

为了通过您的用户类管理角色,此包提供了 UserRoleCollection trait。

安装

先决条件

此包需要 DCSRoleCoreBundle

要求包

运行以下命令

$ composer require dcs/role-provider-orm-bundle "~1.0@dev"

Composer 将将包安装到项目的 vendor/dcs/role-provider-orm-bundle 目录。

启用包

在 kernel 中启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
	$bundles = array(
		// ...
		new DCS\Role\Provider\ORMBundle\DCSRoleProviderORMBundle(),
		// ...
	);
}

创建您的 Role 类

您必须提供一个具体的 Role 类。您必须扩展抽象模型 DCS\Role\Provider\ORMBundle\Model\Role 并创建适当的映射。

注解
<?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>

更新您的 User 类

在本步骤中,您需要更新您的 User 类,使其与上一步骤中创建的 Role 类兼容。为了方便,我们将使用 UserRoleCollection trait。

注解
<?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>

配置

现在您已正确启用此包,下一步是配置它以与您应用程序的具体需求一起工作。

将以下配置添加到您的 config.yml

dcs_role_provider_orm:
    model_class: AcmeBundle\Entity\Role

以下行提供了对 DCSRoleCoreBundle 的配置。

dcs_role_core:
    provider: dcs_role.provider.orm

报告问题或功能请求

问题和功能请求在 Github issue tracker 中跟踪。