我从哨兵处获取用户ID,在创建或更新记录时,我还需要将user_id更新到帖子。我在配置上使用了before_save()回调。Sentry::getUser()->id
工作正常(我检查了路由上的函数,它正确地提供了user_id)。在通过管理员创建帖子时,user_id未更新,因此错误。
/**
* app/config/administrator/posts.php
*/
return array(
'title' => 'Posts',
'single' => 'post',
'model' => 'Post',
/**
* The display columns
*/
'columns' => array(
'user' => array(
'title' => 'User',
'relationship' => 'user',
'select' => "(:table).email",
),
'category' => array(
'title' => 'Category',
'relationship' => 'category',
'select' => "(:table).name",
),
'pricing' => array(
'title' => 'Pricing',
'relationship' => 'pricing',
'select' => "(:table).name",
),
'name' => array(
'title' => 'Name',
'select' => "name",
),
'title' => array(
'title' => 'Title',
'select' => "title",
),
'description' => array(
'title' => 'Description',
'select' => "description",
),
'order' => array(
'title' => 'Order',
'select' => "order_value",
),
'active_date' => array(
'title' => 'Start',
'select' => "active_date",
),
'expiry_date' => array(
'title' => 'End',
'select' => "expiry_date",
),
'status' => array(
'title' => 'Status',
'select' => "status",
)
),
/**
* The filter set
*/
// 'filters' => array(
// 'name' => array(
// 'title' => 'Name',
// ),
// 'description' => array(
// 'title' => 'Description',
// 'type' => "text",
// )
// ),
'before_save' => function(&$data)
{
$data['user_id'] = Sentry::getUser()->id;
// return $data;
},
/**
* The editable fields
*/
'edit_fields' => array(
'category' => array(
'title' => 'Category',
'type' => 'relationship',
'name_field' => "name",
),
'pricing' => array(
'title' => 'Pricing',
'type' => 'relationship',
'name_field' => "name",
),
'name' => array(
'title' => 'Name',
'type' => "text",
),
'title' => array(
'title' => 'Title',
'type' => "text",
),
'description' => array(
'title' => 'Description',
'type' => "textarea",
),
'active_date' => array(
'title' => 'Start',
'type' => "date",
),
'expiry_date' => array(
'title' => 'End',
'type' => "date",
),
'status' => array(
'title' => 'Status',
'type' => "bool",
)
),
);
答案 0 :(得分:0)
假设这是管理员的错误或我的无知,我用另一种方法解决了这个问题。在帖子模型中,我创建了用于创建和更新帖子的事件:
public static function boot()
{
parent::boot();
static::creating(function($post)
{
$post->user_id = Sentry::getUser()->id;
});
static::updating(function($post)
{
$post->user_id = Sentry::getUser()->id;
});
}