$criteria->addInCondition with array data from database

Manually array that works is like this:




$ids=array(1,2,3,5,7);

$criteria= new CDbCriteria;

$criteria->addInCondition('id_project', $ids);



But how if I want to get $ids array form database?

My failed codes is like this:




$uid=Yii::app()->user->id;

$sql="SELECT DISTINCT `id_project` FROM task WHERE username='$uid'";                        

$projects=Task::model()->findAllBySql($sql);

        

foreach($projects as $project):        

    $ids=$project->id_project;

endforeach;


$criteria= new CDbCriteria;

$criteria->addInCondition('id_project', $ids);

$criteria->order='task_end ASC';

$criteria->offset=0;

$criteria->limit=10;



Thank you

I normally use:




->addCondition('id_project IN (SELECT DISTINCT id_project FROM task WHERE username=:username)');



But since now you can use ‘through’ (available since 1.1.7) and ‘scopes’ are quite powerful, make sure, none do that without that long sentence. For example, are you sure you cannot link to your the projects through the tasks?

Thanks you Antonio Ramirez, your solution is suit with my case.

I just want to get tasks list base of projects where the Manager (username) involved.

Though Antonio gave you an answer that did the trick for you, i just want to point out the mistake you were doing.

Here is the code that will work:




$uid=Yii::app()->user->id;

$sql="SELECT DISTINCT `id_project` FROM task WHERE username='$uid'";                        

$projects=Task::model()->findAllBySql($sql);

     

$ids=array();   

foreach($projects as $project):        

    $ids[]=$project->id_project;

endforeach;


$criteria= new CDbCriteria;

$criteria->addInCondition('id_project', $ids);

$criteria->order='task_end ASC';

$criteria->offset=0;

$criteria->limit=10;