Как описать в doctrine orm id genarator при
Только изучаю doctrine-orm. Можно ли как то описать такую генерацию первичного ключа в postgresql?
create sequence requests.seq_uuid
increment by 1
minvalue 1
maxvalue 9223372036854775807
start 1
cache 1
no cycle;
create or replace function requests.uuid() returns uuid
as $$
begin
return ('0000000000000000' || lpad(to_hex(nextval('requests.seq_uuid')), 16, '0'))::uuid;
end;
$$ language plpgsql;
create table requests.request (
id uuid not null default requests.uuid(),
request text,
constraint request_pk primary key (id)
);
Ответы (1 шт):
Автор решения: lalua
→ Ссылка
Необходимо создать свой класс, унаследованный от AbstractIdGenerator:
<?php
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
class MyIdGenerator extends AbstractIdGenerator
{
public function generate(EntityManager $em, $entity)
{
return $em->getConnection()->executeQuery('select requests.uuid() as uuid;')->fetchOne();
}
}
И создать класс entity со следующим определением первичного ключа:
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="requests.request")
*/
class Request
{
/**
* @ORM\Id
* @ORM\Column(type="guid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="MyIdGenerator")
*/
private $id;
...