Filter results in ar query with WHERE statement

Hey,

Now I have a query that retrieves a project with related tasks:


$project = Project::model()->with('task')->findByPk($_GET['project_id']);

But next what I wanna do is get only those tasks, that have field REF_MILESTONE IS NULL.

What should I change in the query, or what is the best way to achieve this?

I’m expecting something like


$project = Project::model()->with('task')->where('REF_MILESTONE IS NULL')->findByPk($_GET['project_id']);

but of course it isn’t working.

Thanks in advance.

You can use:




$project = Project::model()->with('task')->findAll('REF_MILESTONE IS NULL');



or you can use a criteria:




$criteria= new CDbCriteria;

$criteria->condition='REF_MILESTONE IS NULL';

$project = Project::model()->with('task')->findAll($criteria);



Take a look at the documentation of CActiveRecord for a complete list of parameters.

Thanks, it might be a good solution, but I have to specify the project id. So this will not work, unless there is a way to specify a project id too in that line of code.

I know about criteria, but I wanted to find the solution whithout creating CDbCriteria object.

I have temporary solved my problem by adding named scope:




$project = Project::model()->with('task:nomilestone')->findByPk($_GET['project_id']);


// in Task model:

public function scopes()

{

    return array(

        'nomilestone'=>array(

            'condition'=>'REF_MILESTONE IS NULL',

        ),

    );

}