I am trying to create two models productsand product_manufacturersso that I can pull both manufacturers and products, edit them in the administrator, if necessary, and the usual CRUD material. Here's the schema for the tables (it is not finalized if you have any suggestions).
CREATE TABLE `product_manufacturers` (
`id` int(11) unsigned NOT NULL auto_increment,
`manufacturer_name` varchar(100) default NULL,
`active` tinyint(1) default '1',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
CREATE TABLE `products` (
`id` int(11) unsigned NOT NULL auto_increment,
`product_name` varchar(100) default NULL,
`manufacturer_id` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
Sample data:
(products) -
id product_name manufacturer_id
1 iPod Nano 4g 1
(product_manufacturers) -
id manufacturer_name active
1 Apple 1
I created two models, but I'm having problems loading products based on active product manufacturers (and yes, I use the model autoloader).
protected function _initAutoload ()
{
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH ,
'namespace' => '' ,
'resourceTypes' => array(
'form' => array('path' => 'forms/' ,
'namespace' => 'Form_') ,
'model' => array('path' => 'models/' ,
'namespace' => 'Model_')
)
));
return $autoLoader;
}
applications / models / product.php:
<?php
require_once 'Zend/Db/Table/Abstract.php';
class Model_Product extends Zend_Db_Table_Abstract
{
protected $_name = 'products';
protected $_dependentTables = array('Model_Manufacturer');
}
application / models / Manufacturer.php:
class Model_Manufacturer extends Zend_Db_Table_Abstract
{
protected $_name = 'product_manufacturers';
protected $_referenceMap = array(
'Model_Product' => array(
'manufacturer_id' => array('id'),
'id' => array('manufacturer_id'),
),
);
}
I had the feeling that I was completely mistaken, because I am creating an object from the Product Model and trying to print all products that have an appropriate manufacturer, and that the manufacturer is active.
$model = new Model_Product();
$results = $model->fetchAll();
foreach ($results as $result) {
echo $result->product_name;
}
, - , , ?
, /referenceMap, fetchAll , , , , referenceMap dependTables ?