Zend 2框架中的多对多关系 [英] Many-to-Many relationship in Zend 2 Framework

查看:169
本文介绍了Zend 2框架中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html

我的数据库中的两个模型之间存在多对多关系.为了从他们那里获取数据,我在Google上搜索并找到了此链接:

I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/

问题是示例中所有表模型都从Zend_Db_Table_Abstract扩展.我不知道如何从模型中获取数据.

The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.

我有一个包含投票的表格,每个投票都有一个唯一的哈希ID.每个投票也都有标签.因此,我定义了一个具有所有可用标签的表tags和一个映射了所有多对多关系的voting_tag_map.

I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.

到目前为止,我一直在尝试以下内容,这是我的VotingTable类中的代码:

What I have tried so far is the following, that's code from my VotingTable class:

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()方法,它会显示:

Thats because of the from() method. If I delete the from() method, it says:

Statement could not be executed

有人可以帮我吗?

推荐答案

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

此错误是因为您试图在from子句中设置表名,但是已经在TableGateway的构造函数中对其进行了设置,并且一旦设置便无法更改.

This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.

如果您确实需要执行此操作,则可以自己扩展AbstractTableGateway,则不必在构造函数中添加字符串表名,但实际上并不需要在主表上使用别名...

If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...

注释掉from()方法时遇到的SQL错误将归因于您在联接中引用了表决表,因为它是别名"v",当您不使用别名v时,请尝试将其更改为来自"v.XXX"的投票"XXX"

The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'

这篇关于Zend 2框架中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆