zf2 tablegateway select columns by column name

2019-07-09 02:49发布

问题:

<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:

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);