Many-to-Many relationship in Zend 2 Framework

梦想的初衷 提交于 2019-12-11 04:18:34

问题


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

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/

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.

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.

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;
}

It says then:

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

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

Statement could not be executed

Can anyone help me please?


回答1:


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

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.

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...

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'



来源:https://stackoverflow.com/questions/15876755/many-to-many-relationship-in-zend-2-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!