2013-08-30 101 views
3

我需要將數據插入到一個表中,並使用tableGateway將數據引用到ZF2中的另一個表中。插入和更新數據到兩個表(交易)使用Zend框架2

例如,當我註冊一個用戶時,我必須將用戶數據插入到一個表中,並且此用戶將數據(多行)愛好數據到另一個表中,插入的用戶ID和更新數據的引用也應該有效。

我已經提到這個網址: Want to insert into two tables using one form in ZF2

但是這不會幫助我。

我該怎麼做。請幫我解決這個問題。

回答

3

假設我們處於「用戶」模式。所以默認tableGateway將插入數據在用戶表和愛好表中,我已經instaniated新tableGateway'$ userTable'。

$data = array(
     'id' => $user->id, 
     'name' => $user->name, 
    ); 
$this->tableGateway->insert($data); //this will insert data in user table 
$last_id=$this->tableGateway->lastInsertValue; //getting last inserted id 
$adapter=$this->tableGateway->getAdapter(); 
$userTable = new TableGateway('hobbies', $adapter); //this will insert in hobbies table. 
$data_arr = array(
     'link_id' => $last_id, 
     'music_info' =>'test', 
     ); 
$artistTable->insert($data_arr); 
+0

@Ngm AKumar請用戶這個,讓我知道是否有其他。 –