I Want To Select Only Posts With Criteria " Price =1".
#2
Posted 12 January 2013 - 01:03 PM
#3
Posted 12 January 2013 - 02:41 PM
<?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
#4
Posted 13 January 2013 - 03:22 AM
alirz23, on 12 January 2013 - 02:41 PM, said:
<?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.
#5
Posted 13 January 2013 - 05:09 AM
redjohn, on 13 January 2013 - 03:22 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,
));
}
#7
Posted 13 January 2013 - 05:15 AM
alirz23, on 12 January 2013 - 02:41 PM, said:
<?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.
#8
Posted 13 January 2013 - 06:16 AM
fouss, on 13 January 2013 - 05:09 AM, said:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Post',array('criteria'=>array('condition'=>"price =1")));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
It works......Billion of thanks.
#9
Posted 13 January 2013 - 06:38 AM
redjohn, on 13 January 2013 - 06:16 AM, said:
happy to know that it helped you
#10
Posted 13 January 2013 - 08:20 AM
Quote
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

Help













