yii CActiveDataProvider多表连接

时间:2014-08-02 23:58:16

标签: php mysql sql yii cactivedataprovider

我现在正在一个杂志网站上工作,我遇到了让Yii用CActiveDataProvider返回所有数据的麻烦。 SQL查询在通过SQL运行时运行正常,并且在Yii运行时我没有收到任何错误。检查日志时,通过Yii运行整个SQL语句。

public function actionIndex()
{
    $this->layout = '//layouts/column1';

    if(isset($_GET['cat']))
    {
        $cat = $_GET['cat'];
        $catname = Yii::app()->db->createCommand();
        $catname->select='category_name';
        $catname->from='category';
        $catname->where('id=:cid', array(':cid'=>$cat));
        $categoryname = $catname->queryScalar();

        $criteria = new CDbCriteria();
        $criteria->alias = 'article';
        $criteria->select ='article.id, article.title, article.content, article.date_published, image.file_name, image.alt_text, author.first_name, author.last_name';
        $criteria->join='INNER JOIN category_article ON article.id = category_article.article_id ';
        $criteria->join.='INNER JOIN image ON image.article_id = article.id ';
        $criteria->join.='INNER JOIN author ON author.id = article.author_id';
        $criteria->condition='category_article.category_id=:cid AND article.completed = 3 AND article.live = 1 AND article.review = 1';
        $criteria->params=array(':cid'=>$cat);
        $dataProvider=new CActiveDataProvider('Article', array('criteria'=>$criteria));

        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'cat'=>$cat,
            'categoryname'=>$categoryname,
        ));
        Yii::app()->end();
    }

    $dataProvider=new CActiveDataProvider('Category');
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

如果需要,以下是文章模型的关系:

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(
        'author' => array(self::BELONGS_TO, 'Author', 'author_id'),
        'articleCounts' => array(self::HAS_MANY, 'ArticleCount', 'article_id'),
        'articleNote' => array(self::HAS_ONE, 'ArticleNote', 'article_id'),
        'categories' => array(self::MANY_MANY, 'Category', 'category_article(article_id, category_id)'),
        'comments' => array(self::HAS_MANY, 'Comment', 'article_id'),
        'featureds' => array(self::HAS_MANY, 'Featured', 'article_id'),
        'image' => array(self::HAS_ONE, 'Image', 'article_id'),
        'nonmembercomments' => array(self::HAS_MANY, 'Nonmembercomment', 'article_id'),
        'tags' => array(self::MANY_MANY, 'Tag', 'postid_tagid(article_id, tag_id)'),
        'videos' => array(self::HAS_ONE, 'Video', 'article_id'),
    );
}

这是print_r打印输出:

Article Object
(
    [_new:CActiveRecord:private] => 
    [_attributes:CActiveRecord:private] => Array
        (
            [id] => 56
            [title] => Hello My Lovely
            [content] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi a arcu dictum, tincidunt libero vel, imperdiet ante. Proin consectetur risus vel purus vestibulum, nec scelerisque felis posuere. Maecenas ut erat lobortis, rhoncus tellus at, ultricies turpis. Donec pretium aliquet mi. Integer luctus interdum magna, quis dictum ligula bibendum sed. Curabitur nibh felis, sollicitudin hendrerit nunc eget, interdum fringilla leo. Cras consectetur elit metus, at tempus erat vehicula quis.



        [date_published] => 2014-07-15 23:16:07
    )

[_related:CActiveRecord:private] => Array
    (
        [image] => Image Object
            (
                [_new:CActiveRecord:private] => 
                [_attributes:CActiveRecord:private] => Array
                    (
                        [id] => 36
                        [article_id] => 56
                        [file_name] => hello-you20140624190852.jpg
                        [image_name] => Hello You
                        [alt_text] => My Hottie!
                    )

                [_related:CActiveRecord:private] => Array
                    (
                    )

                [_c:CActiveRecord:private] => 
                [_pk:CActiveRecord:private] => 36
                [_alias:CActiveRecord:private] => t
                [_errors:CModel:private] => Array
                    (
                    )

                [_validators:CModel:private] => 
                [_scenario:CModel:private] => update
                [_e:CComponent:private] => 
                [_m:CComponent:private] => 
            )

        [author] => 
    )

[_c:CActiveRecord:private] => 
[_pk:CActiveRecord:private] => 56
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
    (
    )

[_validators:CModel:private] => 
[_scenario:CModel:private] => update
[_e:CComponent:private] => 
[_m:CComponent:private] => 
)

正如您所看到的,返回查询中的所有内容,但作者仍然是空白的,我不确定原因。我确实注意到图像对象做了同样的事情,直到我将文章模型中的关系更改为HAS_ONE,这很好,因为每篇文章只有一个图像与之关联。任何人都认为它的编写方式有什么问题导致了这个问题?感谢所有的答案。

1 个答案:

答案 0 :(得分:0)

我认为'author' => array(self::BELONGS_TO, 'Author', 'author_id'),需要 'author' => array(self::BELONGS_TO, 'Author', array('author_id' => 'author_id'),

来自the docs

  

如果您需要指定自定义PK-> FK关联,您可以将其定义为数组(' fk' =>' pk')。

除非另有说明,Yii使用模型的PK作为外键。看起来图像确实使用了文章的id(article.id),作者没有(article.author_id)。