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

时间:2013-07-19 08:02:34

标签: oracle doctrine-orm doctrine

我在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为空(在deffinition中没有序列!

编辑:以下是实体: 客户实体:

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

联系信息实体:

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

1 个答案:

答案 0 :(得分:0)

Oracle不支持自动递增,因此您无法在Doctrine中使用“IDENTITY”策略。你必须使用“SEQUENCE”(或“自动”)策略。

当指定“AUTO”时,Doctrine将为MySql使用“IDENTITY”,为Oracle使用“SEQUENCE”。

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