如何在Zend2中的删除查询中使用限制和顺序

时间:2016-09-28 12:03:29

标签: zend-framework2 sql-order-by sql-delete zend-db-table sql-limit

我有以下sql查询,我需要在zend2中编写它。

DELETE FROM test_table WHERE user_id = 2 ORDER BY id ASC LIMIT 10

我试过这个:

$sql = new Sql($this->dbAdapter);
$delete = $sql->delete();
$delete->from('test_table');
$delete->where(array('user_id' => 4));

$delete->order('Id ASC');
$delete->limit(10);

$stmt = $sql->prepareStatementForSqlObject($delete);
$results = $stmt->execute();

但是当我添加这两行时它不起作用:

$delete->order('Id ASC');
$delete->limit(10);

2 个答案:

答案 0 :(得分:0)

猜猜你正在使用SQL适配器。但是,如果您查看SQL ZF delete() implementation,您会发现没有order()limit()方法。所以你不能打电话给他们......

答案 1 :(得分:0)

您可以尝试以下操作:

$viewData = DB::table('tbl_contents')
                ->where('record_type', '=', 'Event')
                ->where('status', '=', 1)
                ->paginate(3);

return json_encode($viewData);

OR

$sql = new Sql($this->dbAdapter);
$delete = $sql->delete();
$delete->from('test_table');
$delete->where(array('user_id' => 4));
// get sql string
$deleteQuery = $sql->getSqlStringForSqlObject($delete);
// append the order by and limit
$deleteQuery .= " ORDER BY id ASC LIMIT 10";
// execute query
$result = $this->dbAdapter->query($deleteQuery, 'execute');