Doctrine 2: cascade persist Oracle quot;IDENTITYquot; is returning 0 as last inserted ID(原则 2:级联持久化 Oracle “IDENTITY返回 0 作为最后插入的 ID)
问题描述
我在 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
这篇关于原则 2:级联持久化 Oracle “IDENTITY"返回 0 作为最后插入的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:原则 2:级联持久化 Oracle “IDENTITY"返回 0 作为最后插入的 ID
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
