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?
Best Answer
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:
In the constructor, we're expecting a CollectionFactory instance. If you look at the dynamically generated factory class you'll see:
Where the instance name is the collection.
In your case, change:
To:
Then you can use the collection like so: