2

我正在使用下面的 classMedataData 获取字段类型,

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

我想检查该字段是否为自动增量。我试过用这个,

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

但它没有给出它是否是自动增量?基本上我想要

   generator: { strategy: AUTO }

来自实体名称的元数据。

4

2 回答 2

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;
于 2013-08-30T12:37:56.927 回答
0

迟到回答@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;
}
于 2017-07-07T15:49:03.780 回答