3

我需要使用 tableGateway 将数据插入到一个表中并将数据引用到 ZF2 中的另一个表中。

例如,当我注册用户时,我必须将用户数据插入到一个表中,并且该用户爱好数据(多行)到另一个表中,插入的用户 ID 和更新数据的引用也应该可以工作。

我已经引用了这个网址: 想要在 ZF2 中使用一种表格插入两个表格

但这对我没有帮助。

4

1 回答 1

3

假设我们处于“用户”模型中。因此,默认情况下 tableGateway 将在用户表中插入数据,对于爱好表,我将新的 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);
于 2013-12-17T17:08:34.073 回答