我正在使用classMedataData获取字段类型,使用下面的
$em->getClassMetadata('AcmeDemoBundle:' . $entityName)->getTypeOfField($fieldName)),
我想检查字段是否自动递增。我试过用这个
$em->getClassMetadata('AcmeDemoBundle:' . $entityName)->isIdentifier($fieldName)),
但它没有给出它是否是自动递增的?基本上我想要
generator: { strategy: AUTO }
实体名称中的元数据。
此信息存储在"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;
在回答@vishal关于自动增量使用哪种类型的问题时迟到了——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;
}