I
I
iamsaint2012-03-30 15:01:41
Yii
iamsaint, 2012-03-30 15:01:41

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();

I try like this:
$article = ArticleData::model()->with('article','article.category')->findAll();
echo $article[0]->article->category->title;

But, null is returned.
Models:
<?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

1 answer(s)
M
MastaEx, 2012-03-31
@MastaEx

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 question

Ask a Question

731 491 924 answers to any question