将单个输入与2个表列进行比较

时间:2013-09-13 03:39:17

标签: sql yii

这里有没有人经历过使用标准 - >比较通过模型中的关系将单个输入与2个单独的表列进行比较?

这是我的关系声明:

    'class' => array(self::HAS_ONE, 'BaseEiEquipItem', 'iar_no'),
    'classSi' => array(self::HAS_ONE, 'BaseSiReceivedItem','iar_no'),

现在我想比较我的输入,如果它存在于这两个表中的任何一个

    if(!empty($_GET['BaseIar']['description'])){
        $this->description= $_GET['BaseIar']['description'];
        $criteria->with = array('class','classSi');//this will serve as join
        $criteria->compare('class.description,classSi.description', $this->description, true ); // true is equivalence of like %%
    }
    else{
        $this->description='';
        $criteria->with = array('class','classSi' );//this will serve as join
        $criteria->compare('class.description,classSi.description', $this->description, true ); // true is equivalence of like %%
    }

2 个答案:

答案 0 :(得分:0)

为什么你重复相同的代码时间并再次尝试使用三元运算符 试试这个

$this->description = (!empty($_GET['BaseIar']['description'])) ? $_GET['BaseIar']['description'] : '';
$criteria->with = array(
                    'class' => array(
                      'condition' => "class.description LIKE :desc"
                      'params' => array(":desc" => "%{$this->description}%")
                    ),
                    'classSi' => array(
                      'condition' => "classSi.description LIKE :desc"
                      'params' => array(":desc" => "%{$this->description}%")
                    ));

答案 1 :(得分:0)

你可以试试这样的事情

$criteria->compare('class.description', $this->description, true);
$criteria->compare('classSi.description', $this->description, true, 'OR');