Doctrine2自定义类型与主键Symfony2不一样

Doctrine2自定义类型与主键Symfony2不一样,symfony,orm,doctrine-orm,doctrine,Symfony,Orm,Doctrine Orm,Doctrine,我想根据UUID和二进制存储(16)制作主键 为此,我创建了新的学说类型——“二进制” 也要注册此类型: class MyBundle extends Bundle { public function boot() { Type::addType('binary', '...\Doctrine2\BinaryType'); } } 问题:为什么该类型在简单字段中工作良好,但不使用主键(注释为@ORM\Id的字段),字段只是不出现 示例注释无效。在这种情况下,数据库中不显示

我想根据UUID和二进制存储(16)制作主键

为此,我创建了新的学说类型——“二进制”

也要注册此类型:

class MyBundle extends Bundle
{
  public function boot()
  {
     Type::addType('binary', '...\Doctrine2\BinaryType');
  }
}
问题:为什么该类型在简单字段中工作良好,但不使用主键(注释为@ORM\Id的字段),字段只是不出现

示例注释无效。在这种情况下,数据库中不显示任何行:

/**
 * @ORM\Id
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 *
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $id;

 /**
 * 
 * @ORM\Column(name="second_id", type="integer", nullable=false)
 */
private $secondId;
工作注释的示例。我们看到db和id中的行是二进制类型:

/**
 * 
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $id;

 /**
 * @ORM\Id
 * @ORM\Column(name="second_id", type="integer", nullable=false)
 */
private $secondId;

我在这个问题上花了好几个小时,因为我也需要这样做。最后,我对您的代码做了一个小小的修改:省略@ORM/GeneratedValue(strategy=“NONE”)

换句话说,如果你改变这个

/**
 * @ORM\Id
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 *
 * @ORM\GeneratedValue(strategy="NONE")
 */
private $id;
对此

/**
 * @ORM\Id
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 */
private $id;
这对我有用

还有一件事,如果您想生成id,您必须实现自己的生成器 比如:

并将注释更改为

/**
 * @ORM\Id
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 * @ORM\GeneratedValue(strategy="CUSTOM") 
 * @ORM\CustomIdGenerator(class="path\to\IDGenerators\GuidGenerator") 
 */
private $id;
我知道你可能已经离开了,但只是把这个贴给下一个人

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;

class GuidGenerator extends AbstractIdGenerator
{
    public function generate(EntityManager $em, $entity)
    {
        //return generated id
    }
}
/**
 * @ORM\Id
 * @ORM\Column(type="binary", length=16, name="id", nullable=false)
 * @ORM\GeneratedValue(strategy="CUSTOM") 
 * @ORM\CustomIdGenerator(class="path\to\IDGenerators\GuidGenerator") 
 */
private $id;