zf2 tablegateway select columns by column name

2019-07-09 02:52发布

<code> 
$resultSet = $this->tableGateway->select ( function ($select) {
$select->columns ( 
        array (
        '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] => )

1条回答
Melony?
2楼-- · 2019-07-09 03:19

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:

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);
查看更多
登录 后发表回答