Favourite posts

Hello, I’m trying to create favourite posts, but I have problem with the listing. The favourites are in table fav with columns fav_id, user_id and post_id.

In the post controller I have that function:


public function actionFav()

	{

		$criteria=new CDbCriteria;

		$withOption=array('post');

		$postCount=Fav::model()->count($criteria);	

		$pages=new CPagination($postCount);

		$pages->pageSize=Yii::app()->params['postsPerPage'];

		$pages->applyLimit($criteria);


		$posts=Fav::model()->with($withOption)->findAll($criteria);

		///print_r($posts);

		$this->render('list',array(

			'posts'=>$posts,

			'pages'=>$pages,

		));

	}

And the Fav model is:


<?php


class Fav extends CActiveRecord

{

	public $title, $id, $authorId, $createTime, $contentDisplay, $tags, $commentCount, $updateTime;

	

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	public function tableName()

	{

		return 'Fav';

	}

	

	public function relations()

	{

		return array(

			'post'=>array(self::BELONGS_TO, 'Post', 'post_id', 'order'=>'??.createTime'),

			'author'=>array(self::BELONGS_TO, 'User', 'authorId'),

			'comments'=>array(self::HAS_MANY, 'Comment', 'postId', 'order'=>'??.createTime'),

			'tagFilter'=>array(self::MANY_MANY, 'Tag', 'PostTag(postId, tagId)',

				'together'=>true,

				'joinType'=>'INNER JOIN',

				'condition'=>'??.name=:tag'),

		);

	}

	

	public function getTagArray()

	{

		return Post::getTagArray();

	}

	

	public function safeAttributes()

	{

		return array(

			'user_id',

			'post_id',

		);

	}

}

The massive posts has the right elements, but they are empty. In the private part of the array, there are the right elements, but I don’t know how to reach them. There is an example of array:

So, if anyone knows how to deal with that… :)

Try this




foreach ($posts as $fav)

  echo $fav->post->some_attribute;



As a side note, safeAttributes does not exist in Yii 1.1, you just have to add any validation rule to make massive assignment work.

/Tommy

Wow, thank you :)

You are not the first one who told me about safeAttributes, but I’ll remove them later :).

Thank you again.