zf2 tablegateway按列名选择列 [英] zf2 tablegateway select columns by column name

查看:73
本文介绍了zf2 tablegateway按列名选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<code> 
$resultSet = $this->tableGateway->select ( function ($select) {
$select->columns ( 
        array (
        'id',
        'category_name' 
) );

});

尝试使用上面的代码,但是它返回所有列,下面是return的输出.我需要从数据库中选择ID和category_name

tried with above code but it returning all columns, below is the output of return. I need to select id and category_name from database

Category\Model\Category Object ( [id] => 2 [category_name] => Cat Two [category_created] => [category_status] => [inputFilter:protected] => ) Category\Model\Category Object ( [id] => 4 [category_name] => Cat one [category_created] => [category_status] => [inputFilter:protected] => )

Category\Model\Category Object ( [id] => 2 [category_name] => Cat Two [category_created] => [category_status] => [inputFilter:protected] => ) Category\Model\Category Object ( [id] => 4 [category_name] => Cat one [category_created] => [category_status] => [inputFilter:protected] => )

推荐答案

我遇到了这个问题.我认为这可能是因为该函数在第一个select函数中被忽略了,而只是返回了所有内容.我找到了一种使它工作的方法,请尝试以下操作:

I had this problem. I think it might be because the function is being ignored inside the first select function and it just just returning everything. I found a way to get this to work, try something like the following:

将Select类与tablegateway的selectWith函数一起使用:

Use the Select class along with the selectWith function of the tablegateway:

use Zend\Db\Sql\Select as Select;

$select = new Select();
$select->from('table');
$select->columns(array('id','category_name'));

$resultSet = $this->tableGateway->selectWith($select);

这篇关于zf2 tablegateway按列名选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆