如何使用 Symfony 2 在 Doctrine 2 中添加 BLOB 类型 [英] How add BLOB type in Doctrine 2 using Symfony 2

查看:27
本文介绍了如何使用 Symfony 2 在 Doctrine 2 中添加 BLOB 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Symfony 2 中,我生成了一个 Bundle,用于将任何类型的文档存储到数据库中,但我需要 BLOB 列类型.

In Symfony 2 I generate a Bundle for storing any type of document into database, but I need the BLOB column type.

Tnx 到 this question 我将 BlobType 类添加到 Doctrine DBAL 中,但为了使用新的列类型,我必须更改

Tnx to this question I add the class BlobType into Doctrine DBAL, but for use the new column type I had to change

DoctrineDBALTypesType

[...]

const BLOB = 'blob';

[...]

private static $_typesMap = array(
    [...],
    self::BLOB => 'DoctrineDBALTypesBlobType',
);

DoctrineDBALPlatformsMySqlPlatform(如果我改变了 DoctrineDBALPlatformsAbstractPlatform 可能会更好)

DoctrineDBALPlatformsMySqlPlatform (maybe it was better if I had changed DoctrineDBALPlatformsAbstractPlatform)

[...]
protected function initializeDoctrineTypeMappings()
{
    $this->doctrineTypeMapping = array(
        [...],
        'blob'          => 'blob',
    );
}

[...]

/**
 * Obtain DBMS specific SQL to be used to create time fields in statements
 * like CREATE TABLE.
 *
 * @param array $fieldDeclaration
 * @return string
 */
public function getBlobTypeDeclarationSQL(array $fieldDeclaration) 
{
    return 'BLOB';
}   

现在我没有太多时间来'漂亮的解决方案',但将来我想恢复 Doctrine 类并能够将新的列类型分配到 Symfony 2 引导程序中.我想我应该编辑我的 app/bootstrap.php.cache 但我不知道如何进行干预.

Now I don't have mouch time for a 'pretty solution', but in future I would like to restore the Doctrine classes and be able to assign the new column type into Symfony 2 bootstrap. I think I should edit my app/bootstrap.php.cache but I don't have idea how to intervene.

推荐答案

这对我有用:

  1. 创建您的 blobtype(请参见 https://gist.github.com/525030/6a7f37d95c535c54d95e37e37e38e964e58f39e964c/a>)

  1. create your blobtype (See https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58)

将此添加到您的 Bundle 初始化 (/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php)

add this to your Bundle initialization (/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php)

class YourBundle extends Bundle
{
    public function boot()
    {
        $em = $this->container->get('doctrine.orm.entity_manager');
        Type::addType('blob', 'YOURDOMAINYOURBUNDLEYOURTYPEDIRECTORYBlobType');
        $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');        
    }
}

这篇关于如何使用 Symfony 2 在 Doctrine 2 中添加 BLOB 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆