Pergunta

I have the following tables:

Each with its own mapper:
Product_Model_DbTable_Product
Product_Model_DbTable_Category
Product_Model_DbTable_ProdCategRelation

I've seen some tutorials about how it's done, but I still don't get it.


This is what I currently have:

class Product_Model_DbTable_Product extends Zend_Db_Table_Abstract
{

    protected $_name = 'product';
    protected $_dependentTables = array('Product_Model_DbTable_ProdCategRelation');
}

class Product_Model_DbTable_Category extends Zend_Db_Table_Abstract
{

    protected $_name = 'category';
    protected $_dependentTables = array('Product_Model_DbTable_ProdCategRelation');
}

class Product_Model_DbTable_ProdCategRelation extends Zend_Db_Table_Abstract
{
    protected $_name = 'product_category';
    protected $_referenceMap = array(
        'Product' => array(
            'columns' => 'pid',
            'refTableClass' => 'Product_Model_DbTable_Product',
            'refColumns' => 'id'
        ),
        'Category' => array(
            'columns' => 'cid',
            'refTableClass' => 'Product_Model_DbTable_Category',
            'refColumns' => 'id'  
        )
    );
}

And my experimental controller code (more or less working, but the method doesn't seem right; might as well go back to plain table-joining):

public function indexAction()
{
    $productObj = new Product_Model_DbTable_Product;
    $categoryObj = new Product_Model_DbTable_Category();

    $product = $productObj->fetchRow('id = 1');
    $productRelation = $product->findDependentRowset('Product_Model_DbTable_ProdCategRelation', 'Product')->current();

    $category = $categoryObj->fetchRow('id = ' . $productRelation->cid);
    $categoryInfo = $category->findDependentRowset('Product_Model_DbTable_ProdCategRelation', 'Category')->current();
}

Can I fetch a product's category by using these relationships just by instantiating a product, not the whole bunch?

Foi útil?

Solução

You can use findManyToManyRowset for that purpose. So your code might be transformed to:

$product = $productObj->fetchRow('id = 1');
$categoryInfo = $product->findManyToManyRowset(
    'Product_Model_DbTable_Category',
    'Product_Model_DbTable_ProdCategRelation'
)->current();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top