[英]How add BLOB type in Doctrine 2 using Symfony 2
在Symfony 2中,我生成了一個Bundle,用於將任何類型的文檔存儲到數據庫中,但是我需要BLOB列類型。
在這個問題上,我將類BlobType添加到Doctrine DBAL中,但是要使用新的列類型,我必須進行更改
原則\\ DBAL \\類型\\類型
[...]
const BLOB = 'blob';
[...]
private static $_typesMap = array(
[...],
self::BLOB => 'Doctrine\DBAL\Types\BlobType',
);
Doctrine \\ DBAL \\ Platforms \\ MySqlPlatform (如果我更改了Doctrine \\ DBAL \\ Platforms \\ AbstractPlatform,可能會更好)
[...]
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,但我不知道如何進行干預。
這為我工作:
創建您的Blob類型(請參閱https://gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58 )
將此添加到您的捆綁包初始化中(/src/YOURDOMAIN/YOURBUNDLE/YOURDOMAINYOUBUNDLE.php)
class YourBundle extends Bundle { public function boot() { $em = $this->container->get('doctrine.orm.entity_manager'); Type::addType('blob', 'YOURDOMAIN\\YOURBUNDLE\\YOURTYPEDIRECTORY\\BlobType'); $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob'); } }
對XXXBundle :: boot()中的注冊Blob類型進行小的改進,但在單元測試期間可能有必要。
class XXXBundle extends Bundle
{
public function boot()
{
// Add blob type
if(!Type::hasType('blob')) {
Type::addType('blob', '{CLASS_PATH}\\Blob');
}
// Add blob type to current connection.
// Notice: during tests there can be multiple connections to db so
// it will be needed to add 'blob' to all new connections if not defined.
$em = $this->container->get('doctrine.orm.entity_manager');
if (!$em->getConnection()->getDatabasePlatform()->hasDoctrineTypeMappingFor('blob')) {
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('blob','blob');
}
}
我剛剛發現了這個要點: https : //gist.github.com/525030/38a0dd6a70e58f39e964ec53c746457dd37a5f58
app / bootstrap.php:
<?php
// ...
$em = Doctrine\ORM\EntityManager::create($conn, $config, $evm);
// types registration
Doctrine\DBAL\Types\Type::addType('blob', 'Doctrine\DBAL\Types\Blob');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('BLOB', 'blob');
BTW bootstrap.cache.php是自動生成的AFAIK。因此更改將被覆蓋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.