Doctrine 2:级联持续Oracle“IDENTITY”正在返回0作为最后插入的ID [英] Doctrine 2: cascade persist Oracle "IDENTITY" is returning 0 as last inserted ID

查看:117
本文介绍了Doctrine 2:级联持续Oracle“IDENTITY”正在返回0作为最后插入的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用oracle的doctrine 2,数据库中的表有一些生成ID的触发器,我的表的ID映射如下:

  / ** 
* @ orm\Id
* @ orm\Column(type =integer);
* @ orm\GeneratedValue(strategy =IDENTITY)
* /
protected $ id;

我有一个OneToMany关系, cascade = {persist} 但它不工作,我尝试与MySQL相同的代码,它的工作正常,但在oracle最后一个插入ID似乎总是返回0而不是插入的行的真正的id ...所以级联持续不起作用...这是教条中的错误还是我做错了?任何帮助?



按照代码,似乎方法



Doctrine\\ \\ ORM\Id\IdentityGenerator :: generate



正在返回0,我不知道为什么它被调用,因为 sequenceName 是null(在deffinition中没有序列!



编辑:这里是实体: / strong>
客户端实体:

  / ** 
* @ ORM\Entity
* @ ORM\Table(name =clients)
** /
class Client {
/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* @ ORM\Column(type =integer)
* /
protected $ id;

/ ** @ ORM\Column(name =name,type =string,length = 255,unique = true)* /
protected $ name;

/ **
* @ ORM\OneToMany(targetEntity =ContactInformation,mappedBy =client,cascade = {persist})
** /
protected $ cont actInformations;

公共函数__construct(){
$ this-> contactInformations = new ArrayCollection();
}

public function getId(){
return $ this-> id;
}

public function getName(){
return $ this-> name;
}

public function setName($ name){
$ this-> name = $ name;
return $ this;
}

public function getContactInformations(){
return $ this-> contactInformations;
}

public function addContactInformations(Collection $ contactInformations)
{
foreach($ contactInformations as $ contactInformation){
$ contactInformation-> setClient $本);
$ this-> contactInformations-> add($ contactInformation);
}
}

/ **
* @param Collection $ tags
* /
public function removeContactInformations(Collection $ contactInformations)
{
foreach($ contactInformations as $ contactInformation){
$ contactInformation-> setClient(null);
$ this-> contactInformations-> removeElement($ contactInformation);
}
}

public function setContactInformations($ contactInformations){
$ this-> contactInformations = $ contactInformations;
return $ this;
}
}

联系信息实体:

  / ** 
* @ ORM\Entity
* @ ORM\Table(name =contact_informations)
** /
class ContactInformation {
/ **
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
* @ ORM\Column(type =integer)
* /
protected $ id;

/ **
* @ ORM\OneToOne(targetEntity =ContactInformationType)
* @ ORM\JoinColumn(name =type_id,referencedColumnName =id )
** /
protected $ type;

/ ** @ ORM\Column(type =text)* /
protected $ value;

/ **
* @ ORM\ManyToOne(targetEntity =Client,inversedBy =contact_informations)
* @ ORM\JoinColumn(name =client_id ,referencedColumnName =id)
** /
private $ client;

public function getId(){
return $ this-> id;
}

public function getType(){
return $ this-> type;
}

public function setType($ type){
$ this-> type = $ type;
return $ this;
}

public function getValue(){
return $ this-> value;
}

public function setValue($ value){
$ this-> value = $ value;
return $ this;
}

public function getClient(){
return $ this-> client;
}

public function setClient($ client = null){
$ this-> client = $ client;
return $ this;
}
}


解决方案

Oracle不支持自动递增,所以你不能在Doctrine中使用IDENTITY策略。您必须使用SEQUENCE(或AUTO)策略。



当指定AUTO时,Doctrine将对MySql和SEQUENCE使用IDENTITY for Oracle。



http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-策略


I am using doctrine 2 with oracle, the tables in the database has some triggers that generate the IDs, and my ID mapping of my tables is like the following:

/**
 * @orm\Id
 * @orm\Column(type="integer");
 * @orm\GeneratedValue(strategy="IDENTITY")
 */
protected $id;

and I have a OneToMany relation, with cascade={"persist"} but it is not working, I tried the same code with MySQL and it is working fine, but in oracle the last insert Id seems to always return 0 instead of the real id of the inserted row... and so the cascade persist is not working... is this a bug in doctrine or am I doing something wrong? any help?

After following the code it seems that the method

Doctrine\ORM\Id\IdentityGenerator::generate

is returning 0, I don't know why it is being invoked since the sequenceName is null (there is no sequence in the deffinition!

EDIT: Here are the entities: The Client Entity:

/** 
 * @ORM\Entity 
 * @ORM\Table(name="clients")
 **/
class Client {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** @ORM\Column(name="name",type="string",length=255,unique=true) */
    protected $name;

    /**
    * @ORM\OneToMany(targetEntity="ContactInformation", mappedBy="client", cascade={"persist"})
    **/
    protected $contactInformations;

    public function __construct() {
        $this->contactInformations = new ArrayCollection();
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function getContactInformations() {
        return $this->contactInformations;
    }

    public function addContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient($this);
            $this->contactInformations->add($contactInformation);
        }
    }

    /**
     * @param Collection $tags
     */
    public function removeContactInformations(Collection $contactInformations)
    {
        foreach ($contactInformations as $contactInformation) {
            $contactInformation->setClient(null);
            $this->contactInformations->removeElement($contactInformation);
        }
    }

    public function setContactInformations($contactInformations) {
        $this->contactInformations = $contactInformations;
        return $this;
    }
}

The Contact Information Entity:

/** 
 * @ORM\Entity 
 * @ORM\Table(name="contact_informations")
 **/
class ContactInformation {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\OneToOne(targetEntity="ContactInformationType")
     * @ORM\JoinColumn(name="type_id", referencedColumnName="id")
     **/
    protected $type;

    /** @ORM\Column(type="text") */
    protected $value;

    /**
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="contact_informations")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     **/
    private $client;

    public function getId() {
        return $this->id;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
        return $this;
    }

    public function getValue() {
        return $this->value;
    }

    public function setValue($value) {
        $this->value = $value;
        return $this;
    }

    public function getClient() {
        return $this->client;
    }

    public function setClient($client = null) {
        $this->client = $client;
        return $this;
    }
}

解决方案

Oracle doesn't support auto incrementing, so you cannot use the "IDENTITY" strategy in Doctrine. You'll have to use the "SEQUENCE" (or "AUTO") strategy.

When specifying "AUTO", Doctrine will use "IDENTITY" for MySql and "SEQUENCE" for Oracle.

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#identifier-generation-strategies

这篇关于Doctrine 2:级联持续Oracle“IDENTITY”正在返回0作为最后插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆