Scandal
Well-known member
Let's say we have to fetch all data via xF2 finder:
This entity has two TO_MANY relations (one is the Attachments).
How could I prepare an output variable which will have all necessary data without leave xF2 to make individual queries for the extra TO_MANY relations?
I can get the primary keys of Data and then run one query for ALL attachments.
Example:
But it doesn't seem to work.
Any idea?
I need to make ONE query for all attachments and then prepare the final output with them.
PHP:
$data = \XF::finder('Scandals\Addon:Data')->fetch();
How could I prepare an output variable which will have all necessary data without leave xF2 to make individual queries for the extra TO_MANY relations?
I can get the primary keys of Data and then run one query for ALL attachments.
Example:
PHP:
$output = [];
$ids = [];
foreach ($data AS $d)
{
$output[$d->data_id] = $d;
$ids[] = $d->data_id;
}
if (!empty($ids))
{
$attachments = \XF::finder('XF:Attachment')
->with('Data')
->where('content_type', 'sc_data')
->where('content_id', $ids)
->fetch();
foreach ($attachments AS $a)
{
$output[$a->content_id]['Attachments'][] = $a;
}
}
Any idea?
I need to make ONE query for all attachments and then prepare the final output with them.