【问题标题】:Add TINYINT to Doctrine SQL types将 TINYINT 添加到 Doctrine SQL 类型
【发布时间】:2021-02-12 03:56:12
【问题描述】:

按照 Symfony 文档,我尝试将 TINYINT 添加为实体列类型。

到目前为止它运行良好,但还有两个问题......

  1. 每次我想执行迁移时,Doctrine 都无法为关联的列重新调整TINYINT,并再次执行迁移查询。

  2. 在表单构建器中,默认情况下 TINYINT 被重新转换为 TextType 而不是 NumberType

你知道我在解决这两个问题时缺少什么吗?

TinyintType.php

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

class TinyintType extends Type {
    const TINYINT='tinyint';

    /**
     * @return string
     */
    public function getName() {
        return self::TINYINT;
    }

    /**
     * @param array $fieldDeclaration
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
        return $fieldDeclaration['unsigned'] === true ? 'TINYINT(1) UNSIGNED' : 'TINYINT(1)';
    }

    public function canRequireSQLConversion() {
        return true;
    }

    /**
     * @param $value
     * @param AbstractPlatform $platform
     * @return int|null
     */
    public function convertToPHPValue($value, AbstractPlatform $platform) {
        return $value === null ? null : (int)$value;
    }

    /**
     * @param mixed $value
     * @param AbstractPlatform $platform
     * @return int|mixed|null
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        return $value === null ? null : (int)$value;
    }

    /**
     * @return int
     */
    public function getBindingType() {
        return ParameterType::INTEGER;
    }
}

doctrine.yaml

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        server_version: '5.7'
        types:
            tinyint: 'App\Doctrine\DBAL\Types\TinyintType'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

【问题讨论】:

  • 根据this,可能会有一个额外的mapping_types 用于可能有助于Doctrine 的迁移。
  • 我试过这个,但学说会继续生成相同的迁移文件,就好像数据库类型没有改变一样@ChrisHaas

标签: php symfony doctrine dbal symfony-4.4


【解决方案1】:

第一期:来自https://blog.vandenbrand.org/2015/06/25/creating-a-custom-doctrine-dbal-type-the-right-way/

解决方法是在字段中添加注释以存储元数据 在。这似乎在文档中丢失,但我发现了一些 JIRA 描述该功能的问题。我们必须改变我们的列定义 所以该类型的元数据不会丢失

所以你的 getSQLDeclaration 应该是这样的:

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
    return 'TINYINT'.(!empty($fieldDeclaration['unsigned']) ? ' UNSIGNED' : '').' COMMENT \'(DC2Type:tinyint)\'';
}

第二个问题:这是因为默认情况下 symfony 表单使用文本类型(symfony/form/FormBuilder.php::create

if (null === $type && null === $this->getDataClass()) {
    $type = 'Symfony\Component\Form\Extension\Core\Type\TextType';
}

如果你想设置其他类型,你应该明确设置你的类型。

【讨论】:

  • 我个人无法确认评论是否有效,但我看到它存在于代码库中,带有actual method for parsing,并且有一个thread here,所以这似乎是官方的虽然没有记录的方式来做到这一点!
  • 我确实记得与数组类型类似的事情,我会尽快尝试,但对我来说似乎是正确的。谢谢
  • 工作,现在检测到迁移就好了。谢谢!
猜你喜欢
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 2021-10-18
  • 2020-04-21
相关资源
最近更新 更多