Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Problem on creating database tables on Module install
andrea.t
#1 Posted : Wednesday, September 07, 2011 9:53:54 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 8/31/2011(UTC)
Posts: 6
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
jifeng
#2 Posted : Thursday, September 08, 2011 1:33:22 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,552
Location: Xiamen China
Oh, yes, it has not related to any site, so it can create the content type when you installing the module.

Your solution is correct, you can initial the content type after you include a module under the site.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
haighis
#3 Posted : Thursday, November 10, 2011 7:46:49 PM(UTC)
Rank: Newbie
Groups: Registered

Joined: 11/2/2011(UTC)
Posts: 1
Location: Milton, Ontario, Canada
I tried the above code and I don't see a schema being created called "Schema1".

Is the schema being created a content type?
jifeng
#4 Posted : Friday, November 11, 2011 5:35:01 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,552
Location: Xiamen China
Yes, the content type is the same as schema.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF 1.9.5.5 | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.625 seconds.