-
Notifications
You must be signed in to change notification settings - Fork 10
3.Controller configuration
Because jinxing\admin\controllers\Controller inherits from yii\web\Controller, some configuration of parent class can be used
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 |
Method | Description | Related service methods |
---|---|---|
actionIndex() | Display view file | |
actionSearch() | Search method to provide data for the front end | where() Provide query conditions、getQuery() Provide query object、afterSearch() 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 conditions、getQuery() Provide query object、getExportHandleParams() 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');
Method | Description getPk() | Get the primary key name
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.
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);
}
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);
}
return yii\db\ActiveRecord
By querying the parameters of the request object, no query will set the error, return false
return boolean
After the file is uploaded, the processing is mainly to crop and modify the picture.
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 →