6

I need the list of all the admin users.

And, both the below set of codes, provide the same result.

Code 1

public function __construct(
        \Magento\User\Model\ResourceModel\User\Collection $userCollectionFactory 
    )
    {
        $this->userCollectionFactory = $userCollectionFactory;
    }

    public function getCustomData()
    {
        $userCollection = $this->userCollectionFactory;
        var_dump($userCollection->getData());
    }

Code 2

public function __construct(
    \Magento\User\Model\ResourceModel\User\CollectionFactory $userCollectionFactory 
)
{
    $this->userCollectionFactory = $userCollectionFactory;
}

public function getCustomData()
{
    $userCollection = $this->userCollectionFactory->create();
    var_dump($userCollection->getData());
    exit('test');
}

My question here is, what is the difference between both these set of codes and which one is better to use and in which case we can use the other one? And if there is any other better way to fetch the same result set?

3 Answers 3

4

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

You can use "Collection" to load collection of particuler 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 !!!"

0

Please use collectionFactory. I don't know much about the difference I would like to share real case regarding this

I found one bug in BSS store credit extension where they were using custom collection filter using collection class rather than use collection factory, and it was not filtering right way custom store credit collection for sales order data, consequence is some orders show store credit amount in other sales order api even they did not use it in real case when I used collectionfactory it resoved the issue and it is filtering data perfectly with collectionfactory class. This was real scenario which I faced

Your Answer

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

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