Question

I am beginner with Yii and I used GII to create a CURD table. Everything is working fine but I only want to fetch certain record from the database by putting a where clause (for example "where client gender is male). I cannot find where the data is fetched from the database in Gii's generated code and where I need to insert the WHERE clause in the code.

As you know the Gii generated Model, controller and view file. The model file is as below. My view used CGridView to generate the CRUD table.

    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',
                ),
            ));
}
Was it helpful?

Solution

You have two ways to do your query, one using Query Builder (tutorial here) like this:

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

Or you can use the Active Record itself (tutorial here) like this:

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

Any doubts, just ask! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top