如何使用doctrine2检查symfony2中是否自动增量字段?

时间:2013-08-30 10:36:39

标签: php symfony doctrine-orm

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

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

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

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

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

   generator: { strategy: AUTO }

实体名称中的元数据。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;
}