2013-08-30 26 views
2

我正在使用classMedataData使用以下字段的類型,如何在symfony2中使用doctrine2檢查字段是否爲自動增量?

$em->getClassMetadata('AcmeDemoBundle:' . $entityName)->getTypeOfField($fieldName)), 

我要檢查該字段爲自動增量與否。我試過用這個,

$em->getClassMetadata('AcmeDemoBundle:' . $entityName)->isIdentifier($fieldName)), 

但它不給它是否是自動增量?基本上我想要

generator: { strategy: AUTO } 

來自實體名稱的元數據。

回答

2

此信息存儲在 「generatorType」 公共MetadataInfo類屬性 爲了得到它,使用:

$em->getClassMetadata('AcmeDemoBundleBundle:'.$entityName)->generatorType; 

generator_type常量的定義是:

const GENERATOR_TYPE_AUTO = 1; 
const GENERATOR_TYPE_SEQUENCE = 2; 
const GENERATOR_TYPE_TABLE = 3; 
const GENERATOR_TYPE_IDENTITY = 4; 
const GENERATOR_TYPE_NONE = 5; 
const GENERATOR_TYPE_UUID = 6; 
const GENERATOR_TYPE_CUSTOM = 7; 
+0

感謝,該值與自動增量primory鍵ID列?看起來很明顯的GENERATOR_TYPE_AUTO,但我得到GENERATOR_TYPE_IDENTITY實體有發電機:{strategy:AUTO} – vishal

+0

我正在嘗試$ em-> getMetadataFactory() - > getMetadataFor('AcmeDemoBundle:'。$ entityname) - > idGenerator – vishal

0

晚回答到@維沙爾的問題,關於什麼類型用於自動增量 - GENERATOR_TYPE_IDENTITY實際上是正確的(見下文)。從Symfony的V3.3代碼

摘錄,ClassMetaDataInfo.php

/* The Id generator types. */ 
/** 
* AUTO means the generator type will depend on what the used platform prefers. 
* Offers full portability. 
*/ 
const GENERATOR_TYPE_AUTO = 1; 

/** 
* SEQUENCE means a separate sequence object will be used. Platforms that do 
* not have native sequence support may emulate it. Full portability is currently 
* not guaranteed. 
*/ 
const GENERATOR_TYPE_SEQUENCE = 2; 

/** 
* TABLE means a separate table is used for id generation. 
* Offers full portability. 
*/ 
const GENERATOR_TYPE_TABLE = 3; 

/** 
* IDENTITY means an identity column is used for id generation. The database 
* will fill in the id column on insertion. Platforms that do not support 
* native identity columns may emulate them. Full portability is currently 
* not guaranteed. 
*/ 
const GENERATOR_TYPE_IDENTITY = 4; 

/** 
* NONE means the class does not have a generated id. That means the class 
* must have a natural, manually assigned id. 
*/ 
const GENERATOR_TYPE_NONE = 5; 

/** 
* UUID means that a UUID/GUID expression is used for id generation. Full 
* portability is currently not guaranteed. 
*/ 
const GENERATOR_TYPE_UUID = 6; 

/** 
* CUSTOM means that customer will use own ID generator that supposedly work 
*/ 
const GENERATOR_TYPE_CUSTOM = 7; 

所以,你可以使用一些功能是這樣的:

public function isPrimaryKeyAutoGenerated(): bool 
{ 
    return $this->classMetaData && $this->classMetaData->generatorType == ClassMetadataInfo::GENERATOR_TYPE_IDENTITY; 
} 
相關問題