Therefore for any given
user, we can obtain the list of places that they have reviewed and conversely, for any given place, we can list
the users who have reviewed it. This is known as a many-to-many relationship between places and users. To
map this relationship within a database, a third table, known as a link table, is required. In our case, the link
table is the Reviews table as shown in Figure 5.4.
Figure 5.4: A Many-to-Many relationship between Places and Users is created via the Reviews table acting as a link.
The Reviews table as two foreign keys, one to Places and one to Users and so there are two one-to-many
relationships also. There are three classes to define as shown in Listing 5.14.
Listing 5.14: Many-to-many relationship using Zend_Db_Table
class Places extends Zend_Db_Table_Abstract
{
protected $_name = 'places';
protected $_dependentTables = array('Reviews');
}
class Reviews extends Zend_Db_Table_Abstract
{
protected $_name = 'reviews';
protected $_referenceMap = array( #1
'Place' => array(
'columns' => array('place_id'),
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
'refTableClass' => 'Places',
'refColumns' => array('id')
),
'User' => array(
'columns' => array('user_id'),
'refTableClass' => 'Users',
'refColumns' => array('id')
)
);
}
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_dependentTables = array('Reviews'); #2
}
(annotation) <#1 Two rules in the $_referenceMap>
(annotation) <#2 Directly dependant classes only>
As you can see in Listing 5.
Pages:
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178