Yii Framework Forum: Count With Join - Yii Framework Forum

Jump to content

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

Count With Join Rate Topic: -----

#1 User is offline   loud 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 20-October 12

Posted 09 February 2013 - 06:22 AM

Hello In need help with count my posts in begin of forum categories list
SELECT COUNT(id) FROM posts a INNER JOIN topics b ON a.tid = b.id WHERE b.fid=$fid this is the old code,
Can I do this count in my forum model by relations or better do it in controller and how it looks in yii style?
thanks!
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 09 February 2013 - 03:04 PM

Dear Friend

Let us consider an example.

Author and Posts.

Author has many posts.

relation.
public function relations()
	{
	
		return array(
			'posts' => array(self::HAS_MANY, 'Post', 'a_id'),
		);
	}


Now we listing aurhor names.
we can also the number of posts written by him like following.
$author:Author::model()->findByPk($id);
echo count($author->posts);


We can also finetune a bit.
We want to know how many posts the author written related to YII.
$author=Author::model()->findByPk($id);
echo count($author->posts(array(
   'condition'=>'title LIKE "%yii%"',

  )));


Regards.
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