Please help me figure out what am I doing wrong here:
In search function in one of my models I have:
$registered_sql="(select count(cTeilnehmerID) from buchung b where b.cSchulungsTerminID=t.cSchulungsTerminID)";
$criteria->select=array('*', $registered_sql.' as registered');
This works fine, but if I do:
$registered_sql=Buchung::model()->count('cSchulungsTerminID=:number',array(':number'=>$this->cSchulungsTerminID));
$criteria->select=array('*', $registered_sql.' as registered');
the "registered" column in view returns 0 for all records…where is the problem?
In the former the $registered_sql is a aql. So it will be executed as a sub query for every row.
But in the latter $registered_sql is not a sql. CActiveRecord::count() doesn’t return a sql string, but the count number itself as a string.
It’s just a count of rows whose cSchulungsTerminID is something that you have in $this->cSchulungsTerminID. Note that $this refers to the model that has the search parameters. When you do not specify cSchulungsTerminID in the search, then $this->cSchulungsTerminID should be empty, and you’ll get “0” for the $registered_sql.