Query using CDbCriteria returns array when object needed

Hello,

With a model called Task, I have the following line of code in my controller:





$criteria = new CDbCriteria();

// build the $criteria


$tasks = Task::model()->findAll($criteria);



which works perfectly. However, when I am trying to create a form in my view using:




<?php $form = $this->beginWidget('CActiveForm', array(

    'id'=>'addtask',

    'enableAjaxValidation'=>true,

    'enableClientValidation'=>true,

    'focus'=>array($tasks,'name'),

)); ?>


<?php echo $form->errorSummary($tasks); ?>




it complains that:




get_class() expects parameter 1 to be object, array given



And it refers to the variable $tasks.

Is there anyway to use $criteria in another form of query that the $tasks variable, which is then used in the view, would be an object instead of an array, so that I could use the CActiveForm widget to create the suitable form to update the Task model?

Thanks!

Remove this:




$criteria = new CDbCriteria();

// build the $criteria


$tasks = Task::model()->findAll($criteria);



And use this:




$tasks = new Task;



‘findAll()’ returns an array of model objects.

‘find()’ instead of ‘findAll()’ might be a solution, because it will return only the first model object that matches the criteria.

Are you sure that your criteria always matches one model at a time?