Я пытаюсь создать новый плагин для обновления существующей таблицы rainlab_blog_categories
, добавив новый столбец order
.
Мне удалось обновить представление с новым полем, однако яне удалось обновить базу данных успешно.
Я использовал CLI php artisan create:plugin ZiedHf.BlogSorting
для создания каркаса подключаемого модуля.
Ниже приведены файлы: Plugin.php и файл inder каталога обновлений.
Похоже, что второй не запускает up
метод для обновления БД.
Plugin.php:
<?php namespace ZiedHf\BlogSorting;
use Backend;
use System\Classes\PluginBase;
use RainLab\Blog\Models\Category as CategoryModel;
use RainLab\Blog\Controllers\Categories as CategoriesController;
/**
* BlogSorting Plugin Information File
*/
class Plugin extends PluginBase
{
const DEFAULT_ICON = 'icon-magic';
// const LOCALIZATION_KEY = 'ziedhf.blogsorting::lang.';
const DB_PREFIX = 'ziedhf_blogsorting_';
public $require = [
'RainLab.Blog'
];
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Blog Sorting',
'description' => 'Enhance sorting for Blog',
'author' => 'Zied Hf',
'icon' => 'icon-leaf',
'homepage' => 'https://github.com/ziedhf/helloTunisia'
];
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
$this->extendController();
$this->extendModel();
}
/**
* Extend Categories controller
*/
private function extendController()
{
CategoriesController::extendFormFields(function ($form, $model) {
if (!$model instanceof CategoryModel) {
return;
}
$form->addFields([
self::DB_PREFIX . 'order' => [
'label' => 'Order',
'type' => 'number',
'comment' => 'Set the order here',
'allowEmpty' => true,
'span' => 'left'
]
]);
});
}
/**
* Extend Category model
*/
private function extendModel()
{
CategoryModel::extend(function ($model) {
$model->addDynamicMethod('getBlogSortingOrderAttribute', function() use ($model) {
return $model->{self::DB_PREFIX . 'order'};
});
});
}
}
Файл create_blog_sorting.php:
<?php
namespace ZiedHf\BlogSorting\Updates;
use Schema;
use System\Classes\PluginManager;
use ZiedHf\BlogSorting\Plugin;
use October\Rain\Database\Updates\Migration;
/**
* Class CreateBlogSorting
*
* @package ZiedHf\BlogSorting\Updates
*/
class CreateBlogSorting extends Migration
{
const TABLE = 'rainlab_blog_categories';
/**
* Execute migrations
*/
public function up()
{
if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
$this->createFields();
}
}
/**
* Rollback migrations
*/
public function down()
{
if (PluginManager::instance()->hasPlugin('RainLab.Blog')) {
$this->dropFields();
}
}
/**
* Remove new fields
*/
private function dropFields()
{
$this->dropColumn(Plugin::DB_PREFIX . 'order');
}
/**
* Create new fields
*/
private function createFields()
{
if (!Schema::hasColumn(self::TABLE, Plugin::DB_PREFIX . 'order')) {
Schema::table(self::TABLE, function ($table) {
$table->integer(Plugin::DB_PREFIX . 'order')->nullable();
});
}
}
/**
* @param string $column
*/
private function dropColumn(string $column)
{
if (Schema::hasColumn(self::TABLE, $column)) {
Schema::table(self::TABLE, function ($table) use ($column) {
$table->dropColumn($column);
});
}
}
}
Каков подходящий способ сделать это? Чего не хватает, чтобы правильно выполнить миграцию? Файл в updates / просто игнорировался при каждом запуске php artisan plugin:refresh ZiedHf.Blogsorting
или php artisan october:up
[решено] Обновление: Проблема была в version.yamlфайл. Он должен содержать обновление имя файла:
1.0.1:
- First version of BlogSorting
- create_blog_sorting.php
Спасибо!