Robert W. Hunter
Robert W. Hunter

Reputation: 3003

Doctrine2 + Symfony2 Sequence ID Table

I'm trying to retrieve the id value from vtiger_crmentity_seq in order to update vtiger_crmentity.crmid becase it isn't auto_increment

So this is my entity vtiger_crmentity

/**
 * @var integer
 *
 * @ORM\Column(name="crmid", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="SEQUENCE")
 * @ORM\SequenceGenerator(sequenceName="vtiger_crmentity_seq", allocationSize=1, initialValue=1)
 */
private $crmid;

/**
 * Get crmid
 *
 * @return integer 
 */
public function getCrmid()
{
    return $this->crmid;
}

But when I try to submit my form, Symfony2 returns me this error.

Operation 'Doctrine\DBAL\Platforms\AbstractPlatform::getSequenceNextValSQL' is not supported by platform. 500 Internal Server Error - DBALException

I cannot change vtiger_crmentity.crmid to auto_increment so this idea is discarted, also I need to update vtiger_crmentity_seq.id to the latest value used obviously...

Upvotes: 1

Views: 7320

Answers (2)

Marino Di Clemente
Marino Di Clemente

Reputation: 3190

you can try with a custom generator, with custom generator you can solve any primary key generation problem.

this code calls some native query in sql, but you can do really everything in your custom generator.

<?php

namespace MY\Doctrine;

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

class CustomGenerator extends AbstractIdGenerator
{
    static $cache = array();

    public function generate(EntityManager $em, $entity)
    {
        $id = $em->createNativeQuery(
                'SELECT crmid FROM vtiger_crmentity_seq WHERE used = 0 LIMIT 1',
                new Query\ResultSetMapping()
            )->getResult(Query::HYDRATE_SINGLE_SCALAR);
        $em->getConnection()->executeUpdate(
            'UPDATE vtiger_crmentity_seq SET used = 1 WHERE crmid = ?', 
             array($id)
        )

        return $id;
    }
}

and in your entity

...
class AbstractProduct
{
    /**
     * @var integer
     * @ORM\Id
     * @ORM\Column(name="ID", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="\My\CustomGenerator")
     */
    protected $id;
...

Upvotes: 5

Ulrich Schmidt-Goertz
Ulrich Schmidt-Goertz

Reputation: 1116

Doctrine does not support using the SEQUENCE strategy for GeneratedValue when using MySQL. If you cannot set the column to auto_increment, you'll have to generate the IDs in your own code. See here.

Upvotes: 4

Related Questions