2013-10-21 65 views
0

我有一个Yii应用程序,其中包含具有MANY_MANY关系的产品和兴趣。这些被映射,显然,以下关系式:MANY_MANY Yii查询限制到相关记录列表

  'interests'=>array(self::MANY_MANY, 'Interest', 'interest_product_assignment(product_id,interest_id)'), 

我想查询产品采用CDbCriteria像这样:

$products = Product::model()->with('interests')->findAll($criteria); 

此查询工作正常。我需要扩展它来将其限制为只有某些我有ID存储在数组中的兴趣。我相信这应该是可能的东西,如:

$products = Product::model()->with(
     'interests', 
     array('condition' => {not_sure_what_to_put_here}) 
    )->findAll($criteria); 

我不知道如何完成上面的查询,并一直在寻找了一段时间。这并不是说我找不到任何东西,但我无法理解我挖掘的任何东西。

任何人都可以找到如何完成此查询吗?

编辑

我已经在Telvin的建议尝试:

$products = Product::model()->with(
     'interests', 
     array('condition' => "interests_interests.interest_id IN ($selectedInterestsString)") 
    )->findAll($criteria); 

不加入的 'IN' 语句来查询。

回答

2

阵列提供的ID:

$array_ids = array('1','24','350','4609', ....)  
$array_ids_str = '"' . implode('","', array_values($array_ids)) . '"'; 

$products = Product::model()->with(array(
     'interests'=> array('condition' => "interest_id_column IN ($array_ids_str)" 
    )))->findAll($criteria); 
+0

谢谢您的回答Telvin。这不适合我。我不确定我是否写了正确的东西,但没有IN语句被添加到结果的SQL中。我会发布我写的东西。 – goose

+0

在你的编辑部分,我看到你的查询不正确,在我的“with”是数组后。 –

+0

我明白了。很快就会检查出来,谢谢你回到我身边。 – goose