【问题标题】:Adding where close on Gii generated CRUD在 Gii 上添加 where close 生成的 CRUD
【发布时间】:2023-03-06 14:23:01
【问题描述】:

我是 Yii 的初学者,我使用 GII 创建了一个 CURD 表。一切正常,但我只想通过放置 where 子句从数据库中获取某些记录(例如“客户性别为男性的地方)。我无法在 Gii 生成的代码中找到从数据库中获取数据的位置以及我需要的位置在代码中插入 WHERE 子句。

如您所知,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',
                ),
            ));
}

【问题讨论】:

    标签: php mysql yii gii yii-cmodel


    【解决方案1】:

    您有两种查询方式,一种是使用查询生成器 (tutorial here),如下所示:

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

    或者您可以像这样使用 Active Record 本身 (tutorial here):

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

    有任何疑问,尽管问! :)

    【讨论】:

    • 但是我需要将这些代码放在哪里?,在模型中?,然后在模型的哪个方法/函数中?
    • 在控制器动作函数中
    • 是的,在控制器动作函数中,并将结果传递给$this->render()方法的视图中使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 2016-09-15
    • 2016-02-17
    • 2016-03-14
    相关资源
    最近更新 更多