I am using the CDbCriteria to join two tables restaurants and tables. A restaurant can have multiple tables. I want to get the list of restaurants along with the number of tables in that particular restaurant.
I am using the following code :
$criteria = new CDbCriteria();
$criteria->select = "t.*, COUNT(t2.id) as rowCount";
$criteria->group = "t2.restaurantId";
$criteria->join = "LEFT JOIN {{tables}} t2 ON t.id = t2.restaurantId";
$criteria->condition = "t.clientId = ".Yii::app()->user->clientId;
$restaurants = Restaurants::model()->findAll($criteria);
When I try to display the value of rowCount it is showing Property "Restaurants.tableCount" is not defined. Also please tell me if this is the correct method of doing things in Yii as I am new to Yii
Yii’s Active Record doesn’t work that way, it can only retrieve columns that are defined in the table schemas, not arbitrary ones.
There is one exception to that statement which you might find useful – a STAT relation can be defined on the Restaurant model to count tables in that restaurant, and then you will be able to read it via Active Record. The linked page has a few good examples of that.