Answer the question
In order to leave comments, you need to log in
Sampling from related tables?
Good afternoon.
Help, please, to understand. How to get values
by article.category link when fetching
ArticleData::model()->with('article','article.category')->findAll();
$article = ArticleData::model()->with('article','article.category')->findAll();
echo $article[0]->article->category->title;
<?php
class Category extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'TBL_CATEGORY';
}
public function relations()
{
return array(
'article'=>array(self::HAS_MANY, 'Articles','category'),
);
}
}
?>
<?php
class Articles extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'TBL_ARTICLES';
}
public function relations()
{
return array(
'data'=>array(self::HAS_MANY, 'ArticleData','parentuuid'),
'category'=>array(self::BELONGS_TO, 'Category','category'),
);
}
}
?>
<?php
class ArticleData extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'TBL_ARTICLE_DATA';
}
public function relations()
{
return array(
'article'=>array(self::BELONGS_TO, 'Articles','parentuuid'),
'language'=>array(self::BELONGS_TO, 'Languages','language'),
);
}
}?>
Answer the question
In order to leave comments, you need to log in
Well, let's play telepaths :-)
The first assumption. You have notice errors turned off and you don't see the "trying to get property of non-object..." error, which occurs because you have both a category attribute and a relation attribute of the same name in Articles.
Turn on the display of all errors.
If you didn’t guess, then look in the yii log to see which request is being executed and whether it looks like the truth.
By the way, you don't need to write with('article','article.category'), with('article.category') is enough.
error_reporting(E_ALL);
ini_set("display_errors", 1);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question