Yii过滤搜索适用于一种关系但不适用于其他关系

时间:2017-07-23 13:00:52

标签: activerecord yii cgridview

我正在使用Yii GridView过滤器,我的数据来自Purcahses表。 那些表格是真的 1,.vendors 2。客户 以下是我在购买模型中的关系

 'vendor' => array(self::BELONGS_TO, 'Vendors', 'Vendor'),
 'client' => array(self::BELONGS_TO, 'Clientsinfo', 'clientID'),

我正在管理员视图中使用该关系,下面是admin.php文件的代码

$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'purchases-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
    array(
        'name' => 'Vendor',
        'value' =>'$data->vendor->VendorName',
    ),
    array(
        'name' => 'clientID',
         'value' =>'$data->client->Company'
    ),

    array(
        'name' => 'PurchaseType',
        'value' => 'Purchasetype::model()->findByPk($data->PurchaseType)->PurchaseType',
        'filter' => '',
    ),

),
));

最后这里是模型搜索功能,其中使用和一起从其他表中获取数据

public function search() {
    $criteria = new CDbCriteria;
    $criteria->together = true;
    $criteria->with = array('vendor');
    $criteria->with = array('client');
    $criteria->compare('PurchaseType', $this->PurchaseType);
    $criteria->compare('vendor.VendorName', $this->Vendor, true);
    $criteria->compare('client.Company', $this->clientID, true);
   return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

成功地从客户端获取数据,但是当我使用供应商钳工时,它会向我发出错误

CDbException

CDbCommand无法执行SQL语句:SQLSTATE [42S22]:找不到列:1054未知列  'where子句'中的'vendor.VendorName'。执行的SQL语句是:SELECT COUNT(DISTINCT tPurchaseID )FROM purchases t LEFT OUTER JOIN clientsinfo client ON({{ 1}}。t = clientIDclient)WHERE((Vendor =:ycp0)AND(vendor.VendorName LIKE:ycp1))(C:\ xampp \ htdocs \ laurenparker \骨架 \ db \ CDbCommand.php:543)

#0 C:\ xampp \ htdocs \ laurenparker \ framework \ db \ CDbCommand.php(433):CDbCommand-

  

queryInternal('fetchColumn',0,Array)

1 个答案:

答案 0 :(得分:1)

你在criteria覆盖了关系。改变这个:

$criteria->with = array('vendor');
$criteria->with = array('client');

对此:

$criteria->with = array('vendor', 'client');