【问题标题】:MANY_MANY Yii query limiting to list of related recordsMANY_MANY Yii 查询限制为相关记录列表
【发布时间】:2013-10-21 21:29:43
【问题描述】:

我有一个 Yii 应用,其中包含具有 MANY_MANY 关系的产品和兴趣。显然,这些映射具有以下关系:

            '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”语句。

【问题讨论】:

    标签: php sql yii many-to-many


    【解决方案1】:

    提供的 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);
    

    【讨论】:

    • 感谢您的回答特尔文。它不太适合我。我不确定我是否写了正确的东西,但是没有 IN 语句被添加到产生的 SQL 中。我会发布我写的内容。
    • 在您的编辑部分,我看到您的查询不正确,在我的“与”是数组之后。
    • 我明白了。很快就会检查出来,谢谢你回来找我。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多