Skip to main content
Tweeted twitter.com/StackMagento/status/981549473817034752
added 42 characters in body
Source Link
Luke
  • 802
  • 2
  • 8
  • 24

As we all know, magento 2 suggests using repositories as an additional layer between resource models/collections and the rest of application. It's all pretty straightforward and simple when you use it for single tables. Majority of multi-table query results are obtained in magento 2 sourcecode using collections instead of repositories. I suppose that it's an inconrect way, as for instance extension attributes (which are recommended to be plugged only to repositories) won't load with those collections.

So my question is: what's the official recommended way to handle multi-table source of data in repositories?

Some ways that come to my mind are:

  • always join all possibly required tables (obviously terrible performance)
  • additional repositories/methods for every set of required joins (lots of redundant code and additional plugining when you use mechanisms such us extension attributes)
  • ignore repositories and use raw collections (additional plugins for mechanism such us extension attributes)
  • 'lazy' join additional tables to collections inside repositories when certain predefined fields are in filters (unobvious usage, dirty code I would say).

But none of solutions above looks reasonable to me.

As we all know, magento 2 suggests using repositories as an additional layer between resource models/collections and the rest of application. It's all pretty straightforward and simple when you use it for single tables. Majority of multi-table query results are obtained in magento 2 sourcecode using collections instead of repositories. I suppose that it's an inconrect way, as for instance extension attributes (which are recommended to be plugged only to repositories) won't load with those collections.

So my question is: what's the official recommended way to handle multi-table source of data in repositories?

Some ways that come to my mind are:

  • always join all possibly required tables (obviously terrible performance)
  • additional repositories/methods for every set of required joins (lots of redundant code and additional plugining when you use mechanisms such us extension attributes)
  • ignore repositories and use raw collections (additional plugins for mechanism such us extension attributes)
  • 'lazy' join additional tables to collections inside repositories when certain predefined fields are in filters.

But none of solutions above looks reasonable to me.

As we all know, magento 2 suggests using repositories as an additional layer between resource models/collections and the rest of application. It's all pretty straightforward and simple when you use it for single tables. Majority of multi-table query results are obtained in magento 2 sourcecode using collections instead of repositories. I suppose that it's an inconrect way, as for instance extension attributes (which are recommended to be plugged only to repositories) won't load with those collections.

So my question is: what's the official recommended way to handle multi-table source of data in repositories?

Some ways that come to my mind are:

  • always join all possibly required tables (obviously terrible performance)
  • additional repositories/methods for every set of required joins (lots of redundant code and additional plugining when you use mechanisms such us extension attributes)
  • ignore repositories and use raw collections (additional plugins for mechanism such us extension attributes)
  • 'lazy' join additional tables to collections inside repositories when certain predefined fields are in filters (unobvious usage, dirty code I would say).

But none of solutions above looks reasonable to me.

Source Link
Luke
  • 802
  • 2
  • 8
  • 24

How to handle multiple tables with magento 2 repositories?

As we all know, magento 2 suggests using repositories as an additional layer between resource models/collections and the rest of application. It's all pretty straightforward and simple when you use it for single tables. Majority of multi-table query results are obtained in magento 2 sourcecode using collections instead of repositories. I suppose that it's an inconrect way, as for instance extension attributes (which are recommended to be plugged only to repositories) won't load with those collections.

So my question is: what's the official recommended way to handle multi-table source of data in repositories?

Some ways that come to my mind are:

  • always join all possibly required tables (obviously terrible performance)
  • additional repositories/methods for every set of required joins (lots of redundant code and additional plugining when you use mechanisms such us extension attributes)
  • ignore repositories and use raw collections (additional plugins for mechanism such us extension attributes)
  • 'lazy' join additional tables to collections inside repositories when certain predefined fields are in filters.

But none of solutions above looks reasonable to me.