2012-12-20 2 views
1

저는 초보자 인 Yii와 GII를 사용하여 CURD 테이블을 만들었습니다. 모든 것은 잘 작동하지만 where 절 (예 : "클라이언트 성별이 남성 인 경우)과 같은 특정 레코드를 데이터베이스에서 불러오고 싶습니다 .GII의 생성 된 코드에서 데이터베이스에서 데이터를 가져온 위치를 찾을 수없고 필요한 위치 코드에서 WHERE 절을 삽입합니다.Gii에서 CRUD가 생성 된 곳을 추가합니다.

을 당신이 GII는 모델, 컨트롤러와 뷰 파일을 생성 아시다시피. 모델 파일은 다음과 같습니다. CGridView 사용 내보기는 CRUD 테이블을 생성 할 수 있습니다.

public static function model($className = __CLASS__) { 
    return parent::model($className); 
} 

/** 
* @return string the associated database table name 
*/ 
public function tableName() { 
    return 'test_prefixtbl_client_local'; 
} 

/** 
* @return array validation rules for model attributes. 
*/ 
public function rules() { 
    // NOTE: you should only define rules for those attributes that 
    // will receive user inputs. 
    return array(
     array('client_id', 'required'), 
     array('client_id', 'length', 'max' => 15), 
     array('surname, forename', 'length', 'max' => 20), 
     array('title', 'length', 'max' => 6), 
     array('status', 'length', 'max' => 8), 
     array('dob', 'safe'), 
     // The following rule is used by search(). 
     // Please remove those attributes that should not be searched. 
     array('client_id, surname, forename, title, status, dob', 'safe', 'on' => 'search'), 
    ); 
} 

/** 
* @return array relational rules. 
*/ 
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(
    ); 
} 

/** 
* @return array customized attribute labels (name=>label) 
*/ 
public function attributeLabels() { 
    return array(
     'client_id' => 'Client ID', 
     'surname' => 'Surname', 
     'forename' => 'Forename', 
     'title' => 'Title', 
     'status' => 'Status', 
     'dob' => 'Date of birth (yyyy-mm-dd)', 
     'actions' => 'Actions', 
    ); 
} 

/** 
* Retrieves a list of models based on the current search/filter conditions. 
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. 
*/ 
public function search() { 
    // Warning: Please modify the following code to remove attributes that 
    // should not be searched. 

    $criteria = new CDbCriteria; 

    $criteria->compare('client_id', $this->client_id, true); 
    $criteria->compare('surname', $this->surname, true); 
    $criteria->compare('forename', $this->forename, true); 
    $criteria->compare('title', $this->title, true); 
    $criteria->compare('status', $this->status, true); 
    $criteria->compare('dob', $this->dob, true); 

    return new CActiveDataProvider($this, array(
       'criteria' => $criteria, 
       'sort' => array(
        'defaultOrder' => 'dob DESC', 
       ), 
      )); 
} 

답변

1

을 두 가지 방법으로 쿼리를 할 수 있습니다. 하나는 Query Builder (tutorial here)이고 다음과 같습니다.

,210

또는이 같은 액티브 레코드 자체 (tutorial here)를 사용할 수 있습니다 :

$clientLocalArrayObjects = ClientLocal::model()->findAllByAttributes(array(
"gender" => $gender 
)); 

의심, 그냥 부탁을! :)

+0

하지만 어디에서 코드를 넣어야합니까?, 모델?, 모델/모델의 기능은 무엇입니까? – Sahil

+0

컨트롤러 동작 함수 –

+0

예, 컨트롤러 동작 함수에서 결과를 $ this-> render() 메서드에서 볼 때 사용합니다. –

관련 문제