Display Username With Articles/posts

Hi guys,

I have created my first Yii site using the tutorials and information here. It has a user system (using the userGroups extension) and an article system I created using the blog tutorial, if it helps the site is at ns2hub.com!

I display the username with each article on the main page, articles page and on the specific view. I can easily display the user info on the article view, as I already have the id of the article in the controller. I cannot manage to get the user info on the other pages that display more than one article though.

I do not currently understand how to get Yii to automatically attach the user info when I call the articles. I think I need to get the model relations right, is there anything else?

I will post my tables simplified and then my model relationship code.

tbl_usergroups_user

id (PK)

tbl_article

id_article (PK)

author_id

Article Model


'user' => array(self::BELONGS_TO, 'UserGroupsUser', 'author_id'),

UserGroupsUser Model


'Articles' => array(self::HAS_MANY, 'Article', 'author_id'),

I would also appreciate links to any resources that could help me to understand how this works. The categories and comments are working fine using the code automatically generated by Yii (Gii), I would love to understand how it works well enough to feel comfortable adding more into my app.

I have tried many different combinations of relationships and tried to use the guides around to get it right. Do I need to do something else too? Am I missing something? Any advice or help is appreciated!

How are you outputting each article?

Assuming you have a foreach loop or something equivalent, you just need to access the user relation of each article.




foreach ($articles as $article)

{

    echo $article->user->username; // Or whatever the name attribute is called

}



You might want to change your ‘Articles’ relation to ‘articles’ to keep your casing consistent.

Have I misunderstood your question?

Hi Keith,

Thank you so much for responding, you did not misunderstand my question at all.

The problem is that the information is not there to be used.

I get the data using:




$criteria = new CDbCriteria(array(

    'condition' => 'status=1',

    'order' => 'date_added DESC',

));


$dataProvider = new CActiveDataProvider('Article', array(

    'pagination' => array(

        'pageSize' => 10,

    ),

    'criteria' => $criteria,

));



And I display it using:




$this->widget('zii.widgets.CListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',

));



I have tried using var_dump to check what information I have. I successfuly get the Articles, along with their corresponding categories and comments, but no user data. It is infuriating and I must be missing something.

You need to modify the other view file, "_view.php". It is used by CListView to render each model.

There, you need to add a line to print the user name. See an example below:




<?php echo CHtml::encode($data->user->name); ?>



[font="Lucida Console"]$data[/font] is the variable that holds the data of the current model.

You can also check the CListView’s documentation.

Thanks for replying mentel, I think I was not clear enough.

If I can get access to the info, I can easily output it on the page. The authors info is simply not in $data, which is the problem. For whatever reason I cannot get Yii to collect the author info along with the article, category and comments.

I would really appreciate any help, I am pulling my hair out and going grey far too young!

Post your code :)

You can try adding a with parameter to your criteria.

Also, please check whether the field tbl_article.author_id for the row selected isn’t null and, if you don’t have a constraint set, whether it indeed exists in tbl_usergroups_user.

Thank you so much Rodrigo you have saved me, this works flawlessly. Thank you for showing me this, I will find it really useful in the future and you have saved me from going grey too early!

No stress, Peter. Yii is easy. Read the guide if you didn’t do it before, it will spare your hair.