为什么我不能在yii项目中与关系做这个查询?

我想用Yii关系创建一个查询。 这是我的关系:

//Articles model
return array(
            'articlesHasTags' => array(self::HAS_MANY, 'ArticlesHasTag', 'articles_id',
                            "with"=>"tag",
                            ),
                            ),
//ArticlesHasTag model
        return array(
            'articles' => array(self::BELONGS_TO, 'Articles', 'articles_id'),
            'tag' => array(self::BELONGS_TO, 'Tag', 'tag_id'),
        );

这是查询:

  $blog = Articles::model()->with(array("articlesHasTags"))->findAllByAttributes(array(),array(
        "condition"=>"t.del = 0 AND t.active = 1 AND articlesHasTags.tag_id = {$tag->id}",
        'order'=>"t.publish_start DESC",
        "limit"=>10,
    )); 

在我得到$tag->id

错误消息是:

未知列'articlesHasTags.tag_id'

我认为这种关系很好,因为我可以在没有这个“ articlesHasTags.tag_id = {$tag->id} ”的情况下使用它

我将关系名称更改为表名。 然后完整的错误信息是:

CDbCommand无法执行SQL语句:SQLSTATE [42S22]:未找到列:1054'where子句'中的未知列'articles_has_tag.tag_id'。 执行的SQL语句是:SELECT tid AS t0_c0tactive AS t0_c1tpublish_start AS t0_c2tpublish_start_local AS t0_c3tcreate_time AS t0_c4tlast_modify AS t0_c5ttitle AS t0_c6turl AS t0_c7tshort_desc AS t0_c8tcontent AS t0_c9timage AS t0_c10tcomment AS t0_c11thightlight AS t0_c12tvote AS t0_c13ttype AS t0_c14tusers_id AS t0_c15tnewspaper_id AS t0_c16tdel AS t0_c17tregion_id AS t0_c18tlanguage_id AS t0_c19tadult AS t0_c20usersid AS t1_c0usersemail AS t1_c1userspassword AS t1_c2usersauthor_name AS t1_c3userslocation AS t1_c4userslast_login AS t1_c5usersactive AS t1_c6usersremember_me AS t1_c7usersrank AS t1_c8userspaypal_acc AS t1_c9usersurl AS t1_c10usersabout_me AS t1_c11usersimage AS t1_c12usersfacebook t1_c13usersgoogle AS t1_c14usersdel AS t1_c15usersadmin_active AS t1_c16userscreate_time AS t1_c17usersfirst_name AS t1_c18userslast_name AS t1_c19newspaperid AS t4_c0newspapername AS t4_c1newspapercreate_time AS t4_c2newspapertype AS t4_c3newspaperactive AS t4_c4newspaperurl AS t4_c5newspaperlocation AS t4_c6newspapernewspaper_category_id AS t4_c7 FROM articles t LEFT OUTER JOIN users users ON( tusers_id = usersid )LEFT OUTER JOIN newspaper newspaper ON( tnewspaper_id = newspaperid ),其中(t.del = 0 AND t.active = 1 AND articles_has_tag.tag_id = 42)ORDER BY t.publish_start DESC LIMIT 10

这里是articles_has_tag表

CREATE TABLE IF NOT EXISTS `articles_has_tag` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `articles_id` int(11) DEFAULT NULL,
  `tag_id` int(11) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_articles_has_tag_tag1_idx` (`tag_id`),
  KEY `fk_articles_has_tag_articles1_idx` (`articles_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=93 ;

为什么你没有使用findAll? 你把所有东西都放在条件下 第二件事在你的SQL中提到你没有关系articlesHasTags。

你可以试试这个。

$blog = Articles::model()
   ->with("articlesHasTags" => array(
       'alias' => 'articlesHasTags',
       'together' => true, //join in single query
       'condition' => "articlesHasTags.tag_id = {$tag->id}"//may be use param?
   ))->findAll(array(
       "condition"=>"t.del = 0 AND t.active = 1",
       'order'=>"t.publish_start DESC",
       "limit"=>10,
   )); 

更新:试试这个。

$blog = Articles::model()->with(array('articlesHasTags' => array(
         'alias'=>'articleHTag', 'condition' => "articleHTag.tag_id = ".$tag->id)))->findAllByAttributes(array(),array(
        "condition"=>"t.del = 0 AND t.active = 1",
        'order'=>"t.publish_start DESC",
        "limit"=>10,
    )); 
链接地址: http://www.djcxy.com/p/60857.html

上一篇: Why i cant do this query with relations in yii project?

下一篇: Bad request to the database in Yii?