2011-07-22 6 views
0

私は私の問題の答えを検索して検索しましたが、どちらもうまくいきませんでした。私の問題は、YiiのCGridViewでのフィルタリング/検索に関するものです。私の申請モデルでCGridViewフィルタ複数のモデル

私は、次のしている:私の見解では

private $_city = null; 
private $province_id = null; 
public function getCity() 
    { 
     if($this->_city === null && $this->address_id !== null) { 
      $this->_city = $this->address->city; 
     } 

     return $this->_city; 
    } 

    public function setCity($value) 
    { 
     $this->_city = $value; 
    } 

public function search() 
    { 
     // Warning: Please modify the following code to remove attributes that 
     // should not be searched. 

     $criteria=new CDbCriteria; 

     $criteria->with = array('address'=>array('alias'=>'address')); 

     $criteria->compare('id',$this->id); 
     $criteria->compare('phn',$this->phn); 
     $criteria->compare('gender',$this->gender); 
     $criteria->compare('dob',$this->dob,true); 
     $criteria->compare('first_name',$this->first_name,true); 
     $criteria->compare('last_name',$this->last_name,true); 
     $criteria->compare('band_id',$this->band_id); 
     $criteria->compare('note',$this->note,true); 
     $criteria->compare('address.city',$this->city); 
     $criteria->compare('address.province_id',$this->province_id); 

     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
      'sort'=>array(
       'attributes'=>array(
        'id', 
        'phn', 
        'first_name', 
        'last_name', 
        'band_id', 
        'city'=>array(
         'asc'=>'address.city ASC', 
         'desc'=>'address.city DESC', 
        ), 
        'province_id'=>array(
         'asc'=>'address.province_id ASC', 
         'desc'=>'address.province_id DESC', 
        ), 
       ) 
      ) 
     )); 
    } 

私は

$this->widget('zii.widgets.grid.CGridView', array(
     'id'=>'applicants-grid', 
     'dataProvider'=>$model->search(), 
     'filter'=>$model, 
     'columns'=>array(
      'first_name', 
      'last_name', 
      'phn', 
      array(
       'name'=>'band_id', 
       'value'=>'$data->band->name', 
       'filter'=>CHtml::listData(Band::model()->findAll(), 'id', 'name') 
      ), 
      array(
       'name'=>'city', 
       'value'=>'$data->address->city', // causes 'Trying to get property of non-object' - no error if $data->city 
       'filter'=>CHtml::listData(Address::model()->findAll(), 'city', 'city'), 
      ), 
      array(
       'class'=>'CButtonColumn', 
       'template'=>'{view}', 
       'buttons'=>array(
        'view'=>array(
         'url'=>'Yii::app()->createUrl("/applicant/view", array("id"=>$data->id))', 
        ) 
       ) 
      ), 
     ) 
    )); 

を持って、私は都市を持っていると示すドロップダウンリストが、私が選択したときにドロップダウンから都市です適切な結果でウィジェットをフィルタリングしません。

私はあなたの比較文の都市テーブルの主キーに探しています以下のページ

http://www.yiiframework.com/forum/index.php?/topic/11546-cgridview-with-mulitple-model/ http://www.yiiframework.com/forum/index.php?/topic/19913-cgridview-with-multiple-models/

答えて

0

で解決策をやって試してみましたか?

例えば、都市のPKは、「CITY_ID」あなたは、それぞれ、モデルとビューに以下の行を変更する必要がありますの場合:

$criteria->compare('address.city_id',$this->city); 

'filter'=>CHtml::listData(Address::model()->findAll(), 'city_id', 'city') 

は(この例では、あなたのように「都市」を残してnameはグリッドにテキストとして表示されますが、value属性のlistDataにPKのcity_idを指定してください)。

+0

いいえ都市をフィルタリングしようとしています。ユーザーが任意の都市を置くことができるので、都市のルックアップテーブルはありません。そのため、ドロップダウンリストは、ユーザーが入力した都市のリストです。 – Koralarts

+0

さて、Firebugなどを使って正しいデータが渡され、JSエラーがないことを確認します。都市名がリストに入っていれば、あなたの関係はそこではうまくいくように見えますが、PHPのコメントごとにエラーが発生しているかどうかは不明です。また、クエリのログを有効にするか、MySqlログを使用して、クエリが何をしようとしているかについてのいくつかの洞察を得ることができます。 – ldg

+0

また、都市のフィールドが空白になることがある場合は、次を参照してください。http://stackoverflow.com/questions/6775347/yii-framework-picking-up-field-value-from-other-model値を構造化する。 – ldg