5

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(...)
...

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.