Skip to content

Nested Merlin Objects

Squil11ams edited this page Nov 9, 2022 · 1 revision

Nested Merlin Objects

Merlin Auto-Population Logic (APL) supports populating multiple nested MerlinObjects within each other.

Lets say we have a database with 2 tables,

Orders

order_id

order_amount

order_customer_fk

order_employee_fk

People

person_id

person_name

Then we have a view to represent the orders.

SELECT 
    O.*,
    C.person_id as 'c_person_id',
    C.person_name as 'c_person_name',
    E.person_id as 'e_person_id',
    E.person_name as 'e_person_name'
From Orders `O` 
    LEFT JOIN People `C` ON O.order_customer_fk = C.person_id
    LEFT JOIN People `E` ON O.order_employee_fk = E.person_id

Our Model would look like this

class Person : MerlinModelBase
{
    public int person_id { get; set; }
    public string person_name { get; set; }
}

class Order : MerlinModelBase
{
    public int order_id { get; set; }
    public int order_amount { get; set; }
    public int order_customer_fk { get; set; }
    public int order_employee_fk { get; set; }

    [MerlinObject("c_")]
    public Person Customer { get; set; }
    
    [MerlinObject("e_")]
    public Person Employee { get; set; }
}

In this instance we are looking at the orders table and referencing the person table twice. Since we are doing it twice we need to prefix the columns with an identifier so that A) the sql is valid and B) we can tell the difference between customer and employee.

When we setup the model all we have to do is specify using attributes that Person fields implement the MerlinObject and tell it the prefix we are using and the APL is able to populate both objects.

Clone this wiki locally