Yii Framework Forum: Yii Model Shortcut - Yii Framework Forum

Jump to content

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

Yii Model Shortcut Rate Topic: -----

#1 User is offline   nguyendh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 21-March 11

Posted 29 December 2012 - 11:32 PM

Hi guy,

I have the following code:

$criteria = new CDbCriteria;
$criteria->condition = 'ratingAdjustment = 1 AND sortOrder IS NOT NULL';
$criteria->order = 'sortOrder ASC';

$things_you_want = DC::model()->findAll($criteria);


Is there another way to do this so that I can put the logic in DC model and I call DC::model()->getThingsYouWant() for example.

In DC model file, I have

	public static function thingsYouWant()
	{
		$criteria = new CDbCriteria;
		$criteria->condition = 'ratingAdjustment = 1 AND sortOrder IS NOT NULL';
		$criteria->order = 'sortOrder ASC';
		
		return $this->findAll($criteria);
	}


and I call DC::thingsYouWant() which return
Fatal error: Using $this when not in object context in C:\Ampps\www\MPP\protected\models\DC.php on line 94


I am missing something here and I have no clues.

Thanks for helps,
I am new to Yii and this is my blog http://blog.michaelduy.com (deprecated hatadu.wordpress.com) about Yii, Git and PHP.
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,082
  • Joined: 16-February 11
  • Location:Japan

Posted 30 December 2012 - 12:36 AM

$this is not valid in a static function.
And you have to call Model::model() when ever you want to call find*() functions.
http://www.yiiframew...#reading-record
	public static function thingsYouWant()
	{
		$criteria = new CDbCriteria;
		$criteria->condition = 'ratingAdjustment = 1 AND sortOrder IS NOT NULL';
		$criteria->order = 'sortOrder ASC';
		
		return DC::model()->findAll($criteria);
	}

1

#3 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 30 December 2012 - 06:03 AM

$thingsYouWant = DC::model()->getThingsYouWant();


public function thingsYouWant()
{
  $criteria = new CDbCriteria;
  $criteria->condition = 'ratingAdjustment = 1 AND sortOrder IS NOT NULL';
  $criteria->order = 'sortOrder ASC';
                
  return $this->findAll($criteria);
}

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