martin187/doctrine-set-type

为Doctrine设置类型

1.1 2022-11-11 09:24 UTC

This package is auto-updated.

Last update: 2024-09-11 13:51:25 UTC


README

Doctrine Logo

Doctrine设置类型

安装

推荐的安装方法是使用Composer

composer require martin187/doctrine-set-type

使用示例

<?php

namespace Acme\Types;


use Doctrine\DBAL\Types\SetType;

class RolesType extends SetType {

    const NAME = 'roles_type';

    const ROLE_SUPER_USER_VALUE = 'ROLE_SUPER_USER';
    const ROLE_ADMIN_VALUE = 'ROLE_ADMIN';
    const ROLE_USER_VALUE = 'ROLE_USER';
    const ROLE_NONE_VALUE = 'ROLE_NONE';


    protected function getValue() {
        return array(
            self::ROLE_SUPER_USER_VALUE,
            self::ROLE_ADMIN_VALUE,
            self::ROLE_USER_VALUE,
            self::ROLE_NONE_VALUE
        );
    }


    public function getName() {
        return self::NAME;
    }
}
示例使用实体
<?php

namespace Acme\Entities;


use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 *
 * @ORM\Table(
 *     name = "users"
 * )
 */
class User {

    /**
     * @ORM\Column(
     *     type = "roles_type"
     * )
     */
    private $roles;
}

警告

不要忘记注册类型

\Doctrine\DBAL\Types\TypeType::addType(RolesType::NAME, RolesType::class);

/** @var \Doctrine\DBAL\Connection $conn */
$conn->getDatabasePlatform()->registerDoctrineTypeMapping('roles', RolesType::NAME);

许可证

此包采用MIT许可证。完整的许可证在文件此处