DavidKmenta / Doctrine-Simple-Array-Types
为 Doctrine 扩展的简单数组类型
1.1.0
2018-10-18 18:29 UTC
Requires
- php: >=7.1
- doctrine/orm: ^2.4
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-19 10:09:32 UTC
README
一个用于整数、浮点数和字符串简单数组的 Doctrine 字段类型。
描述
这些类型主要解决两个常见问题
- simple_string_array 解决了包含逗号符号的字符串问题。这种字符串不能持久化存储在 Doctrine 提供的 simple_array 类型中。
- simple_integer_array 和 simple_float_array 扩展了 Doctrine 的 simple_array 类型。这些类型解决了当从数据库返回持久化的整数或浮点数作为字符串时的问题。
安装
运行以下命令
composer require davidkmenta/doctrine-simple-array-types
示例
要配置 Doctrine 使用这些类型,你需要在你的引导文件中设置以下内容
<?php \Doctrine\DBAL\Types\Type::addType('simple_string_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleStringArrayType'); $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_string_array', 'simple_string_array'); \Doctrine\DBAL\Types\Type::addType('simple_integer_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleIntegerArrayType'); $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_integer_array', 'simple_integer_array'); \Doctrine\DBAL\Types\Type::addType('simple_float_array', 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleFloatArrayType'); $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('simple_float_array', 'simple_float_array');
或者,如果你使用的是 Symfony,在 Doctrine 配置文件中设置类型(例如:doctrine.yml)
doctrine: dbal: types: simple_string_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleStringArrayType' simple_integer_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleIntegerArrayType' simple_float_array: 'DavidKmenta\DoctrineSimpleArrayTypes\SimpleFloatArrayType'
然后,在你的实体中,你可以通过设置 @Column 类型为 simple_string_array、simple_integer_array 或 simple_float_array 来注解属性。