1

我使用 Zend 2 框架来构建我的 Web 应用程序。我通过本教程实现了我的数据库表模型:http: //framework.zend.com/manual/2.1/en/user-guide/database-and-models.html

我的数据库中的两个模型之间存在多对多关系。为了从他们那里获取数据,我用谷歌搜索并找到了这个链接: 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() 方法。如果我删除 from() 方法,它会说:

Statement could not be executed

任何人都可以帮助我吗?

4

1 回答 1

4
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 时,请尝试将其更改为 'voting.XXX '来自'v.XXX'

于 2013-04-08T11:00:36.510 回答