Now that we have explored the
Zend_Db_Table class and its assocated rowset and row classes, we can look at joining tables together.
5.3.2 Table Relationships with Zend_Db_Table
Table relationships are where Zend_Db_Table starts getting very interesting. This is the realm of linking tables
together using SQL JOIN statements and is one area where it is nice to get the framework doing the leg work
for us. We will look at how Zend_Db_Table can help us with one-to-many and many-to-many relationships.
One-to-Many relationships
We have already come across one to many relationships in Chapter 3 when we created the initial schema for
the Places application. Each location can have many reviews. This is shown in figure 5.3.
Figure 5.3: A One-to-Many relationship is created via a foreign key (places_id) in the Reviews table.
To represent this using Zend_Db_Table we create two new classes that extend from
Zend_Db_Table_Abtract and then use the $_dependantTables and $_referenceMap properties to link them
together. This is show in Listing 5.13.
Listing 5.13: One-to-many relationship using Zend_Db_Table
class Places extends Zend_Db_Table_Abstract
{
protected $_name = 'places'; #1
protected $_dependentTables = array('Reviews'); #2
}
class Reviews extends Zend_Db_Table_Abstract
{
protected $_name = 'reviews';
protected $_referenceMap = array( #3
'Place' => array( |#4
'columns' => array('place_id'), |
'refTableClass' => 'Places', |
'refColumns' => array('id') |
)
);
}
(annotation) <#1 name of the database table>
(annotation) <#2 classname of dependency >
(annotation) <#3 list of relationships for this table>
(annotation) <#4 one-to-many relationship definition>
Licensed to Menshu You
Pages:
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176