简体   繁体   English

Doctrine2 + Symfony2序列ID表

[英]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 我正在尝试从vtiger_crmentity_seq检索id值,以更新vtiger_crmentity.crmid如果不是auto_increment

So this is my entity vtiger_crmentity 这是我的实体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. 但是,当我尝试提交表单时,Symfony2返回此错误。

Operation 'Doctrine\\DBAL\\Platforms\\AbstractPlatform::getSequenceNextValSQL' is not supported by platform. 平台不支持“ Doctrine \\ DBAL \\ Platforms \\ AbstractPlatform :: getSequenceNextValSQL”操作。 500 Internal Server Error - DBALException 500内部服务器错误-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... 我无法将vtiger_crmentity.crmid更改为auto_increment因此这个想法难以为继,我还需要将vtiger_crmentity_seq.id更新为显然使用的最新值...

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. 这段代码在sql中调用了一些本机查询,但实际上您可以在自定义生成器中执行所有操作。

<?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;
...

Doctrine does not support using the SEQUENCE strategy for GeneratedValue when using MySQL. 使用MySQL时,Doctrine不支持将SEQUENCE策略用于GeneratedValue If you cannot set the column to auto_increment , you'll have to generate the IDs in your own code. 如果无法将列设置为auto_increment ,则必须在自己的代码中生成ID。 See here . 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM