#1 2021-06-25 10:11:00

ttomas
Member
Registered: 2013-03-08
Posts: 117

MVC Sample for mORMot2

I'm testing mormot2 with FPC Win and Linux and convert 30-MVC Server sample to PUREMORMOT2
You can download converted sample from http://halkyon.com/download/MVCServer.zip
In MVCViewModel.pas
1. Default URI is OK
2. AuthorView is broken with "out Author: TOrmAuthor" parameter, I changed to "var Author: TOrmAuthor" and now is OK (line 42)
3. ArticleView is broken, exception on TObjectList parameter create in ExecuteJson.
In mormot.core.json.pas line 9203, function _New_ObjectList(Rtti: TRttiCustom): pointer;
I change
result := TObjectList(Rtti.ValueClass).Create;
to
result := Rtti.ValueClass.Create;
Now TObjectList is created and ArticleView method is executed, but new exception is raised on serialization of var Article: TOrmArticle in mormot.core.json
line 4966 in procedure _JS_ID
  Ctxt.W.Add(Data^); here Data heve value of $09, Can not access memory at address $09.

Offline

#2 2021-06-26 08:08:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: MVC Sample for mORMot2

We didn't validate MVC for mORMOt 2 yet.
I want to add some automated tests for MVC (there is none yet) to mORMot 2.
Stay tuned!

To help, I have just fixed TRttiCustom.ClassNewInstance for TObjectList.

Offline

#3 2021-06-26 10:10:13

ttomas
Member
Registered: 2013-03-08
Posts: 117

Re: MVC Sample for mORMot2

Thanks, just to notice TOrmArticle serialization this is not MVC related
  RestModel.Orm.Retrieve(ID,Article);
  writeln(FormatUTF8('%', [ObjectToJson(Article)]));   // << is broken, test in ArticleView

Offline

#4 2021-07-07 11:50:23

ttomas
Member
Registered: 2013-03-08
Posts: 117

Re: MVC Sample for mORMot2

Can someone check what is a problem with serialization of TOrmArticle, problem is in procedure _JS_ID serialization of ID.
I create new test simple rest server project to exclude MVCApp, using same sqlite db and MVCModel, same problem.
TOrmArticle use FTS, I create new class TOrmArttest same as TOrmArticle (copy/paste) without FTS, same problem.
I try to change TOrmArttest = class(TOrmContent) to TOrmArttest = class(TOrm) and add all published property from inherited classes, same problem.

I'm experimenting with htmx.org, alpinejs, windi, vite project served by MVC mORMot and want to use mORMot2, some changes must be done in MCV App for serving htmx request, every URI must have 2 mustache templates, 1 for htmx request (HX-data in header of request) only returning iframe/partial and 2 for page reload/refresh full page as now.

Offline

#5 2021-07-07 17:24:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: MVC Sample for mORMot2

Could you please be more precise about the "problem"?
If I understand correctly, then your Article object pointer may be invalid.

When I call ObjectToJson() on a freshly create TOrmArticle:

procedure TTestOrmCore.OrmHierarchy;
var
  a: TOrmArticle;
begin
  a := TOrmArticle.Create;
  try
    a.IDValue := 1203;
    writeln(a.GetJsonValues(true,true,ooSelect));
    writeln(ObjectToJson(a));
  finally
    a.Free;
  end;
end;

then I have got the expected result:

'{"ID":1203,"CreatedAt":0,"ModifiedAt":0,"Title":"","Content":"","ContentHtml":false,"Author":null,"AuthorName":"","PublishedMonth":0,"Abstract":"","Tags":[],"LegacyHash":0}'

What does Article.GetJsonValues(true,true,ooSelect) return?

I will try and see your project sample.

Offline

#6 2021-07-07 18:03:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: MVC Sample for mORMot2

OK.

I tried your sample.
Nice conversion.

After TBlogApplication.ArticleView() is executed, the nested Author: TOrmAuthor property is not properly identified as an integer, so it is handled as a pointer, therefore it fails to serialize it.
I will fix it.

Offline

#7 2021-07-07 19:13:37

ttomas
Member
Registered: 2013-03-08
Posts: 117

Re: MVC Sample for mORMot2

@ab this simple test project work in mORMot1 but in mORMot2 raise exception

  aModel := CreateModel;
  try
    aServer := TRestServerDB.Create(aModel,Executable.ProgramFilePath+'MVCServer.db');
    try
      aServer.DB.Synchronous := smNormal;
      aServer.DB.LockingMode := lmExclusive;
      aServer.Server.CreateMissingTables;
      try
        Article := TOrmArticle.Create;
        try
          if aServer.Orm.Retrieve(666,Article) then begin
             writeln(article.GetJsonValues(true,true,ooSelect)); // This is OK Author: 1
             writeln(FormatUTF8('%', [ObjectToJson(Article)]));  // exception
          end;
        finally
          Article.Free;
        end;
        readln;
    finally
      aServer.Free;
    end;
  finally
    aModel.Free;
  end;

Offline

#8 2021-07-08 08:56:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: MVC Sample for mORMot2

I have just fixed the TOrm nested properties serialization.

Still some other issues to fix for mORMot 2 MVC.

Offline

#9 2021-07-08 10:31:24

ttomas
Member
Registered: 2013-03-08
Posts: 117

Re: MVC Sample for mORMot2

@ab Thanks and Thanks for adding OnBeforeRender in MVC it will help a lot when using htmx, hotwire turbo or similar libraries where we need to  serve different html response depend of request headers.

Offline

#10 2021-07-09 07:04:24

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: MVC Sample for mORMot2

I have made some fixes, and also included your converted sample as https://github.com/synopse/mORMot2/tree … x/mvc-blog

Offline

Board footer

Powered by FluxBB