在 zend 框架 2 中需要自定义连接查询的帮助

need help in custom join query in zend framework 2

我对带有多列显示的 zend 2 连接查询感到困惑,无法显示那些列

StudTable.php

public function custom_query() {
        $select = new Select();
        $select->from(array('s' => 'stud'));
        $select->columns(array('s.id','a.artist', 's.name', 's.sem','s.age'));
        //$select->where(array('id' => 14));

        $select->join(array('a' => 'album'),
                    'a.id = s.album_id');
        $select->order('s.id DESC'); 
        $select->limit(5);

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

index.phtml

<ul>
    <?php foreach ($this->customeObject as $obj_cus) : ?>
        <li>ID: <?php echo $obj_cus->id?>| Artist: <?php echo $obj_cus->artist?> | Name:<?php echo $obj_cus->name?> | Sem:<?php echo $obj_cus->sem?> | Age:<?php echo $obj_cus->age?></li>
    <?php endforeach; ?>
    </ul>

但是显示如下错误

列方法签名如下所示:

public function columns(array $columns, $prefixColumnsWithTable = true)

因此默认情况下启用 prefixColumnsWithTable。

这就是您收到错误消息的原因:

Unknown column s.s.id

因此,解决此问题的最简单方法是将 false 作为第二个参数传递给列:

$select->columns(array('s.id','a.artist', 's.name', 's.sem','s.age'), false);
$select = new Select();
$select->from(array('s' => 'stud'));
/* Select columns from primary table without prefix table */
$select->columns(array('id', 'name', 'sem', 'age'));

/* If need where */
$select->where(array('s.id' => 14));

/* It's a INNER JOIN */
$select->join(
    array('a' => 'album'),
    'a.id = s.album_id',
    /* Select joined columns */
    array('artist')
);
$select->order('s.id DESC'); 
$select->limit(5);
return $resultSet = $this->tableGateway->selectWith($select);