0

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

4

2 に答える 2

3

Yii のアクティブ レコードはそのようには機能しません。任意の列ではなく、テーブル スキーマで定義された列のみを取得できます。

このステートメントには、役立つと思われる例外が 1 つあります。Restaurant モデルでSTAT リレーションを定義して、そのレストランのテーブルをカウントすると、Active Record を介してそれを読み取ることができます。リンクされたページには、その良い例がいくつかあります。

于 2012-05-19T19:07:53.960 に答える
0

レストランモデルの属性rowCountを仮想属性として定義する必要があります。そうすれば、定義した CDBCriteria を介してそれを取得できます。

于 2014-04-15T05:02:38.617 に答える