Zend: Creating Models for the Product Table

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 ()
{
    // Add autoloader empty namespace
    $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 it so that it can be stored by the bootstrap
    return $autoLoader;
}

applications / models / product.php:

<?php
require_once 'Zend/Db/Table/Abstract.php';

class Model_Product extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'products';
    protected $_dependentTables = array('Model_Manufacturer');
}

application / models / Manufacturer.php:

class Model_Manufacturer extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    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) {
     //print_r($result);
     echo $result->product_name;
 }

, - , , ?

, /referenceMap, fetchAll , , , , referenceMap dependTables ?

+5
2

- , .

. , . (, $product_set = findProducts() findModel_Product())

II. (, findParentX()). , product , 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. product_manufacturers, : ON DELETE/UPDATE CASCADE . ( )

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



Fetching

, :

$manufacturer = $product_row->findParentManufacturer();


, :

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


, , Zend_Db_Table_Row_Abstract ( public function __call).

+9

, , $childObject- > findParentObject(), .

, [ ], , , findParentObject() , , . , , . , .

- , ? , , , , , , ...

rob ganly

0

All Articles