2014-02-23 49 views
2
<code> 
$resultSet = $this->tableGateway->select (function ($select) { 
$select->columns ( 
     array (
     'id', 
     'category_name' 
)); 

}); zf2 tablegateway按列名选择列

尝试使用上面的代码,但它返回所有列,下面是返回的输出。我需要从数据库

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] => )

+0

是什么问题?你告诉网关只选择'id'和'category_name',网关只填充这两个字段。那么你的问题究竟是什么,你想做什么? – Sam

+0

这里我想检索只有id和category_name,但为什么类别状态和category_created来... –

+1

因为你告诉你的'TableGateway'返回一个'类别'对象。然而,那些类别对象在所有其他字段上没有DATE,除了你想要的字段。所以我真的不知道你的问题是什么。 – Sam

回答

0

我有这个问题,选择ID和CATEGORY_NAME。我认为这可能是因为函数在第一个select函数中被忽略,它只是返回所有内容。我找到了一种方法来得到这个工作,尝试像下面的内容:

使用选择类随着tablegateway的selectWith功能:

use Zend\Db\Sql\Select as Select; 

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

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