2013-04-08 106 views
0

我使用Zend 2 Framework構建我的web應用程序。我通過本教程實現了我的數據庫表模型:http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.htmlZend 2 Framework中的多對多關係

我在數據庫中的兩個模型之間有多對多的關係。從他們那裏獲取數據我搜索了一下,發現這個鏈接:http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/

問題是所有的表模型從Zend_Db_Table_Abstract延伸的例子。我不知道如何從模型中獲取數據。

我有一個包含投票的表格,每個投票都有一個唯一的哈希ID。每個投票也都有標籤。因此,我定義了一個表格tags,其中包含所有可用標籤和一個voting_tag_map,其中映射了所有多對多關係。

我至今嘗試過如下,這是代碼從我VotingTable類:

public function getTagsByVoting($votingHash){ 
    $select = $this->tableGateway->getSql()->select(); 
    $select->from(array('v' => 'voting')) 
      ->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id') 
      ->join('tags', 'voting_tag_map.tag_id=tags.tag_id'); 
    $resultSet = $this->tableGateway->selectWith($select); 
    return $resultSet; 
} 

它接着說:因爲從()方法的

Since this object was created with a table and/or schema in the constructor, it is read only. 

那。如果我刪除from()方法,它說:

Statement could not be executed 

任何人都可以幫助我嗎?

回答

2
Since this object was created with a table and/or schema in the constructor, it is read only. 

這個錯誤是因爲你試圖from子句中設置表名,但它已經在TableGateway的構造器設置,並且你不能改變它設置一次。

如果你真的需要這樣做,那麼你可以自己擴展AbstractTableGateway,那麼你將不必向構造器添加一個字符串表名,但是你並不需要在你的主表上使用別名......

當你註釋掉from()方法時,你會得到SQL錯誤,這是因爲你在引用投票表時,因爲它在連接中是別名'v',所以當你不使用別名v時,嘗試將它改爲'vXXX''voting.XXX'