Search code examples
zend-framework2zend-db

Select Columns to return when joining a table with Zend\Db\Sql


I just want to return one column (per.cd_acao), so I tried something like:

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

but this is producing a query string like: SELECT "act".*, "per"."cd_acao" AS "cd_acao" FROM "tb_acao" AS "act" INNER JOIN "tb_perfil_acao" AS "per" ON "per"."cd_acao" = "act"."cd_acao" WHERE "per"."sq_perfil" = '89'

it is bringing all columns from the first table, when I want none. What am I missing here?

Update

summarizing: When I don't inform 'columns' in a select object, it defaults to return all columns to me. But when I'm joining, I don't want any columns to be returned by the first table.


Solution

  • An empty array will suffice

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