2012-10-11 129 views
2

我是Zend Framework和php的新手。zend框架2教程AbstractTableGateway

我經歷了Zend Framework 2教程,並嘗試使用AbstractTableGateway查詢多個表。

,但得到網頁上的以下信息:

所提供的選擇對象的表名稱必須匹配表

這裏是我的代碼部分:

類PublicationTable擴展AbstractTableGateway {

protected $table = 'publication'; 

public function fetchAll() 
{ 
    $sql = new Sql($this->adapter); 
    $select = $sql->select(); 
    $select->from(array('p' => 'publication')) 
      ->join('author','publication_fk=p.publication_pk'); 

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


... 

}

我知道變量「protected $ table」是一個字符串。 那麼如何解決呢? 感謝您的幫助!

EC

回答

5

from()方法以表名,而不是列的列表。使用columns()指定所需的列。雖然我從來沒有嘗試過從TableGateway進行連接,但TableGateway並不是最好的模式。

如果使用直接DbAdapater,那麼這樣的事情應該工作:

use Zend\Db\Sql\Select, 
    Zend\Db\ResultSet\ResultSet; 

$select = new Select; 
$select->from('publication') 
    ->join('author', 'publication.publication_pk = author.publication_fk', 
      array('columnnamefromauthortable1', 'columnnamefromauthortable2')); 

$statement = $adapter->createStatement(); 
$select->prepareStatement($adapter, $statement); 

$resultSet = new ResultSet(); 
$resultSet->initialize($statement->execute()); 
+0

感謝。有效。 我想是這樣的: $選擇 - >從(陣列( 'P'=> 'vivo_publication'))... 並加載沒有錯誤的頁面。 – user1655688