Yii Framework Forum: Cannot filter posts by tags (many to many relation) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Cannot filter posts by tags (many to many relation) Rate Topic: -----

#1 User is offline   khonggiannet 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 21-May 09

Posted 09 February 2010 - 03:56 PM

I copy here an excerpt of code in demos\blog\protected\models\Post.php from blog example in Yii 1.0
	public function relations()
	{
		return array(
			'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'),
		);
	}


This works very well in Yii 1.0. It retrieves all posts with specific tag by using an eager loading.

However, in Yii 1.1, it doesn't work (of course I changed ?? to tagFilter). Whenever I set limit in $criteria, the following code
$withOption['tagFilter']['params'][':tag']=$_GET['tag'];

....


$pages=new CPagination($postCount);
$pages->pageSize=Yii::app()->params['postsPerPage'];
$pages->applyLimit($criteria);

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

returns all post because it doesn't perform an eager loading anymore!

Is this a bug in Yii 1.1 or this method is just removed from Yii 1.1?

(In blog example of Yii 1.1, the many to many relationship is not used anymore, so we don't see the problem).

Currently, I have to work around by declaring 'join' and 'where' property for $criteria to do INNER JOIN to PostTag and Tag tables, but I hope there is a more robust way to get the same result.
Everything having a beginning has an end!
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 747
  • Joined: 02-February 09

Posted 09 February 2010 - 04:16 PM

is this the same issue as http://www.yiiframew...s-many-to-many/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users