Skip to content

3.Controller configuration

jinxing edited this page Jun 17, 2018 · 2 revisions

About controller description

Because jinxing\admin\controllers\Controller inherits from yii\web\Controller, some configuration of parent class can be used

Public attributes

Attributes Types of Defaults Description
$modelClass string ------- The data model object used
$pk string id Data table primary key field
$sort string id Default sort field
$strategy string DataTables or JqGrid Use data processing class, corresponding to front-end data table processing
$uploadFromClass string jinxing\admin\models\forms\UploadForm Upload file processing model
$strUploadPath string ./uploads/ Upload file save path

Action method

Method Description Related service methods
actionIndex() Display view file
actionSearch() Search method to provide data for the front end where() Provide query conditionsgetQuery() Provide query objectafterSearch() Data processing after query
actionCreate() Create data
actionUpdate() Change the data
actionDelete() Delete Data
actionDeleteAll() Delete multiple data
actionEditable() Edit data inline
actionUpload() File Upload afterUpload() File upload processing
actionExport() Data output where() Provide query conditionsgetQuery() Provide query objectgetExportHandleParams() Provide data export processing parameters

About the actionSearch method, the parameters of the front-end query request need to be passed through Yii::$app->request->post('params');

The public method

Method | Description getPk() | Get the primary key name

where() public method

return an array Determine the query condition processing

public function where()
$params array The query condition information passed from the front end can be used without adding this parameter.

Need to return an array, determine if the front-end query parameters, and finally spliced to db query array E.g:

    public function where()
    {
        return [
            // Simple expression query
            'id'   => '=',
            'name' => 'like',
            
            /**
             * a complicated query
             * field    Query field
             * and      Query expression
             * func     Query value processing function
             */
            'username' => [
                'field' => 'name', 
                'and'   => 'like',
                'func'  => function ($value) {
                    return $value.'123';
                }
            ],
            
            // Use anonymous functions
            'email' => function ($value) {
                return ['like', 'email', $value];
            }
           
        ];
    }

When all queries are used, the above configuration will eventually generate the following query conditions:

    $params = Yii::$app->request->post();
    $db->where([
        'and',
        ['=', 'id',  $params['id']],
        ['like', 'name', $params['name']],
        ['like', 'name', $params['username'].'123'],
        ['like', 'email', $params['email']]
    ]);

If it is a more complex query, such as linked table query, you need to override the getQuery() method.

getQuery($where) protected method

return yii\db\Query or yii\db\ActiveRecord

protected function getQuery()
$where array Process the completed query array with front-end request parameters

The default Query Query object returned by $modelClass, if it is a complex query, the method will be re-opened

    protected function getQuery($where)
    {
        return (new Query())->from('user')->leftJoin('actrive', 'user.id=active.user_id')->where($where);
    }

afterSearch(&$array) protected method

Incoming query by reference getQuery () Query the data formatted as date and time:

    protected function afterSearch(&$array) 
    {
        foreach ($array as &$value) {
            $value['created_at']  = date('Y-m-d H:i:s', $value['created_at']);
            $value['status_name'] = $value['status'] == 10 ? 'open' : 'close'; 
        }
        
        unset($value);
    }

findOne($data = []) protected method

return yii\db\ActiveRecord

By querying the parameters of the request object, no query will set the error, return false

afterUpload($object, &$strFilePath, $strField) protected method

return boolean

After the file is uploaded, the processing is mainly to crop and modify the picture.

getExportHandleParams() protected method

return an array

Mainly used to format the exported data (data that needs to be processed) and returns an array:

    protected function getExportHandleParams()
    {
        return [
            // Can only be the data that needs to be formatted, and then corresponds to an anonymous function
            'created_at' => function ($value) {
                return date('Y-m-d H:i:s', $value);
            },
            'status' => function ($value) {
                return $value == 10 ? 'open' : 'close';
            }
        ];
    }

← Module configuration description | Table configuration instructions →