首页 >后端开发 >php教程 >Yii2中activerecord与activecontroller的示例详解

Yii2中activerecord与activecontroller的示例详解

黄舟
黄舟原创
2017-08-23 10:14:241578浏览

一些补充

activecontroller的各种action是可以重载的,但必须先要unset掉父类的方法。

比如下面这样,注意里面的actions()方法,如果这里没有unset掉父类方法的话,重载的function actionXXX不会起作用。


<?php

namespace app\modules\rest\v2\controllers;

use Yii;
use yii\data\ActiveDataProvider;
use app\models\db\Task;
use app\models\db\TaskSearch;

class TaskController extends BaseActiveController
{
    public $modelClass = &#39;app\models\db\Task&#39;;

    public function actions()
    {
        $actions = parent::actions();
        unset($actions[&#39;index&#39;]);

        return $actions;
    }

    public function actionIndex()
    {
        $searchModel = new TaskSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $dataProvider;
    }
}

这里调用的TaskSearch类仿照了gii生成的ModelSearch类,TaskSearch类的代码


<?php

namespace app\models\db;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\db\Task;

class TaskSearch extends Task
{
    public $keyword;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[&#39;projectId&#39;, &#39;keyword&#39;], &#39;string&#39;],
            [&#39;main_type&#39;, &#39;in&#39;, &#39;range&#39; => self::getEnumColumnValues(&#39;main_type&#39;)],
            [&#39;type&#39;, &#39;in&#39;, &#39;range&#39; => self::getEnumColumnValues(&#39;type&#39;)],
            [&#39;_status&#39;, &#39;in&#39;, &#39;range&#39; => self::getEnumColumnValues(&#39;_status&#39;)],
            [&#39;publish_status&#39;, &#39;in&#39;, &#39;range&#39; => self::getEnumColumnValues(&#39;publish_status&#39;)],
        ];
    }

    /**
     * @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)
    {
        $this->load($params, &#39;&#39;);

        if (!$this->validate()) {
            return (new \yii\rest\Serializer())->serialize($this);
        }

        $query = Task::find();

        $dataProvider = new ActiveDataProvider([
            &#39;query&#39; => $query,
            &#39;pagination&#39; => [
                &#39;defaultPageSize&#39; => self::DEFAULT_PAGE_SIZE,
                &#39;pageSizeParam&#39; => &#39;page_size&#39;,
                &#39;pageSizeLimit&#39; => [0, 100],
            ],
        ]);

        $query->andFilterWhere([
            &#39;projectId&#39; => $this->projectId,
            &#39;main_type&#39; => $this->main_type,
            &#39;type&#39; => $this->type,
            &#39;_status&#39; => $this->_status,
            &#39;publish_status&#39; => $this->publish_status,
        ]);
        $query
            ->andFilterWhere([&#39;like&#39;, &#39;name&#39;, $this->keyword]);

        return $dataProvider;
    }

}

这里面的

$this->load($params, &#39;&#39;);

需要加上第二个参数,因为和activeform的表单提交不同,这里的输入只是一维数组而不是两维。

&#39;defaultPageSize&#39; => self::DEFAULT_PAGE_SIZE

默认的每页记录数(querystring内没有提供的情况下),如果没有这行,Yii2给的默认值是20

&#39;pageSizeParam&#39; => &#39;page_size&#39;

每页记录数的querystring名称,默认是'per-page'。

&#39;pageSizeLimit&#39; => [0, 100]

如果page_size=0表示无分页限制,取出所有的纪录。这个值默认是[1, 50],防止page_size过大影响性能。

if (!$this->validate()) {
    return (new \yii\rest\Serializer())->serialize($this);}

这一段可以直接格式化输出错误内容,并且将Http status code改成4xx(表示客户端错误)。

使用的时候发现一些问题

mongodb的objectId并不是纯数字,put/get one/delete方法会报404错误,所以yii的路由规则要加上一段,类似:


[&#39;class&#39; => &#39;yii\rest\UrlRule&#39;, &#39;controller&#39; => [&#39;rest/v2/tasklogs&#39; => &#39;rest-v2/task-log&#39;], &#39;tokens&#39; => [&#39;{id}&#39; => &#39;<id:\\w[\\w,]*>&#39;]],

 

 存日期类型的话要像下面一样


$this->_created = new \MongoDate(); // yii2-mongodb v2.0.x

$this->_created = new \Mongodb\BSON\UTCDateTime(round(microtime(true))*1000); // yii2-mongodb v2.1.x

输出显示的时候


$this->_created = $this->_created->toDateTime()->setTimezone(new \DateTimeZone(&#39;Asia/Shanghai&#39;))->format("Y-m-d H:i:s");

 

attributes和rules方法,attributes表示查询后输出显示的字段,rules里面控制的是输入验证的字段:


    /**
     * @inheritdoc
     */
    public function attributes()
    {
        return [&#39;_id&#39;, &#39;message&#39;, &#39;data&#39;, &#39;_created&#39;, &#39;_updated&#39;];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[&#39;message&#39;, &#39;data&#39;], &#39;safe&#39;],
        ];
    }

page_size值等于0的时候,只返回一条记录,而不是所有,这里有个bug

yii2-mongodb/Command.php下面(目前版本v2.1.2),603行

if ($options[&#39;limit&#39;] === null) {

应该改成

if ($options[&#39;limit&#39;] === -1) {

以上是Yii2中activerecord与activecontroller的示例详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn