#1 2013-02-02 19:36:42

TommyYommi
Member
Registered: 2013-01-18
Posts: 27

One to one relationship

Hello,

I have 2 tables, on the first table I have a Integer field that relates with the second table. I need that the ORM goto the second table get a text field( of the corresponding row that indicates on the ID of the first table) and show togheter in the same select (same TSQLRecord).

A example of what I'm talking about:

  TUnit = class(TSQLRecord)
  private
    FName: RawUTF8;
  published
    property Name: RawUTF8 read FName write FName;
  end;


  TProduct = class(TSQLRecord)
  private
        ....
    FIDUnit: TUnit;
  published
       ....
    property IDUnit: TUnit read FIDUnit write FIDUnit;
  end;

In this case when I retrieve a TProduct the TUnit.Name has to come together with this record. How should I proceed?

Offline

#2 2013-02-02 20:01:11

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

Re: One to one relationship

You can use function TSQLRest.MainFieldValue() to retrieve the "main field" from its record's ID.

The UI classes use this feature to implement easy-to-work with "names" of retrieved fields.

Or you just use your own SQL request, run it as a service on the server, and return the needed information as a dedicated class, including the name - this is my favorite.

RoadMap wrote:

Introduce TSQLRecord published properties to (optionally) create true instances, just as with TSQLRecordMany;

This feature request could even be modified not to create true instances, but add the possibility to optionaly join some fields to a request:

  TProduct = class(TSQLRecord)
  private
        ....
    FUnit: TUnit;
    FUnitName: RawUTF8;
  public
    property UnitName: RawUTF8 read FUnitName write FUnitName;
  published
       ....
    property Unit: TUnit read FUnit write FUnit;
  end;

Here, an overriden FillPrepare() method may be able to ask to retrieve UnitName with an automatic join to the Unit field ID.
Of course, this won't be so simple, but I'm open to any implementation pattern.

By the way, take a look at http://synopse.info/forum/viewtopic.php?id=929 - link taken from the roadmap - which clarify all this.

Read also the "Database sharding" paragraph in the SAD pdf, by which JOINed queries are sometimes to be avoided, by storing the needed data.
In a CQRS architecture, this is pretty common and efficient, with today's hardware.
mORMot may be able to make such "internal JOINs" easier: that is, for instance, ask for a copied/sharded property name of a detail record to be automatically updated if the main property name is changed.

Offline

#3 2013-02-02 20:46:10

TommyYommi
Member
Registered: 2013-01-18
Posts: 27

Re: One to one relationship

ab wrote:

ask for a copied/sharded property name of a detail record to be automatically updated if the main property name is changed.

How can I do it? Is there a command to do it, like a trigger, every time some user alters the unit table the UnitName in the product table is updated?

Offline

#4 2013-02-04 11:30:13

TommyYommi
Member
Registered: 2013-01-18
Posts: 27

Re: One to one relationship

Is there a method that is called after a record be Updated?

Offline

#5 2013-02-04 12:00:52

ManUn
Member
Registered: 2013-02-04
Posts: 22

Re: One to one relationship

I have the same doubt. How can the server know if a record as updated? Is there a event like AfterUpdate?

Offline

#6 2013-02-04 12:17:53

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

Re: One to one relationship

See TSQLRestServer.OnUpdateEvent property and the following enum:

  /// used to define the triggered Event types for TNotifySQLEvent
  // - some Events can be trigerred via TSQLRestServer.OnUpdateEvent when
  // a Table is modified, and actions can be authorized via overriding the
  // TSQLRest.RecordCanBeUpdated method
  // - OnUpdateEvent is called BEFORE deletion, and AFTER insertion or update; it
  // should be used only server-side, not to synchronize some clients: the framework
  // is designed around a stateless RESTful architecture (like HTTP/1.1), in which
  // clients ask the server for refresh (see TSQLRestClientURI.UpdateFromServer)
  // - is used also by TSQLRecord.ComputeFieldsBeforeWrite virtual method
  TSQLEvent = (
    seAdd,
    seUpdate,
    seDelete);

Offline

#7 2013-02-04 15:20:00

TommyYommi
Member
Registered: 2013-01-18
Posts: 27

Re: One to one relationship

Can you show a example? It's a property, I can't override it.

Offline

#8 2013-02-04 15:24:44

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

Re: One to one relationship

It is a standard Delphi event, like OnShow or OnClick.

Create a method with the good signature, then assign it to the OnUpdateEvent property.

Offline

Board footer

Powered by FluxBB