2016-07-31 4 views
0

以下に私のアプリケーションのコードをいくつか追加しました。このproduct_categoryに属するproduct_categoryの名前と数だけが表示されます。それは魅力のように働く。しかし、私は特定の都市だけに属するカテゴリ名とその製品を再度入手したい。たとえば、「木製」カテゴリに属し、バクーに所在する製品の数を取得したいとします。私はモデルのgetActiveProducts()関数でいくつかの変更を行う必要があると思います。どんな助けもありがとうございます。Yii2は多数の条件と複数のテーブルを持っています

製品

id | name  | offer_category_id 
----------------------------- 
1 | Khar  | 3 
2 | SantaCruz | 2 
3 | Furniture | 2 
4 | VT  | 1 
5 | newFort | 4 

PRODUCT_CATEGORY

id | name 
-------------- 
1 | Khar   
2 | Wooden 
3 | Sion  
4 | VT 
5 | newFort 

product_adress

id | city  | offer_id 
----------------------------- 
1 | Baku  | 1 
2 | Ismailly | 2 
3 | Absheron | 5 
4 | Paris  | 4 
5 | Istanbul | 3 
: これらは私のテーブルです

マイコントローラ:

$data['productCategory'] = ProductCategory::find() 
->joinWith('products') 
->all(); 

ビューコード:

<?php foreach($data['productCategory'] as $key=>$value):    
<li> 
    <span class="m-offer-count"><?= $value->productsCount; ?></span> 
</li> 
<?php endforeach; ?> 

そして最後に、私のProductCategoryモデル:

public function getActiveProducts() { 
     $all_products = $this->hasMany(Product::className(),['product_category_id' => 'id'])->select(['id'])->where(['status'=>1])->all(); 
     return $all_product; 
} 
public function getProductsCount() { 
     return sizeof($this->activeProducts); 
} 

答えて

1
  1. Relation functionActiveQueryオブジェクトを返す必要があります:

    /** 
    * @return \yii\db\ActiveQuery 
    **/ 
    public function getProducts() { 
        return $all_products = $this->hasMany(Product::className(), ['product_category_id' => 'id']); 
    } 
    

    他の条件やステートメントなどの関数に追加しないでください。ActiveQuery::link()などのメソッドで使用できます。

  2. あなたは他の機能

    であなたの関係を再利用することができますが、ここであなたがCityクラスとProductクラスのaddress関係を持っていると仮定すると、都市

    /** 
    * @param City|null $city filter result by city 
    * @return \yii\db\ActiveQuery 
    **/ 
    public function getActiveProducts($city = null) { 
        $query = $this->getActiveProducts() 
         ->andWhere(['status' => 1]); 
    
        if (!empty($city)) { 
         $query->joinWith('address', false) 
          ->andWhere(['address.city_id' => $city->id]); 
        } 
    
        return $query; 
    } 
    

    によるフィルタリングの結果のためのオプションです。

    second parameter in joinWithは、eager loadingを無効にして、DBへの不要な追加クエリを回避します。

  3. あなたが代わりにActiveQuery::count()機能を使用し、そのカウントを取得する関連レコードのフルセットを選択しないでください。あなたのビューで$cityフィルタ

    <?php foreach($data['productCategory'] as $category):    
    <li> 
        <span class="m-offer-count"> 
         <?= $category->getActivePoductsCount($city); ?> 
        </span> 
    </li> 
    <?php endforeach; ?> 
    

/** 
* @param City|null $city get the count for particular city 
* @return integer 
**/ 
public function getActiveProductsCount($city = null) { 
    return $this->getActiveProducts($city)->count(); 
} 
  • 使用ps私はちょうどすべてのカテゴリを選択し、あなたは(あなたがさらにクエリまたはeager loadingのためにそれを使用していない場合)あなたのコントローラでjoinWithを作成する必要がないことだと思う:

    $data['productCategory'] = ProductCategory::find()->all(); 
    

    p.p.s.を私はここにあなたが$key

    希望を使用していないので、あなたは私は、 `それを共有したい場合、これは

  • +0

    ...助けIは、別の方法でこのタスクを解決し、配列$key => $valueをフェッチする必要はありませんと信じています –

    +0

    @ShaigKhaligliこの回答をあなたの解決策で更新できます。またはあなたの質問を更新してください。あなたがそれをどのように解決したかは面白いですし、誰かにとっても役立つかもしれません – oakymax

    関連する問題