7

I get in my editor (PhPStorm) on getCollection the message that it is deprecated.

In my controller i got the function

public function __construct(
    Registry $registry,
    TableRepositoryInterface $dataRepository,
    PageFactory $resultPageFactory,
    ForwardFactory $resultForwardFactory,
    Manager $messageManager,
    TableInterfaceFactory $dataFactory,
    DataObjectHelper $dataObjectHelper,
    Context $context,
    Logger $logger,
    DirectoryList $directoryList,
    File $ioFile,
    Menu $helper,
    Costs $costsModel,
    CostsRepositoryInterface $costsRepository,
    CostsFactory $costsFactory
) {
    $this->costsFactory     = $costsFactory;
    $this->costsRepository  = $costsRepository;
    $this->costsModel       = $costsModel;
    $this->helper           = $helper;
    $this->ioFile           = $ioFile;
    $this->directoryList    = $directoryList;
    $this->logger           = $logger;
    $this->messageManager   = $messageManager;
    $this->dataFactory      = $dataFactory;
    $this->dataRepository   = $dataRepository;
    $this->dataObjectHelper = $dataObjectHelper;
    parent::__construct($registry, $dataRepository, $resultPageFactory, $resultForwardFactory, $context, $logger);
}

And here i call the function

public function existingColorRange($data)
{
    // Check the color_amount/from/till with database
    $costsCollection = $this->costsFactory->create()->getCollection();
}

Got my model build just like in following link

How do i call the collection the right way without deprecated function?

  • Have you found your question's answer? – Rutvee Sojitra Sep 27 '18 at 12:17
1

I had the same exact issue and after digging through core code and reviewing generated classes, I found how.

If you look in vendor/magento/modulecatalog/Model/ResourceModel/Product.php

Notice:

\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory

In the constructor, we're expecting a CollectionFactory instance. If you look at the dynamically generated factory class you'll see:

<?php
namespace Magento\Catalog\Model\ResourceModel\Category;

/**
 * Factory class for @see \Magento\Catalog\Model\ResourceModel\Category\Collection
 */
class CollectionFactory
{
    /**
     * Object Manager instance
     *
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $_objectManager = null;

    /**
     * Instance name to create
     *
     * @var string
     */
    protected $_instanceName = null;

    /**
     * Factory constructor
     *
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param string $instanceName
     */
    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection')
    {
        $this->_objectManager = $objectManager;
        $this->_instanceName = $instanceName;
    }

    /**
     * Create class instance with specified parameters
     *
     * @param array $data
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection
     */
    public function create(array $data = array())
    {
        return $this->_objectManager->create($this->_instanceName, $data);
    }
}

Where the instance name is the collection.

In your case, change:

CostsFactory $costsFactory

To:

CostsCollectionFactory $costsFactory

use Example\Namespace\Model\ResourceModel\Cost\CollectionFactory as CostsCollectionFactory;

Then you can use the collection like so:

$costsCollection = $this->costsFactory->create();

$costsCollection->addFieldToSelect(...)
$costsCollection->addFieldToFilter(...)
...
0

1. Create Your Collection Model

Create class Vendor\Module\Model\ResourceModel\Costs\Collection in file app/code/Vendor/Module/Model/ResourceModel/Costs/Collection.php, codes like below

<?php
namespace Vendor\Module\Model\ResourceModel\Costs;


use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Vendor\Module\Model\Costs;
use Vendor\Module\Model\ResourceModel\Costs as CostsResource;

class Collection extends AbstractCollection
{
    /**
     * @var string
     */
    protected $_idFieldName = 'id';

    /**
     * Initialize resource collection.
     */
    public function _construct()
    {
        $this->_init(
            Costs::class,
            CostsResource::class
        );
    }
}

2. Get the Collection

In your class, write codes like below, the function getCollection is exactly what you need.

<?php
namespace Vendor\Module\Model;

use Vendor\Module\Model\ResourceModel\Costs\CollectionFactory;

class CostsRepository
{
    protected $collectionFactory;

    public function __construct(
        CollectionFactory $collectionFactory
    )
    {
        $this->collectionFactory = $collectionFactory;
    }

    public function getCollection()
    {
        return $this->collectionFactory->create();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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