Creating the next sequence value manually in Doctrine 2

What would be the easiest way to generate nextval for a specific sequence with a given name?

Solution annotation indicating

  * @ORM\GeneratedValue(strategy="SEQUENCE") * @ORM\SequenceGenerator(sequenceName="sq_foobar", allocationSize="1", initialValue="1") 

it doesn’t satisfy me if there is more complicated logic: in some cases I need to get nextval , in another I would go with a value obtained from other sources (and not for the sequence).

So, I hope there is a way to get the nextval sequence manually in the entity constructor.

+12
php doctrine2
Jan 31 2018-12-12T00:
source share
3 answers

Then I think you should implement your own identifier generator.

The easiest way is to override the Doctrine \ ORM \ Id \ SequenceGenerator class to handle your specific case.

Then you need to register this generator in the class metadata using the Doctrine ORM API.

Some links: http://ranskills.wordpress.com/2011/05/26/how-to-add-a-custom-id-generation-strategy-to-doctrine-2-1/

https://github.com/doctrine/doctrine2/pull/206

+4
Feb 03 2018-12-12T00:
source share

Just in case, someone else attacks this question (like me):
The @Florian stretch request mentioned has turned it into a doctrine. Although the documentation still does not contain any information on the CUSTOM identifier generation strategy. Only the part I found where the CUSTOM parameter for IdGenerator is specified is in the GeneratedValue description. If I missed this, please correct me in the comments.

It’s hard to implement it. Just create a class extending Doctrine\ORM\Id\AbstractIdGenerator\AbstractIdGenerator :

 namespace My\Namespace; use Doctrine\ORM\Id\AbstractIdGenerator; class MyIdGenerator extends AbstractIdGenerator { public function generate(\Doctrine\ORM\EntityManager $em, $entity) { // Create id here $id = <do some logic>; return $id; } } 

Then add it to the id description in the doctrine entity configuration ( YAML example):

 My\Bundle\Entity\MyEntity: type: entity id: id: type: bigint unique: true generator: strategy: CUSTOM customIdGenerator: class: 'My\Namespace\MyIdGenerator' fields: otherField: .... 

If you use Annotations instead of YAML, the configuration of the object should look like this (unchecked):

 /** * @Id * @Column(type="integer") * @GeneratedValue(strategy="CUSTOM") * @CustomIdGenerator(class="My\Namespace\MyIdGenerator") */ public $id; 

And all this;)

+36
Feb 17 '15 at 12:00
source share

There are two ways to get the nextval sequence in Doctrine2:

  • Using Doctrine ORM SequenceGenerator

     use Doctrine\ORM\Id\SequenceGenerator; $sequenceName = 'file_id_seq'; $sequenceGenerator = new SequenceGenerator($sequenceName, 1); $newId = $sequenceGenerator->generate($entityManager, $entity); // $entity in this case is actually not used in generate() method, so you can give any empty object, or if you are not worried about editor/IDE warnings, you can also specify null 
  • Use native SQL

     $sequenceName = 'file_id_seq'; $dbConnection = $entityManager->getConnection(); $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL($sequenceName); // $nextvalQuery is now following string "SELECT NEXTVAL('file_id_seq')" $newId = (int)$dbConnection->fetchColumn($nextvalQuery); 
+16
Jul 17 '12 at 11:35
source share



All Articles