2012-12-20 31 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

你有兩種方法來做你的查詢,一種是使用這樣的查詢生成器(tutorial here):

$clientLocalArray = Yii::app()->db->createCommand() 
->select() 
->from("test_prefixtbl_client_local") 
->where("gender = :gender", array(":gender"=>$gender)) 
->queryAll(); 

或者你可以用這樣的活動記錄本身(tutorial here):

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

的任何問題,只是問! :)

+0

但是,我需要將這些代碼放在?,模型?中,然後在模型的哪個方法/函數中? – Sahil

+0

在控制器中的動作功能 –

+0

是的,在控制器的動作函數中,並傳遞結果以在$ this-> render()方法中使用。 –