Php 通过Yii中的多个关系查找不相关的记录

Php 通过Yii中的多个关系查找不相关的记录,php,mysql,sql,activerecord,yii,Php,Mysql,Sql,Activerecord,Yii,我有一个具有以下关系的订单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

我有一个具有以下关系的订单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\u id的记录集,对于特定的order\u id,test\u id不存在

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

找到了答案

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);
}