If you have a relationship between two tables, you have a parent and a
dependent by definition. The dependent contains a foreign key that
references the primary key of its parent. In the relationship you
described, your parent is product_categories, and your dependent is
products.
The SQL table definitions for the relationship you describe might look
like this:
CREATE TABLE product_categories (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE products (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
category_id BIGINT UNSIGNED
REFERENCES product_categories(id)
);
The Zend_Db_Table classes might look like this:
// Parent table
class ProductCategoryTable extends Zend_Db_Table_Abstract
{
protected $_name = 'product_categories';
}
// Dependent table contains _referenceMap
class ProductTable extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
protected $_referenceMap = array(
'Category' => array(
'columns' => array('category_id'),
'refTableClass' => 'ProductCategoryTable'
)
);
}
By default, the referenced column(s) in the parent table are the
column(s) of its primary key, so you don't need to declare 'refColumns'.
When you get a row of the ProductCategoryTable, you can iterate through
products that belong to a given category like this:
$categoryTable = new ProductCategoryTable();
$categoryRow = $categoryTable->find(123)->current();
$productRowset =
$categoryRow->findDependentRowset('ProductCategoryTable');
foreach ($productRowset as $productRow) {
// do whatever you want with the row, e.g.:
print_r($productRow->toArray());
}
Regards,
Bill Karwin
________________________________
From: Matt M. [mailto:[EMAIL PROTECTED]
Sent: Sunday, July 22, 2007 6:50 AM
To: [email protected]
Subject: [fw-general] Zend_Db_Table Relationships - help
Hi,
I'm trying to follow the docs but I'm still confused as to what
to do to get relationships going. Here are my models:
Product + ProductTable
ProductCategory + ProductCategoryTable
I don't want cascading actions, so I left out $_dependentTables
as the docs say.
My tables are setup so products.category_id is a link to the
category the product belongs to. The product_categories table doesn't
have a reference to the product. So:
products.id
products.category_id
product_categories.id
I want to be able to iterate through the products a category has
and also have access to the category data a product belongs to. With
that, what would my class definitions look like?
I'm confused with the terminology used in the docs. Dependant
table and parent table don't really fit in here. From my perspective,
neither of these tables are parents.
Anyone want to post a simple set of classes that would enable
the functionality I'm seeking? :)
Matt