0

事前に感謝します...コードの下を参照してください..私は2つのモデル、カテゴリと製品を持っています

私の製品モデルクラスAdmin_Model_ProductはZend_Db_Table_Abstractを拡張します{

protected $_name = 'products';
protected $_referenceMap = array(
    'category' => array(
        'columns' => array('category_id'),
        'refTableClass' => 'Admin_Model_Category',
        'refColumns' => array('id'),
        'onDelete' => self::CASCADE,
        'onUpdate' => self::RESTRICT
    )
);

}

私のカテゴリモデルは次のとおりです。

class Admin_Model_Category extends Zend_Db_Table_Abstract {

protected $_name = 'categories';
protected $_dependentTables = array('Admin_Model_Product');

私の製品コントローラーには

class Admin_ProductsController extends Zend_Controller_Action {

public function init() {

}

public function indexAction() {
    echo '<pre>';
    $model = new Admin_Model_Product();

}

}

私がする必要があるのは、fetchAll()メソッドを使用してすべての製品を取得し、各製品の親を取得してビューに表示する必要があります...すべての製品をプルできますが、各製品の親カテゴリを見つける方法がわかりません。それらをバインドします。ソースコードの例はありますか?または何か提案?すべての製品と親カテゴリ名を含む配列が必要です。

4

2 に答える 2

0

これを実現する最善の方法は、製品の結果を反復処理し、すべてのcategories_idsを含む配列を作成してから、を使用してカテゴリモデルをクエリし、を使用してwhere('category_id IN (?)', $array_of_categories_ids)カテゴリ行セットから配列を作成することid_category => row_pairsです。次に、2つのクエリでこれを行うことができます:)

$categories_ids = array();
foreach ($products as $product)
{
     $categories_ids[ $product->category_id ] = $product->category_id; // set key to category id to avoid duplicated category' ids
}
$categories = $categoryModel->fetchAll($categoryModel->select()->where('id_category in (?)', $categories_ids)); // here u have to add check on array 'coz if empty it will thoro Query exception
// now just iterate over categories
$categories = array();
foreach ($categories as $category)
{
     $categories[ $category->id_category ] = $category;
}
// now when iterating over products u can just do $categories[ $product->category_id ] to get proper category for profuct

とにかくタイプミスの可能性をお詫びします、その場でそれを書きました;)

于 2012-02-20T21:27:17.917 に答える
-1

次のことを試してください:

カテゴリとその製品を取得する:

$model_category = new Admin_Model_Category();
$categorySet = $model_category->fetchAll(); 
$categories = $categorySet->toArray();

$i = 0;
$results = array();
foreach($categories as $category)
   $categoryRow = $model_category->fetchRow('id =', $category->category_id)
   $products = $categoryRow->findDependentRowset('Admin_Model_Product');
   $results[$i]['parent'] = $category;
   $results[$i]['products'] = $products;
   $i++;
}

次に、それをビューに渡します。

$view->results = results;
于 2012-02-20T21:36:53.873 に答える