G
G
Georgy Baruchyan2020-05-20 22:44:34
ORM
Georgy Baruchyan, 2020-05-20 22:44:34

Bitrix ORM: get one of the linked table records?

Hello.
Something does not work for me to make a slightly non-standard request through the ORM. please help

For example, there is a table of orders b_order (OrderTable)
B there is a table of logs, for example, logs (LogTable)

The logs table can have many records for each order, and there may also be no records

I need to get a list of orders linked to the last record logs table (if any)

$query = OrderTable::query()

   ->registerRuntimeField('LOG', array(
        'data_type'=> LogTable::class,
        'reference' => [
            '=this.ID' => 'ref.ORDER_ID',
            
        ]
    ))
;

In this form, we will get all records of the related table for existing orders

. For the case when we know that there is a log entry for each order, we can do this:

->registerRuntimeField('LAST_LOG_ENTITY', [
    'data_type'=>'integer',
    'expression' => ['MAX(%s)', 'LOG.ID']
])
->where('LAST_LOG_ENTITY', '!=', false)


In this case, all orders for which there is no entry in the logs will be eliminated.

I tried various combinations of the request, I see no reason to write all the options, so I described the basic version.

I really ask for help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kontrael, 2020-05-21
@Kontrael

And you do not consider a variant with two requests.?
Select orders, collect IDs, select logs with maximum IDs linked to collected orders. If logs has an index by ORDER_ID, then the query will be very fast.

A
Alexey Emelyanov, 2020-05-21
@babarun

Try to use complex logic with a flag to eliminate duplicates "LOGIC"=>"OR"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question