Hi,
as described in Module development guide (http://kooboo.com/Documents/Detail/CMS/v3/Module-development-guide) you can use installer/uninstaller controller to create/drop database tables.
Ok so I've written some code to do this, here my InstallerController actions:
[HttpPost]
public ActionResult Install(string moduleName, FormCollection form)
{
JsonResultEntry result = new JsonResultEntry() { RedirectToOpener = false };
var parentUrlHelper = new UrlHelper(PageControllerContext.RequestContext);
result.RedirectUrl = parentUrlHelper.Action("InstallationCompleted", new { controller = "ModuleManagement", ModuleName = moduleName, Message = "" });
#region Create Schema
SchemaManager schemaManager = new SchemaManager();
Schema schema = new Schema(Repository.Current, "Schema1");
Column col = new Column()
{
Name = "Column1",
DataType = Data.DataType.String,
AllowNull = false,
DefaultValue = "Hello World",
Length = 50
};
schema.AddColumn(col);
col = new Column()
{
Name = "Column2",
DataType = Data.DataType.Int,
AllowNull = false
};
schema.AddColumn(col);
var schemaInstalled = schemaManager.Get(Repository.Current, "Schema1");
if (schemaInstalled == null)
schemaManager.Add(Repository.Current, schema);
#endregion
return Json(result);
}
[HttpPost]
public ActionResult Uninstall(string moduleName)
{
JsonResultEntry result = new JsonResultEntry() { RedirectToOpener = false };
var parentUrlHelper = new UrlHelper(PageControllerContext.RequestContext);
result.RedirectUrl = parentUrlHelper.Action("UninstallModule", new { controller = "ModuleManagement", ModuleName = moduleName, Message = "" });
#region Drop Schema
SchemaManager schemaManager = new SchemaManager();
Schema schema = new Schema(Repository.Current, "Schema1");
var schemaInstalled = schemaManager.Get(Repository.Current, "Schema1");
if (schemaInstalled != null)
schemaManager.Remove(Repository.Current, schema);
#endregion
return Json(result);
}
If I try this in module debug all works fine and database table is created/dropped.
To do this I have to simulate Install/Uninstall action, in my case:
INSTALL -> http://localhost:6692/dev~site1/Admin?ModuleUrl=%2FInstaller%2FInstall%3FSiteName%3Dsite1%26ModuleName%3Ddev&ModuleName=dev
UNINSTALL -> http://localhost:6692/dev~site1/Admin?ModuleUrl=%2FInstaller%2FUninstall%3FSiteName%3Dsite1%26ModuleName%3Ddev&ModuleName=dev
The problem occur when I install the module on Kooboo CMS (Admin->Modules->Install).
In this case Repository.Current is NULL so schemaManager.Add(Repository.Current, schema) throws an exception.
But in Admin->Modules->Install Repository.Current is never instantiated because it is instantiated only when I select a Site.
Am I doing something wrong or am I missing something?
Thank you in advance.
P.s. Actually I'm solving this issue adding a specific module menu item and a action that creates database tables after
the module is included in a Site