5

What is the difference between the collection and collection factory?

1
  • Hi @pankaj Please use collectionFactory I dont know much about the difference but I found one bug in BSS store credit extension where they were using custom collection filter using collection rather than collection factory, and it was not filtering right way for sales order api usinf collection class, consequence is some orders show store credit amount even they did not use it in real case when I used collectionfactory it resoved the issue Oct 11, 2023 at 14:57

3 Answers 3

2

You can use "Collection" to load collection of perticuler module (e.g : customer) with all attributes,it will returns all attributes values.

While you can use CollectionFactory , you have to add create() method,generally Collection Factory is used if you want to filter data from given collection(e,g : you can filter customer collection with wholesaler group only ).

" Hope This Will Help You !!!"

1

Reference Link

I understand this follows the Factory pattern https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)#PHP

So, create() method returns the requested object, in this case: \Magento\User\Model\ResourceModel\User\Collection

However... it seems you shouldn't be able to inject that object in constructor (Code 1), or at least, and responding to best practices, you should better inject the factory

Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.

http://devdocs.magento.com/guides/v2.0/extension-dev-guide/factories.html

1

A collection is an instance of an object.

A collection factory is responsible for creating an instance of an object.

IE a collection factory creates a collection.

This is part of the Factory pattern:

https://en.wikipedia.org/wiki/Factory_method_pattern

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.