0

I am building a custom feature to create lists of favourite products.

For this purpose I have created 2 models: The actual list-entity and a model representing information about a single product. For each of these models I crated a database-table as well as a model, resource-model, collection and repository class (I will thin this out once I know what I don't need).

The list-entity table holds information about the customer the list belongs to and list-specific data such as a name. The list-item table holds information about what list it belongs to and which product it represents.

At which point/layer should I connect these when trying to fetch information (eg. I am trying to get all lists for a single user, along with the included products)? Should this be accessible in the model, the collection or the repository? What would be the cleanest way?

1 Answer 1

1

The best way to connect the two models by collection, If you want the collective data from the two tables then load the collection of the main table and make joins to get the whole data on the basis of common field like id

Example :

// Main table collection and then getting combined data by joining the sales table
$this->collection->getSelect()->joinLeft(
            ['sales' => $this->collection->getResource()->getTable('sales_order_grid')],
            'main_table.magento_order_id = sales.increment_id',
            [
                'sales.billing_name',
                'sales.shipping_name',
                'sales.billing_name',
                'sales.base_grand_total',
                'sales.status as magento_status'
            ]
        )->where('sales.store_id IS NULL OR sales.store_id = '.$storeId);
1
  • 1
    This is technically the correct answer to my question. Thank you! I had to solve it on my model layer (calling methods from repositories there) because of various limitations.
    – Hurrpadurr
    Commented Mar 24, 2022 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.