[英]need help in custom join query in zend framework 2
I am confused with zend 2 join query with multiple column display, and unable to display those column 我对带有多列显示的zend 2 join查询感到困惑,无法显示这些列
StudTable.php 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 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>
The columns method signature looks like this: 列方法签名如下所示:
public function columns(array $columns, $prefixColumnsWithTable = true)
So by default prefixColumnsWithTable is enabled. 因此,默认情况下启用prefixColumnsWithTable。
That's why you're getting the error message: 这就是为什么您收到错误消息的原因:
Unknown column ssid
未知列ssid
So the easiest way to fix this is to pass in false as the second parameter to columns: 因此,解决此问题的最简单方法是将false作为第二个参数传递给column:
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.