Uploading with left join

Uploading with left join

AxendoNLAxendoNL Posts: 6Questions: 2Answers: 0

I have the following c# code:

editor
    .LeftJoin("certificates", "certificates.Id", "=", "events.CertificateId")
    .Model<Certificate>("certificates")
    .Field(new Field("certificates.CertificateLine1"))
    .Field(new Field("certificates.CertificateLine2"))
    .Field(new Field("certificates.CertificateLine3"))
    .Field(new Field("events.CertificateId")
    .Upload(
       new Upload((file, id) => {
           _blobStorageService.UploadBlobAsync(file.OpenReadStream(), "eventTemplates");

           editor.Db().Update(
             "Files",
             new Dictionary<string, object>{
                { "FilePath", $"eventTemplates/{file.FileName}" }
             },
             new Dictionary<string, object>{
                { "id", id }
             }
           );

           return id;
       })
       .Db("Files", "Id", new Dictionary<string, object>
       {
           { "FileName", Upload.DbType.FileName },
           { "FilePath",  string.Empty }
       })
       .Validator(
           Validation.FileExtensions(
               ["pdf"],
               "Alleen pdf files zijn toegestaan."))
       )
   .SetFormatter(Format.NullEmpty()));

Now I have an event model that has an id to a Certifcate model that in turn has an Id to the Files table, where file info will be stored.
I want to be able to upload a file and alter some properties belonging to the certieficate model, but I can't get it to work.
When I edit an event to add a file I need a row to be created in the certificate tabel with a link to the file table. There should be only one certificate associated with an event row. How to do this?

Replies

  • allanallan Posts: 62,858Questions: 1Answers: 10,344 Site admin

    You need to include the events primary key both in the values being read from the database, but also in the values being submitted to the server. That can make things a little complex if you are allowing the end user to change the event for the certificate. In such a case, I would without hesitation suggest using the datatable field type for nested editing.

    Allan

Sign In or Register to comment.