原则 2:级联持久化 Oracle “IDENTITY"返回 0 作为最后插入的 ID

2022-01-16 00:00:00 doctrine oracle doctrine-orm

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

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:

/**
 * @ormId
 * @ormColumn(type="integer");
 * @ormGeneratedValue(strategy="IDENTITY")
 */
protected $id;

我有一个 OneToMany 关系,与 cascade={"persist"} 但它不工作,我用 MySQL 尝试了相同的代码,它工作正常,但在 oracle 中最后插入id 似乎总是返回 0 而不是插入行的真实 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?

按照代码看起来方法

DoctrineORMIdIdentityGenerator::generate

返回 0,我不知道为什么调用它,因为 sequenceName 为空(定义中没有序列!

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

以下是实体:客户实体:

/** 
 * @ORMEntity 
 * @ORMTable(name="clients")
 **/
class Client {
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     * @ORMColumn(type="integer")
     */
    protected $id;

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

    /**
    * @ORMOneToMany(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;
    }
}

联系信息实体:

/** 
 * @ORMEntity 
 * @ORMTable(name="contact_informations")
 **/
class ContactInformation {
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     * @ORMColumn(type="integer")
     */
    protected $id;

    /**
     * @ORMOneToOne(targetEntity="ContactInformationType")
     * @ORMJoinColumn(name="type_id", referencedColumnName="id")
     **/
    protected $type;

    /** @ORMColumn(type="text") */
    protected $value;

    /**
     * @ORMManyToOne(targetEntity="Client", inversedBy="contact_informations")
     * @ORMJoinColumn(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")策略.

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.

当指定AUTO"时,Doctrine 将为 MySql 使用IDENTITY",为 Oracle 使用SEQUENCE".

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

相关文章