views:

360

answers:

1
+5  A: 

There are a few things to fix up here, but here are a few tips.

i. Table models work a little better if their names are pluralized. They sound better in the magic methods (e.g. $product_set = findProducts() instead of findModel_Product())

ii. The reference map is the important one and allows for those nifty magic selection methods (e.g. findParentX()) to work. It belongs in the dependent table, which is the product table here because it references the product_manufacturer.

  // in Model_Product
  protected $_referenceMap = array(

    // rule name
    'Manufacturer' => array(

      'columns'       => 'manufacturer_id',     // this column
      'refTableClass' => 'Model_Manufacturer',  // references that table object
      'refColumns'    => 'id'                   // and references that column
    )
  );



iii. Move dependent tables entry to your product_manufacturers table model, if you'd like: it's used only to replicate ON DELETE/UPDATE CASCADE database commands, though. (Leave it for now)

// in manufacturer table model
protected $_dependentTables = array('Model_Product');



Fetching

To get a product's manufacturer, do this:

$manufacturer = $product_row->findParentManufacturer();


To get a manufacturer's products, do this:

$product_set = $manufacturer_row->findProducts(); // Well, findModel_Product(), which is why you should rename your table ;)


For other magic methods originating from each single row, dig into the source code for Zend_Db_Table_Row_Abstract (especially public function __call).

Derek Illchuk
Thanks for taking the time to reply. "it's really only useful if you want to cascade deleted and updates through PHP" - so are you saying it's better to just keep it more simple by creating a DB Select object, which I've been doing up to this point if I don't need cascading and such?
meder
regarding the active manufacturers, I know the way to do it with the select object is just, `->where('product_manufacturers.active=?',1`.. so you're saying because I'm doing it a different way it's going to be harder than this?
meder
Regarding your first comment, I've clarified my post -- the $_dependantTables is used to replicate ON DELETE/UPDATE CASCADE database operations, if you don't have foreign keys in your database. Maybe leave it for now.
Derek Illchuk
Regarding your second comment, I have edited the very last part of my post. What I was *really* saying was simply, dig into the Zend_Db_Table_Row_Abstract's __call function code to learn more about the magic methods (including limiting the results using a 'select' object). Your use of 'select' is good, I think.
Derek Illchuk