gpt4 book ai didi

php - yii CActiveDataProvider 多表连接

转载 作者:行者123 更新时间:2023-11-29 03:34:22 24 4
gpt4 key购买 nike

我现在正在一个杂志网站上工作,我在让 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,这很好,因为每篇文章只有一个图像与之关联。任何人都认为它的编写方式有任何错误会导致该问题吗?提前感谢所有答案。

最佳答案

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

来自 the docs

In case you need to specify custom PK->FK association you can define it as array('fk'=>'pk').

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

关于php - yii CActiveDataProvider 多表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25100416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com