在我的插件中,当我编辑和保存项目时,某些字段值不会刷新。
例如,即使已在数据库中进行更新,“ updated_at”字段仍显示旧的DateTime值。
如何刷新特定的字段值?
我应该使用局部变量还是有另一种方法?
答案 0 :(得分:2)
在FormController中使用AJAX处理程序时,可以使用以下方法更新表单中的单个字段:
public function onYourAjaxHandler($recordID)
{
$model = MyModel::findOrFail($recordID);
$model->fieldToUpdate = "new value";
$this->initForm($model);
$fieldMarkup = $this->formGetWidget()->renderField('fieldToUpdate', ['useContainer' => true]);
return [
'#field-id' => $fieldMarkup
];
}
如果需要替换字段容器,请将useContainer =>设置为false。如果需要保留容器,请设置useContainer => true。
从OctoberCMS v452开始,现在可以直接使用formRenderField()方法,因为已向其中添加了“ options”参数:
$this->formRenderField('fieldToUpdate', ['useContainer'=>false])
答案 1 :(得分:0)
嗯,您需要将模型的certain attributes/variables
设置为achieve
。
class YourModel extends Model
{
// set this to true
public $timestamps = true;
}
$ timestamps:如果
true
将automatically
设置 created_at 和 updated_at 字段。 (确保您的表具有与 created_at 和 updated_at 相同的相同列名)
检查参考:https://octobercms.com/docs/database/model#standard-properties
如有任何疑问,请发表评论。