php - 在可排序网格行为中添加项目
问题描述
我使用可排序网格行为,当我尝试添加项目时,我的排序属性有问题“正在执行的 SQL 是:SELECT MAX”
public function behaviors()
{
return [
'sort' => [
'class' => SortableGridBehavior::class,
'sortableAttribute' => 'sort'
],
];
}
我在控制器中的操作
public function actionAddLibraryValue($libId)
{
$request = Yii::$app->request;
$model = new LibrariesValues();
$model->lib_id = $libId;
if ($request->isPjax && $request->isPost && $model->load($request->post()) && $model->validate()) {
try {
$model->save();
} catch (\Exception $e) {
print_r($e);exit;
}
}
}
解决方案
小部件似乎有问题,我重新定义了类方法,现在可以工作了
class CustomSortableGrid extends SortableGridBehavior
{
public function beforeInsert()
{
/** @var ActiveRecord $model */
$model = $this->owner;
if (!$model->hasAttribute($this->sortableAttribute)) {
throw new InvalidConfigException("Invalid sortable attribute `{$this->sortableAttribute}`.");
}
$query = $model::find();
if (is_callable($this->scope)) {
call_user_func($this->scope, $query);
}
/* Override model alias if defined in the model's class */
$query->from([$model::tableName()]);
$maxOrder = $query->max('{{' . trim($model::tableName(), '{}') . '}}.[[' . $this->sortableAttribute . ']]');
$model->{$this->sortableAttribute} = $maxOrder + 1;
}
}
推荐阅读
- node.js - Nodejs将图像转换为字节数组
- r - 更改闪避条形图的默认 ggplot 颜色
- javascript - 尝试从服务器执行 GET 时出现内部服务器错误 500
- react-native - 使用 Google Cloud Storage 上传预签名 URL 图片无法正确上传图片(MERN + React Native)
- node.js - WebRTC 对等到 nodejs
- python - 使用文章 ID 从 wiki API 中提取父类别 ID?
- javascript - 渲染问题
- python - 如何使用 Python 中的 Selenium 中的 setPlaybackRate 提高 YouTube 视频播放速率?
- jestjs - 如何用玩笑对 WebWorker 代码进行单元测试?
- python - Python 请求 - 隐藏“url 超出最大重试次数”