0

enter image description here

Please Explain me in Magento 2.0 (Only Override Case)

2 Answers 2

-3

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()
2
  • Great :) I am use ->create('\Magento\Sales\Model\Orde') like this Commented Nov 28, 2015 at 16:53
  • 1
    OM 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. Commented Nov 29, 2015 at 8:49
2

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();
}
2
  • I use this and pass __contruct($context,$data) but showing error ==> Object DOMDocument should be created Commented Nov 28, 2015 at 16:57
  • you should pass all parameters declared in parent to parent constructor
    – KAndy
    Commented Nov 28, 2015 at 20:19

Your Answer

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

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