如何在zendframework 2中向tablegateway添加连接列

时间:2014-02-20 22:20:37

标签: join zend-framework2 zend-db

我现在正在努力与Zend正确地在sql字符串中连接表列。

我有这个代码(工作正常):

$expression = new \Zend\Db\Sql\Expression('group = acl_groups.id');

$select = $this->tableGateway->getSql()->select();
$select->columns(array(
    $select::SQL_STAR,
    'group',
    'id',
    'status'
))->join('acl_groups', 'group = acl_groups.id');
return $this->tableGateway->selectWith($select);

但是我不能让acl_groups列工作。 任何尝试都失败了我已阅读Zend文档,但对我来说没有任何有效的工作。

我的联接表有以下列:

id
name
status

我需要名称才能有别名“groupname”

我试过了:

$select->columns(array(
    $select::SQL_STAR,
    'group',
    'id',
    'status',
    array('acl_groups.name' => 'groupname')
)

$select->columns(array(
    $select::SQL_STAR,
    'group',
    'id',
    'status',
    'acl_groups.name AS groupname'
)

但没有效果。

1 个答案:

答案 0 :(得分:1)

如前面提到的here

,您有两种方法可以做到这一点

方法1

 $resultSet = $tableGateway->select (function (Select $select) {
    // now you have `select` object, do whatever you like
});

方法2

$select = new Select('table_name');
$select->join(
  'another_table_name',
  'join condition',
  array('column of another table name'),
  Select::JOIN_INNER
);
$resultSet = $tableGateway->selectWith($select);