【问题标题】:Zend / ZF2 / TableGateway mysql_insert_id replacement?Zend/ZF2/TableGateway mysql_insert_id 替换?
【发布时间】:2012-09-28 15:31:41
【问题描述】:

我正在尝试从 mysql 切换到 Zend / TableGateway 类。

不知道有没有类似mysql_insert_id的方法,可以获取最后插入的行的自增id。

谷歌搜索,我找到了一些答案,指向 DB-Adapter 的 lastInsertId(),但是,这种方法在 ZF2 中似乎不再可用。 另外:insert 方法的返回值是一个布尔值,而不是 ZF1 中的最后一个 id。

目前我正在使用一个丑陋的解决方法。请看下面的代码。

是否有更好/推荐的获取 ID 的方法?

table_1 {
    id: integer, primary key, autoincremented
    content: text
}

table_2 {
    table_1_id: integer
    other_content: text
}

// using mysql
$sql = "INSERT INTO table_1 (content) VALUES ('some text')";
$result = mysql_query($sql);
// check omitted

$id = mysql_insert_id();
$sql = "INSERT INTO table_2 (table_1_id, other_content) VALUES ($id, 'other text')";
$result = mysql_query($sql);


// using Zend - this is the code, I am currently using
//*************************************************************
// get_last_insert_id emulation; works only if content is unique
private function getLastInsertId($tableGateway, $content) {
    $entries = $tableGateway->select(array('content' => $content));
    foreach ($entries as $entry) {
        return $entry->id;
    }

    return null;
}
// another option: get highest ID, must be the last inserted
private function getLastInsertId($tableGateway) {
    // needs a method like 'getRowWithHighestId'
}
//*************************************************************

// ...
table_1_entry = new Table1Entry('some text');
$tableGateway->insert($hydrator->extract($table_1_entry));

//*************************************************************
// using the workaround:
$id = getLastInsertId($tableGateway, $table_1_entry->content);
// 
// there MUST be some Zend method to get this last id.
//*************************************************************

table_1_entry = new Table1Entry('other text', $id);
$tableGateway->insert($hydrator->extract($table_2_entry));

【问题讨论】:

    标签: zend-framework2 zend-db lastinsertid last-insert-id tablegateway


    【解决方案1】:

    使用魔法属性$tableGateway->lastInsertValue 获取Id

    $id = $tableGateway->lastInsertValue;
    

    执行插入数据时设置该属性

    source

    $this->lastInsertValue = $this->adapter->getDriver()->getConnection()->getLastGeneratedValue();
    

    【讨论】:

    • 值得注意的是,还有一个访问器“getLastInsertValue()”返回相同的值。值得使用,这样您就可以模拟响应并使您的课程更具可测试性。
    【解决方案2】:
    `enter code here  `$data_add=array(
                    'line_1'        =>  $Data1->line_1,
                    'line_2'        =>  $Data1->line_2,
                    'line_3'        =>  $Data1->line_3,
                    'city_id'       =>  $Data1->city_id,
                    'state_id'      =>  $Data1->state_id,
                    'country_id'    =>  1,
                    'is_active'     =>  '1',
            );
                $adapter = $this->tableGateway->getAdapter();
                $otherTable = new TableGateway('address', $adapter);
                $otherTable->insert($data_add);
                $lastInsertValue= $adapter->getDriver()->getConnection()->getLastGeneratedValue();
                print_r('lastInsertId1 :'.$lastInsertValue);
                error_log($lastInsertValue);
            die();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多