我的条件中有一个with子句,如此
$criteria2->with = array('subcat','adimages');
但我实际上需要让这个subcat是唯一的,但如果我要添加这样的条件
$criteria2 = new CDbCriteria();
$criteria2->select = '"t".USERID, "t".ADID ,"t".ADTYPE, "t".ADTITLE, "t".ADDESC, "t".PAGEVIEW, "t".DISPPUBLISHDATE,"t".SUBCATID';
$criteria2->addCondition("USERID IN (".implode(',',$uniqueuser).")");
$criteria2->addCondition("ADID IN (".implode(',',$uniquead).")");
// $criteria2->addCondition("SUBCATID IN(".implode(',',$uniquesubcat).")");
// $criteria2->compare("\"t\".SUBCATID", $this->SUBCATID);
$criteria2->addCondition('DISPPUBLISHDATE >= CURRENT_DATE - 3 AND STATUS = 1
AND EXPIRYDATE >= CURRENT_DATE AND USERID IS NOT NULL');
$criteria2->addCondition('EXISTS (select 1 FROM {{ADIMAGES}} WHERE {{ADIMAGES}}.ADID = "t".ADID)');
$criteria2->order = '"t".PAGEVIEW DESC,"t".PUBLISHDATE DESC';
$criteria2->limit = 4;
$criteria2->addCondition("\"subcat\".SUBCATID IN(".implode(',',$uniquesubcat).")");
$criteria2->with = array('subcat','adimages');
$result2 = $this->findAll($criteria2);
return $result2;
我收到了定义的ambigious列的错误... 我如何将该subcat关系设置为唯一的
$uniquesubcat arrray ?
答案 0 :(得分:4)
尝试像这样设置表别名
$criteria2 = new CDbCriteria();
// SELECT
$criteria2->select = 't.USERID, t.ADID, t.ADTYPE, t.ADTITLE, t.ADDESC, t.PAGEVIEW, t.DISPPUBLISHDATE, subcat.SUBCATID';
// JOIN
$criteria2->with = array('subcat','adimages');
// WHERE
$criteria2->compare('t.STATUS', 1);
$criteria2->addCondition('t.DISPPUBLISHDATE >= CURRENT_DATE - 3');
$criteria2->addCondition('t.EXPIRYDATE >= CURRENT_DATE');
$criteria2->addCondition('t.USERID IS NOT NULL');
$criteria2->addCondition('EXISTS (select 1 FROM {{ADIMAGES}} as ADIMAGES WHERE ADIMAGES.ADID = t.ADID)');
$criteria2->addInCondition('t.USERID', $uniqueuser);
$criteria2->addInCondition('t.ADID', $uniquead);
$criteria2->addInCondition('subcat.SUBCATID', $uniquesubcat);
// ORDER
$criteria2->order = 't.PAGEVIEW DESC, t.PUBLISHDATE DESC';
// LIMIT
$criteria2->limit = 4;
$result2 = $this->findAll($criteria2);
return $result2;
答案 1 :(得分:0)
我在这里看到您正在尝试为CActiveRecord构建查询条件,但我认为您应该记住CDbCriteria-> with用于 Eager Loading ,意味着您将获得一个CActiveRecord对象,包括CDbCriteria->中带有关系名称的相关CActiveRecord。
因此,如果您正在寻找一个subcat并且急切地加载了所有子类,那么您应该对其进行过滤。
我更喜欢第二种选择。
$criteria2->join = "LEFT JOIN subcat s ON (s.SUBCATID = t.SUBCATID)"
所以,你可以改变这个:
$criteria2->addInCondition('subcat.SUBCATID', $uniquesubcat);
的
$criteria2->addInCondition('s.SUBCATID', $uniquesubcat);
总之,您可以使用join property of CDbCriteria为subcat
提供别名并消除您的查询歧义。