2013-07-19 41 views
0

我在oracle中使用doctrine 2,數據庫中的表有一些生成ID的觸發器,我的表的ID映射像以下:Doctrine 2:cascade persist Oracle「IDENTITY」返回0作爲最後插入的ID

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

,我有一個一對多的關係,與cascade={"persist"}但它不工作,我試圖與MySQL相同的代碼,它工作正常,但在甲骨文的最後插入標識似乎總是返回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; 
    } 
} 
+0

你能告訴我們兩個實體的代碼(特別是映射)嗎? –

+0

@Jasper我只是在問題中添加了實體... –

回答

0

Oracle不支持自動遞增,所以你不能在原則使用「身份」的策略。您必須使用「SEQUENCE」(或「AUTO」)策略。

指定「AUTO」時,Doctrine將對MySql使用「IDENTITY」,對Oracle使用「SEQUENCE」。

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

+0

我知道,但我使用觸發器來自動生成ID,如果我使用「AUTO」,那麼Doc​​trine將嘗試找到一個序列來獲取ID值,這不是我的情況,在我的情況下,ID是使用觸發器生成的(大部分時間是帶序列的觸發器),而且我不希望教條處理生成ID ... –

+0

對不起,我沒有任何Oracle經驗,所以很遺憾不能給你一個更好的答案。 –

+0

我在這裏提出了一個解決方案 http://www.doctrine-project.org/jira/browse/DBAL-563 也許你想檢查它並幫助那:) –