ZF3:如何在ZF3中执行DB fetchRow()或fetchAll()或fetchCol()或fetchOne()?

时间:2017-08-03 11:14:21

标签: php zend-framework zend-db zend-framework3

我已经使用了ZF1 DB adapter,现在我正在学习ZF3,但我似乎无法找到相同的内容:

$rows = $db->fetchAll('select * from `my_table`');
$row = $db->fetchRow('select * from `my_table` where `id` = 1');
$values = $db->fetchCol('select `my_col` from `my_table`');
$value = $db->fetchOne('select `my_col` from `my_table` where `id` = 1');

我在ZF3中发现的例子提到了使用准备语句。那么如何在ZF3中的每一行中进行上述操作?

2 个答案:

答案 0 :(得分:2)

一个字:走了。现在你必须处理ResultSet\ResultSetInterface接口。但它是一个迭代器。你肯定不会有任何问题得到结果。

答案 1 :(得分:2)

是的它已经消失了。

您可以使用与ZF1相同的方式构建选择。我的代码示例:

// From Mapper that extends AbstractTableGateway 
// and implements AdapterAwareInterface

$select = $this->getSql()->select()
        ->join('articles', 'article_events.article_uuid = articles.article_uuid')
        ->where(['articles.article_id' => $id]);

$result = $this->selectWith($select);  

// $result; is fetchAll()
// $result->current();  is fetchRow() or fetchOne() 
// $result->current()->col_name  is fetchCol();