2
我想根據UUID和二進制(16)中的存儲製作主鍵。Doctrine2定製類型不能像主鍵一樣工作Symfony2
爲此我創建主義新類型 - 「二進制」
class BinaryType extends Type
{
const BINARY = 'binary';
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return sprintf('BINARY(%d)', $fieldDeclaration['length']);
}
public function getName()
{
return self::BINARY;
}
public function convertToPhpValue($value, AbstractPlatform $platform)
{
if ($value !== null) {
$value= unpack('H*', $value);
return array_shift($value);
}
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value !== null) {
return pack('H*', $value);
}
}
}
而且註冊此類型:
class MyBundle extends Bundle
{
public function boot()
{
Type::addType('binary', '...\Doctrine2\BinaryType');
}
}
問:爲什麼這種類型的工作在一個簡單的領域不錯,但沒有使用主鍵(註解@ORM \ Id的字段),字段沒有出現。
示例不工作註釋。在這種情況下,不會出現db中的任何一行:
/**
* @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;
工作註釋示例。我們看到從數據庫和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;