选择使用Zend \ Db \ Sql

时间:2015-09-08 18:33:07

标签: zend-framework2 zend-db

我只想返回一列(per.cd_acao),所以我尝试了类似的东西:

$select->from(array('act' => 'tb_acao'))
           ->join(array('per' => 'tb_perfil_acao'),
                'per.cd_acao = act.cd_acao',
                array('cd_acao'),
                $select::JOIN_INNER
            );

但这会生成一个查询字符串,如:     SELECT" act"。*," per"。" cd_acao" AS" cd_acao"来自" tb_acao" AS"行动" INNER JOIN" tb_perfil_acao" AS" per" ON" per"。" cd_acao" ="行为"。" cd_acao"在哪里"每"。" sq_perfil" =' 89'

当我不想要时,它会从第一个表中提取所有列。我在这里缺少什么?

更新

总结:当我没有通知'列'在select对象中,它默认返回所有列给我。但是当我加入时,我不希望第一个表格返回任何列。

2 个答案:

答案 0 :(得分:3)

空数组就足够了

$select->from(array('act' => 'tb_acao'))
           ->columns(array())
           ->join(array('per' => 'tb_perfil_acao'),
                'per.cd_acao = act.cd_acao',
                array('cd_acao'),
                $select::JOIN_INNER
            );

答案 1 :(得分:0)

尝试使用此查询

use Zend\Db\Sql\Sql;
protected $tableGateway;
public $adapter;

public function __construct(TableGateway $tableGateway)
{
    $this->tableGateway = $tableGateway;
}

publice function getData(){
    $adapter = $this->tableGateway->getAdapter();
    $sql = new Sql($adapter);
    $select = $sql->select();
    $select->from('tb_acao')
       ->join('per'),
            'per.cd_acao = act.cd_acao',
            array('cd_acao'),
        );
    $statement = $sql->prepareStatementForSqlObject($select);
    $results = $statement->execute();

    return $results;
}