Yii Framework Forum: I Want To Select Only Posts With Criteria " Price =1". - Yii Framework Forum

Jump to content

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

I Want To Select Only Posts With Criteria " Price =1". Rate Topic: -----

#1 User is offline   redjohn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-January 13

Posted 12 January 2013 - 10:37 AM

how to do that please help...

this is my controller

http://pastebin.com/uFvkKjNc
0

#2 User is offline   iainjames88 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 13-December 11

Posted 12 January 2013 - 01:03 PM

I think this is what you could be looking for CActiveRecord::findAllByAttributes()
0

#3 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,032
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 12 January 2013 - 02:41 PM

There are many ways to approach this problem take look at this might help you

<?php
// first approach
... 
$criteria = new CDbCriteria;
$criteria->compare("price", 1);
$posts = Post::model()->find($criteria);
... 
 
// second approach
... 
$posts = Post::model()->find(array('price'=>1));
... 


https://gist.github.com/4520140
0

#4 User is offline   redjohn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-January 13

Posted 13 January 2013 - 03:22 AM

View Postalirz23, on 12 January 2013 - 02:41 PM, said:

There are many ways to approach this problem take look at this might help you

<?php
// first approach
... 
$criteria = new CDbCriteria;
$criteria->compare("price", 1);
$posts = Post::model()->find($criteria);
... 
 
// second approach
... 
$posts = Post::model()->find(array('price'=>1));
... 


https://gist.github.com/4520140

doesn't work, in which functions must put this code, what variable must have that function. Exists a clear example.
0

#5 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 13 January 2013 - 05:09 AM

View Postredjohn, on 13 January 2013 - 03:22 AM, said:

doesn't work, in which functions must put this code, what variable must have that function. Exists a clear example.

For exemple in your PostsController change your actionIndex() this way
	public function actionIndex()
	{       

                
		$dataProvider=new CActiveDataProvider('Post',array('criteria'=>array('condition'=>"price =1")));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

Posted Image
1

#6 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 13 January 2013 - 05:11 AM

And don't forget to learn about CActiveDataProvider
Posted Image
0

#7 User is offline   Dariusz Męciński 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 04-March 09

Posted 13 January 2013 - 05:15 AM

View Postalirz23, on 12 January 2013 - 02:41 PM, said:

There are many ways to approach this problem take look at this might help you

<?php
// first approach
... 
$criteria = new CDbCriteria;
$criteria->compare("price", 1);
$posts = Post::model()->find($criteria);
... 
 
// second approach
... 
$posts = Post::model()->find(array('price'=>1));
... 


https://gist.github.com/4520140


second approach is not valid
it should be:
$posts = Post::model()->findByAttributes(array('price'=>1));

or
$posts = Post::model()->find('price = :price, 'array(':price'=>1));

or
$posts = Post::model()->find(array('condition'=>'price = :price', 'params'=>array(':price'=>1)));


as say api for find function:
If an array, it is treated as the initial values for constructing a CDbCriteria object; Otherwise, it should be an instance of CDbCriteria.

0

#8 User is offline   redjohn 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 10-January 13

Posted 13 January 2013 - 06:16 AM

View Postfouss, on 13 January 2013 - 05:09 AM, said:

For exemple in your PostsController change your actionIndex() this way
	public function actionIndex()
	{       

                
		$dataProvider=new CActiveDataProvider('Post',array('criteria'=>array('condition'=>"price =1")));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


It works......Billion of thanks.
0

#9 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 13 January 2013 - 06:38 AM

View Postredjohn, on 13 January 2013 - 06:16 AM, said:

It works......Billion of thanks.

happy to know that it helped you ;)
Posted Image
0

#10 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,032
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 08:20 AM

@kazio

Quote

second approach is not valid
it should be:
$posts = Post::model()->findByAttributes(array('price'=>1));


mybad It was late in the night sleep was hitting me bad thanks for the correction
0

#11 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,032
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 13 January 2013 - 08:28 AM

redjohn u must read the blog tutorial
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