如何在 October Cms DB 中向现有 Table 添加新列
How to add a new Column to an exesting Table in OctoberCms DB
我正在尝试创建一个新插件,通过添加新的 order
列来更新现有的 table rainlab_blog_categories
。
我设法用新字段更新了视图,但是我无法成功更新数据库。
我使用 CLI php artisan create:plugin ZiedHf.BlogSorting
构建插件框架。
下面的文件是:Plugin.php和更新目录中的文件。
第二个好像没有运行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);
});
}
}
}
执行此操作的适当方法是什么?
正确执行迁移缺少什么?每次 运行 php artisan plugin:refresh ZiedHf.Blogsorting
或 php artisan october:up
时,updates/ 下的文件都被忽略了
[已解决]更新:
问题出在 version.yaml 文件中。
它应该包含 更新 文件名:
1.0.1:
- First version of BlogSorting
- create_blog_sorting.php
谢谢!
插件有 updates\version.yaml
文件,它将跟踪插件的所有数据库更新。
So make sure your file name [create_blog_sorting.php]
is inside updates\version.yaml
, then refresh plugin
it should add changes of migration file to database in your case it will add column :)
示例:
1.0.1:
- First version of BlogSorting
- create_blog_sorting.php <- this one :)
It should work :), make sure class name
will be in CamelCase [ CreateBlogSorting ]
.File name and entry
in updates\version.yaml
will be in snake_case [create_blog_sorting.php]
我正在尝试创建一个新插件,通过添加新的 order
列来更新现有的 table rainlab_blog_categories
。
我设法用新字段更新了视图,但是我无法成功更新数据库。
我使用 CLI php artisan create:plugin ZiedHf.BlogSorting
构建插件框架。
下面的文件是:Plugin.php和更新目录中的文件。
第二个好像没有运行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);
});
}
}
}
执行此操作的适当方法是什么?
正确执行迁移缺少什么?每次 运行 php artisan plugin:refresh ZiedHf.Blogsorting
或 php artisan october:up
[已解决]更新: 问题出在 version.yaml 文件中。 它应该包含 更新 文件名:
1.0.1:
- First version of BlogSorting
- create_blog_sorting.php
谢谢!
插件有 updates\version.yaml
文件,它将跟踪插件的所有数据库更新。
So make sure
your file name [create_blog_sorting.php]
is insideupdates\version.yaml
, thenrefresh plugin
it should add changes of migration file to database in your case it will add column :)
示例:
1.0.1:
- First version of BlogSorting
- create_blog_sorting.php <- this one :)
It should work :), make sure
class name
will be inCamelCase [ CreateBlogSorting ]
.File name and entry
inupdates\version.yaml
will be insnake_case [create_blog_sorting.php]