gpt4 book ai didi

php - 通过Yii中的多对多关系查找不相关的记录

转载 作者:行者123 更新时间:2023-11-29 23:47:54 24 4
gpt4 key购买 nike

我有一个具有以下关系的 Orders ActiveRecord

    /**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'tests' => array(self::MANY_MANY, 'Test', 'orderstests(orders_id, test_id)'),
);
}

具有以下关系的测试 ActiveRecord

    /**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'orders' => array(self::MANY_MANY, 'Orders', 'orderstests(test_id, orders_id)'),
);
}

以及多对多关系记录

class OrdersTests extends CActiveRecord

我需要获取与特定订单没有关系的所有测试,即 order_id 的记录集,特定 order_id 不存在 test_id。

我似乎在 Yii 中找不到任何关系查询。

最佳答案

找到答案on Yii forum

public static function getTestsNotInOrder($order)
{
$excludedTestIds = array_keys($order->tests); // This makes use of the index attribute above

$criteria = new CDbCriteria();
$criteria->addNotInCondition('id', $excludedTestIds);

return self::model()->findAll($criteria);
}

关于php - 通过Yii中的多对多关系查找不相关的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25822245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com