Sql Statement

I have the action in the main controller:


public function actionIndex()

	{	

		$criteria = new CDbCriteria();

		$criteria->condition = 'carousel == 1';

		$carousel = Cars::model()->findAll($criteria);


		$this->render('index', array(

			'carousel' => $carousel,

			));

	}

on the condition i have the mistake:

what is wrong and how to resolve this problem.

P.S. i am newbie in YII


$criteria->condition = 'carousel = 1';

Are you sure you are looking for SQL and not AR filtering criteria?

Here is an example of SQL in AR (to get the maximum value in a column using a WHERE condition):

In controller:


$max = $model->getMax($filter);

In model:


public $maxColumn = 0;

public function getMax($filter = null)

{

	/* Get max value from column1 WHERE column2 = $filter */

	

	If($filter == null){return false;}


	$criteria		= new CDbCriteria;

	$criteria->select	= 'MAX(t.column1) as maxColumn';

	$criteria->condition	= 't.column2 LIKE :parm';

	$criteria->params	= array(':parm'=>$filter);

	

	$tempmodel = $this->find($criteria);

	$max	   = $tempmodel['maxColumn'];

	return $max;

}

this was what i needed (:

Hi Nicolae

What you wanted was Active Record filtering criteria - not SQL. :rolleyes:

Yii converts the criteria into SQL. That is why the error complained about ‘SQL’.

Enjoy!

Hi,

No need to use criteria if you are not performing any join operations. You can use like this also

Instead of

$carousel = Cars::model()->findAll($criteria);

Use

$carousel = Cars::model()->findAll(‘carousel = 1’);

It will perform the same and you will reduce the writing of code.