码迷,mamicode.com
首页 > 其他好文 > 详细

Yii2 Working with Relational Data at ActiveDataProvider

时间:2015-03-09 10:31:18      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

Yii2 Working with Relational Data at ActiveDataProvider

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Recording;

/**
 * RecordingSearch represents the model behind the search form about `common\models\Recording`.
 */
class RecordingSearch extends Recording
{
    public $integerReg = ‘/^\s*[+-]?\d+\s*$/‘;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[‘book_id‘], ‘integerAndString‘],
        ];
    }
    
    public function integerAndString($attribute, $params)
    {
        if (!preg_match($this->integerReg, $this->$attribute) && !is_string($this->$attribute)) {
            $this->addError($attribute, Yii::t(‘yii‘, ‘{attribute} must be an integer or a string.‘, [‘attribute‘=>$this->getAttributeLabel($attribute)]));
        }
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        // ......
        
        if (empty($this->book_id) || preg_match($this->integerReg, $this->book_id)) {
            $query->andFilterWhere([‘book_id‘ => $this->book_id]);
        } else {
            $query->joinWith(‘book‘)->andFilterWhere([‘like‘, Book::tableName().‘.name‘, $this->book_id]);
        }

        // ......
    }
}

 

Yii2 Working with Relational Data at ActiveDataProvider

标签:

原文地址:http://www.cnblogs.com/mztest/p/4322833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!