0

名前が付けられた 2 つのテーブルがcontentありcategoryMANY_MANY次のような関係があります。

'contents'=>array(self::MANY_MANY, 'Content',
                'content_category(category_id, content_id)',
                'order'=>'contents.id DESC',
    )

content_category は、これら 2 つのテーブルの中間のテーブルです。したがって、この関係を使用して、次のようなカテゴリのコンテンツを見つけます。

$category = Category::model()->findByPk($cat_id);
foreach ($category->contents as $content)
{
    //some code for each content
}

contentここで、このリレーションがコンテンツを見つけるために、テーブルのリレーションから条件を書きたいと思います。私の状態はさまざまで、$_GET変数から取得できます。条件をどのように書く必要があるか

4

1 に答える 1

0

これを行うには、CDbCriteria を使用する必要があります。簡単な例を次に示します。

$criteria = new CDbCriteria;
$criteria->select = 'the columns you need';
$criteria->limit = 5; // the number of row to fetch
$criteria->condition = 'column=:param OR column2="example"';
$criteria->params = array(':param' => $_GET['param'];//bind the param to the condition

//here I am going to add some conditions about the related model
$criteria->with = array(
   'content' => array(
       'select' => 'the columns you need in the related model',
       'condition' => 'column=:param',
       'params' => array(':param' => $_GET['param']),
   ),
);

$category = Category::model()->findAll($criteria);

その他のオプションについては、 CDbCriteriaに関する Yii のドキュメントを参照してください。yii ガイドにもいくつかの例があります

于 2013-01-27T12:23:54.533 に答える