Please Explain me in Magento 2.0 (Only Override Case)
2 Answers
Following way you can get object manager instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
Now you can get/create any model or model collection. Here is an example of order collection.
$orderCollection = $objectManager->get('Magento\Sales\Model\Order')->getCollection()
-
Great :) I am use ->create('\Magento\Sales\Model\Orde') like this Nov 28, 2015 at 16:53
-
1OM should never be instantiated directly, this contradicts the basics of M2 architecture. It will at least be impossible to write unit tests for such classes. See response from @KAndy. Nov 29, 2015 at 8:49
In Magento 2 constructor is not part of public API and you can change it (add more dependencies). And you should do not call ObjectManager direct.
in your case, add constructor with order factory
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Directory\Helper\Data $directoryHelper,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\App\Cache\Type\Config $configCacheType,
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollectionFactory,
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
\Magento\Framework\Module\Manager $moduleManager,
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Model\Url $customerUrl,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
array $data = []
) {
parent::__construct(...);
$this->collectionFactory = $collectionFactory;
and in method hello use it
public function hello()
{
return $this->collectionFactory->create()->load();
}
-
I use this and pass __contruct($context,$data) but showing error ==> Object DOMDocument should be created Nov 28, 2015 at 16:57
-
you should pass all parameters declared in parent to parent constructor– KAndyNov 28, 2015 at 20:19