2012-04-20 127 views
0

我正在使用Doctrine2 + CodeIgniter2,並試圖創建一個連接表的簡單測試。通過學說實體插入外鍵?

這裏是我所涉及的兩個表的模式:

CREATE TABLE test_lastnames(ID INT AUTO_INCREMENT NOT NULL,姓氏 VARCHAR(255)NOT NULL,PRIMARY KEY(ID))ENGINE = InnoDB的;

CREATE TABLE test_firstnames(ID INT AUTO_INCREMENT NOT NULL, mylastname_id INT DEFAULT NULL,如first_name VARCHAR(255)NOT NULL, INDEX IDX_23D7305696EC0FA4(mylastname_id),PRIMARY KEY(ID))ENGINE = InnoDB的;

ALTER TABLE test_firstnames添加約束FK_23D7305696EC0FA4 FOREIGN KEY(mylastname_id)參考test_lastnames(ID)

這裏是我的YAML映射

ORM\Testing\Firstnames: 
    type: entity 
    table: test_firstnames 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    firstname: 
     type: string 
     column: first_name 
    manyToOne: 
    mylastname: 
     targetEntity: ORM\Testing\Lastnames 

ORM\Testing\Lastnames: 
    type: entity 
    table: test_lastnames 
    fields: 
    id: 
     type: integer 
     id: true 
     generator: 
     strategy: AUTO 
    lastname: 
     type: string 
     column: last_name 

我試圖將數據寫入表格。

$new_lastname = new ORM\Testing\Lastnames; 
$new_lastname -> setLastName ('Shakespear'); 
$this->doctrine->em->persist($new_lastname); 
$this->doctrine->em->flush(); 

$new_firstname = new ORM\Testing\Firstnames; 
$new_firstname->setFirstname('William'); 
$new_firstname->setMyLastName($new_lastname ->getID()); 
$this->doctrine->em->persist($new_firstname); 
$this->doctrine->em->flush(); 

它返回下列錯誤:

消息:傳遞給ORM \測試\ Firstnames參數1 :: setMylastname()必須ORM \測試\ Lastnames,整數給出的實例,叫做在/[PATH]/applicationFolder/controllers/testing/test_namejoins_insert.php上線31和限定

文件名:測試/ Firstnames.php

行號:66

以及一堆Message: spl_object_hash() expects parameter 1 to be object, integer given錯誤。

這裏是線66 Firstnames.php:public function setMylastname(\ORM\Testing\Lastnames $mylastname = null)

我還沒有開始它黑客 - 是問題就在那裏用 '$ mylastname = NULL'?

如何通過實體插入外鍵值?

回答

0
$new_firstname->setMyLastName($new_lastname); 

而不是$new_firstname->setMyLastName($new_lastname ->getID());