2012-01-27 4 views
3

Yii 1.1 응용 프로그램 개발 요리 책은 관련 Active Record Models의 데이터를 사용하여 관련 모델을 검색하는 방법도 설명합니다. 이 방법은 193 페이지와 194 페이지에 설명되어 있습니다.이 메서드를 내 응용 프로그램에 통합하려고했지만 작동하지 않습니다. 아무도 나 에게이 기능은 여전히 ​​Yii 프레임 워크 버전 1.1.8에서 사용할 수 있는지 설명 할 수Yii 프레임 워크 : 검색을 위해 관련 Active Record 모델의 데이터 사용

이 위치에서 또한 데이터 폼 관련 활성 레코드 모델 검색에 대한 의견을 찾을 수 있습니다. 그러나 그것은 또한 작동하지 않습니다. http://www.yiiframework.com/doc/api/1.1/CDbCriteria

내가 주문 테이블과 사용자 테이블이

주문 테이블과 사용자 테이블 많은 관계로 하나가 있습니다.

사용자의 주문량이 많고 주문에 정확히 한 명의 사용자가 있습니다.

그래서 CDbCriterial을 편집하여 사용자 테이블 이름과 전자 메일 필드를 주문 테이블 검색 항목에 포함시킵니다.

주문 테이블은 다음과 같은 관계

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'comments' => array(self::HAS_MANY, 'Comment', 'order_id'), 
     'user' => array(self::BELONGS_TO, 'User', 'user_id'), 
     'orderstatus' => array(self::BELONGS_TO, 'Orderstatus', 'orderstatus_id'), 
     'commentCount' => array(self::STAT, 'Comment' , 'order_id') 
    ); 
} 

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

     $criteria=new CDbCriteria; 

     $criteria->compare('id',$this->id); 
     $criteria->compare('order_create_date',$this->order_create_date,true); 
     $criteria->compare('price',$this->price,true); 
     $criteria->compare('bank_account_number',$this->bank_account_number,true); 
     $criteria->compare('hardwaredetail_Id',$this->hardwaredetail_Id); 
     $criteria->compare('user_id',$this->user_id); 
     $criteria->compare('order_update_date',$this->order_update_date,true); 
     $criteria->compare('is_received',$this->is_received); 
     $criteria->compare('order_received_date',$this->order_received_date,true); 
     $criteria->compare('is_notify_by_email',$this->is_notify_by_email); 
     $criteria->compare('warehouse_offered_price',$this->warehouse_offered_price,true); 
     $criteria->compare('warehouse_offered_price_date',$this->warehouse_offered_price_date,true); 
     $criteria->compare('orderstatus_id',$this->orderstatus_id); 
     $criteria->together = true; 
     $criteria->with = array('user'); 
     $criteria->compare('user.name',$this->user,true); 
     //$criteria->compare('user.name',$this->user->name); 
     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

및 주문 관리 페이지에 포함

을 다음과 같이 제기 이름을 표시 편집 제기 사용자 테이블의 이름을 검색/필터 조건입니다있다
<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid', 
    'dataProvider'=>$model->search(), 
    //'filter'=>$model, 
    'columns'=>array(
     'id', 
     'order_create_date', 
     'price', 
     'bank_account_number', 
     array(
      'name'=>'user', 
      'value'=>'$data->user->name' 
     ), 
     ), 
)); 

오류 메시지가 반환되었습니다.

enter image description here

thaddeusmt 내가 다음과 같은 오류 메시지가 직면 한 준 솔루션을 적용하여 ID 열 모호성 문제를 해결 한 후. 어떤 도움 모두 userorder이 열 id 명명 한 것 같습니다

+0

코드를 게시 할 수 있습니까? 적어도 샘플 .. –

+0

요청한대로 코드를 추가했습니다 – KItis

+0

오류가 발생 했습니까? 그리고 "public $ user;"를 추가했습니다. 첫 줄의 순서 클래스 모델? – user677900

답변

11

답변을 찾을 수 있습니다. 이것이 내가 한 전부입니다.

다음 두 테이블이 있습니다. 주문 및 사용자

enter image description here

I 주문/관리 페이지가, 기본 코드는 주문 테이블 데이터 만 검색 제공 생성. 나는 검색 기준에 사용자 테이블 이름 필드를 관련시키고 싶다.

이것은 검색 페이지의 초기 모습입니다.

enter image description here

나는이 검색에 다른 테이블에서 제기 통합 된 사용자 이름을합니다. 마지막 모습은 다음과 같습니다.

enter image description here

그래서이 내가 한 단계입니다. 주문 모델에서 처음

난 다음 한 관계

public function relations() 
     { 
      // NOTE: you may need to adjust the relation name and the related 
      // class name for the relations automatically generated below. 
      return array(

       'user' => array(self::BELONGS_TO, 'User', 'user_id'), 


      ); 
     } 
This is generated by Yii framework , i did nothing :) 

Then , i changed the search method as follows 

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

     $criteria=new CDbCriteria; 


     $criteria->compare('t.order_create_date',$this->order_create_date,true); 
     $criteria->compare('t.price',$this->price,true); 
     $criteria->compare('t.bank_account_number',$this->bank_account_number,true); 
     $criteria->compare('t.hardwaredetail_Id',$this->hardwaredetail_Id); 
     //$criteria->compare('user_id',$this->user_id); 

     $criteria->compare('t.order_update_date',$this->order_update_date,true); 
     $criteria->compare('t.is_received',$this->is_received); 
     $criteria->compare('t.order_received_date',$this->order_received_date,true); 
     $criteria->compare('t.is_notify_by_email',$this->is_notify_by_email); 
     $criteria->compare('t.warehouse_offered_price',$this->warehouse_offered_price,true); 
     $criteria->compare('t.warehouse_offered_price_date',$this->warehouse_offered_price_date,true); 
     $criteria->compare('t.orderstatus_id',$this->orderstatus_id); 
     $criteria->together = true; 
     $criteria->compare('t.id',$this->id,true); 
     $criteria->with = array('user'); 
     $criteria->compare('name',$this->user,true,"OR"); 
     return new CActiveDataProvider($this, array(
      'criteria'=>$criteria, 
     )); 
    } 

는 모두 이름 저장이있는 경우에-정면 t 경우 주문 테이블의 기본 키 필드의 t을 넣어하는 것이 중요하다. 제 경우에는 이드와 이드입니다. 그래서 저는 t를 써야했습니다.

다른 점은 = 진정한위한 것일 요소

$ criterial->의 순서입니다; 관계형 요소 앞에 와야합니다.

그러면 주문 테이블의 규칙 방법으로 업데이트되었습니다. 나는 사용자 필드를 추가하고 이름을 안전한 속성에 보관했습니다.

public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      //array(' orderstatus_id', 'required'), 
      array('hardwaredetail_Id, user_id, is_received, is_notify_by_email, orderstatus_id', 'numerical', 'integerOnly'=>true), 
      array('price, warehouse_offered_price', 'length', 'max'=>10), 
      array('bank_account_number', 'length', 'max'=>100), 
      array('order_create_date, order_update_date, order_received_date, warehouse_offered_price_date, user,name', 'safe'), 
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('id, order_create_date, price, bank_account_number, hardwaredetail_Id, user_id, order_update_date, is_received, order_received_date, is_notify_by_email, warehouse_offered_price, warehouse_offered_price_date, orderstatus_id', 'safe', 'on'=>'search'), 
     ); 
    } 

마지막으로 UI 코드를 업데이트하십시오.

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'order-grid', 
    'dataProvider'=>$model->search(), 
    'filter'=>$model, 
    'columns'=>array(
     'id', 
     'order_create_date', 
     'price', 
     'bank_account_number', 
     array(
      'name'=>'user', 
      'value'=>'$data->user->name' 
     ) 
)); ?> 

은 내가했고, 그것은 나를 위해 일한 것입니다

array(
       'name'=>'user', 
       'value'=>'$data->user->name' 
      ) 

와 주문 관리를 업데이트했습니다. 어떤 도움이 필요하면 저에게 물어보십시오. 이 문제를 조사해 주신 모든 분들께 감사드립니다.

+1

수정되었으므로 'user'를 사용할 수 있다고 생각하지 않았습니다. 컬럼 이름 -'$ model-> attributes'가'search()'메소드의 데이터를 설정하지 않을 것이라고 생각했습니다. 매일 새로운 것을 배우십시오! – thaddeusmt

+0

추가 된 사용자 필드와 이름을 안전한 속성에 추가하여 세이프 온 검색 규칙에 추가 할 수 있습니다. –

+0

빠른 검색 (아약스)이 작동하지 않습니다. 이상하지만 진정한 고급 검색 작업. 왜? – jasmines

2

에 미리

enter image description here

감사합니다. 그리고 귀하의 기준은 둘 다 WHERE 절에서 "모호한"mySql 오류 메시지를 제공합니다.

당신의 두 테이블이 같은 이름의 열이있는 경우, 당신은 조건에 MySQL의 "점"접두사를 사용과 같이 필요 (A SQL은 테이블의 조인 않음) with 기준을 사용하는 경우 :

$criteria->compare('t.id',$this->id); // the default table prefix in Yii is "t" 

$criteria->with을 사용하여 테이블을 조인 할 때 모든및 condition 기준 등의 모든 열 이름 앞에 접두어를 붙입니다.그래서 같은 :

$criteria->compare('t.id',$this->id); // t 
$criteria->compare('t.order_create_date',$this->order_create_date,true); // t 
$criteria->with = array('user'); 
$criteria->compare('user.name',$this->user_id,true); // user (the filter will set user_id) 

귀하의 gridview는 다음과 같이해야합니다 또한

array(
    'name'=>'user_id', 
    'header'=>'User', 
    'sortable'=>false, 
    'value'=>'$data->user->name' 
), 

, 나는 더 큰 문제가 있다고 생각, 당신은 당신의 편집과 지적과 같이

귀하 검색 기능은 다음과 같이 설정됩니다. user.name',$this->user - 그러나 $this->user은 검색 기준이 아닌 관계를 통해 현재 사용자 객체를 반환합니다. 열 필터는 user_id 속성을 설정합니다.

편집 : 아니, 당신 수 너무 오래 당신이 safe 속성으로 설정대로 열 이름으로$this->user.

나는이 문제를 얻고 방법

은 여기에서 더 자세히 표시됩니다 :

Search in BELONGS_TO model column with CGridView, Yii

분류는하지만 작동하지 않습니다 - 단지 필터링.

여기 너무, 당신에게 더 많은 단서를 제공 수있는 YII 포럼에서 좋은 게시물입니다 :

http://www.yiiframework.com/forum/index.php?/topic/9083-search-filter-of-a-relations-field-through-cgridview/

슬프게도, 정렬 및 관계에 CGridViews를 필터링하는 것은 정말 기본 기능이 아닙니다. user.name과 같은 열 이름으로 관련 정보를 쉽게 표시 할 수 있지만 정렬이나 필터링은 수행하지 않습니다. 행운을 빕니다!

+0

오류 메시지와 함께 질문을 업데이 트했습니다. 새로운 오류가 나타납니다. 질문을 새로운 오류 메시지로 업데이트했습니다. – KItis

+0

답을 thaddessmt 고맙다, 나는 문제를 해결할 수 있었다. 당신의 솔루션은 많은 도움이되었습니다. – KItis

+0

좀 더 자세한 설명으로 업데이트되었습니다. 도움이되기를 바랍니다. – thaddeusmt