2016-06-18 41 views
0

我很困惑與Zend 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); 
    } 

指數一個.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> 

,但它顯示如下錯誤 enter image description here

回答

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

列方法簽名是這樣的:

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

所以默認prefixColumnsWithTable啓用。

這就是爲什麼你得到錯誤信息:

未知列SSID

因此,要解決這個問題最簡單的方法是在假傳遞的第二個參數的列:

$select->columns(array('s.id','a.artist', 's.name', 's.sem','s.age'), false);