Yii2框架学习 4-1 modelSearch类学习 自定义搜索,关联查询

Posted 没事就更

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2框架学习 4-1 modelSearch类学习 自定义搜索,关联查询相关的知识,希望对你有一定的参考价值。

1、数据提供者DataProvider,如用在modelsearch类中

 

 

 

 

 

 

 

 

 

 

<?php

namespace common\\models;

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

/**
 * PostSearch represents the model behind the search form about `common\\models\\Post`.
 */
class PostSearch extends Post
{

    //增加数据的字段属性
    public function attributes()
    {
        return array_merge(parent::attributes(), [\'author_name\']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[\'id\', \'status\', \'create_time\', \'update_time\', \'author_id\'], \'integer\'],
            [[\'title\', \'content\', \'tags\', \'author_name\'], \'safe\'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Post::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            \'query\' => $query,
             \'pagination\' => [
                \'pageSize\' => 10,
             ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where(\'0=1\');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            \'id\' => $this->id,
            \'status\' => $this->status,
            \'create_time\' => $this->create_time,
            \'update_time\' => $this->update_time,
            \'author_id\' => $this->author_id,
        ]);

        $query->andFilterWhere([\'like\', \'title\', $this->title])
            ->andFilterWhere([\'like\', \'content\', $this->content])
            ->andFilterWhere([\'like\', \'tags\', $this->tags]);

        //增加自定义查询
        $query->join(\'INNER JOIN\', \'adminuser\', \'post.author_id = adminuser.id\');
        $query->andFilterWhere([\'like\', \'adminuser.nickname\', $this->author_name]);


        //增加自定义的索引字段
        $dataProvider->sort->attributes[\'author_name\'] = [
            \'asc\' => [\'adminuser.nickname\'=>SORT_ASC],
            \'desc\' => [\'adminuser.nickname\'=>SORT_DESC],
        ];

    //设置待审核的排在前面
    $dataProvider->sort->defaultOrder = [
    \'status\' => SORT_ASC,
    \'id\' => SORT_DESC
    ];

return $dataProvider; } }

 

以上是关于Yii2框架学习 4-1 modelSearch类学习 自定义搜索,关联查询的主要内容,如果未能解决你的问题,请参考以下文章

如何将原始 SQL 转换为 Yii2,如查找查询

yii2框架添加扩展类

学习yii2.0——依赖注入

Yii系列Yii2.0基础框架

Yii2框架学习 1-2 应用主体和MVC

yii2学习笔记