mirror of
https://github.com/flarum/framework.git
synced 2024-11-29 04:33:47 +08:00
Do not use model classes in the migrations
The model classes encapsulate knowledge about the database that may change. That knowledge may be table names, or the value of constants (such as `Group::MEMBER_ID` in this case). Models may even disappear after a while due to refactorings. In all these cases, this migration would break. Thus, we use Laravel's query builder features which makes building queries almost as easy as using the model - but correct in all cases. :-)
This commit is contained in:
parent
10e929f2dc
commit
95ea40a7c4
|
@ -8,27 +8,23 @@
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Flarum\Core\Group;
|
use Illuminate\Database\ConnectionInterface;
|
||||||
use Flarum\Core\Permission;
|
|
||||||
|
|
||||||
$getPermissionAttributes = function () {
|
$permissionAttributes = [
|
||||||
return [
|
'group_id' => 3, // Default group ID of members
|
||||||
'group_id' => Group::MEMBER_ID,
|
'permission' => 'discussion.likePosts',
|
||||||
'permission' => 'discussion.likePosts',
|
];
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'up' => function () use ($getPermissionAttributes) {
|
'up' => function (ConnectionInterface $db) use ($permissionAttributes) {
|
||||||
Permission::unguard();
|
$instance = $db->table('permissions')->where($permissionAttributes)->first();
|
||||||
|
|
||||||
$permission = Permission::firstOrNew($getPermissionAttributes());
|
if (is_null($instance)) {
|
||||||
|
$db->table('permissions')->insert($permissionAttributes);
|
||||||
$permission->save();
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'down' => function () use ($getPermissionAttributes) {
|
'down' => function (ConnectionInterface $db) use ($permissionAttributes) {
|
||||||
|
$db->table('permissions')->where($permissionAttributes)->delete();
|
||||||
Permission::where($getPermissionAttributes())->delete();
|
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user