ZF3:如何在ZF3中执行DB fetchRow()或fetchAll()或fetchCol()或fetchOne()?
问题描述:
我用ZF1 DB adapter,现在我正在学习ZF3,但我似乎无法找到等价的:ZF3:如何在ZF3中执行DB fetchRow()或fetchAll()或fetchCol()或fetchOne()?
$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中的每一行中执行上述操作?
答
一个字:没了。现在您必须改为处理ResultSet\ResultSetInterface
接口。但它是一个迭代器。你一定不会有任何麻烦得到结果。
答
是的,它消失了。
您可以按照与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();
一个侧面说明对自己和别人看:通过使用'ResultSetInterface',它有一个有趣的功能来获取所有的结果,就像这样:'$结果= $适配器 - >查询从我的代码示例('SELECT * from users',Adapter :: QUERY_MODE_EXECUTE); $ users = $ result-> toArray();' – evilReiko