Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the equivalents of magento 1.x models in magento 2.x

I am new to magento2 and I am finding it very difficult to get the regular code snippets right in the new version. So, please help me out here and explain the equivalents of the following snippets in magento2 :

Mage::getModel('catalog/product')->getCollection();
Mage::getModel('sales/order');
Mage::getModel('catalog/category')->getCollection();
Mage::getModel('customer/customer');
Mage::getModel('cart/quote');
Mage::getModel('checkout/cart');
Mage::getSingleton('customer/session');
Mage::getModel('catalog/category')->load(id);

I hope this question will help all the new magento 2 developers to find relative queries all at one place.

like image 943
Abhishek Dhanraj Shahdeo Avatar asked Oct 30 '22 20:10

Abhishek Dhanraj Shahdeo


1 Answers

In magento 2 there is no more static method for instantiating models.
You have to use dependency injection.
For the models that not injectable you can use a factory that will instantiate the models.
not injectables are for example the product model, the order model...generally something you can call load on. This includes collections.
The injectables, you can just inject in your constructor.
For example the customer session is injectable.

Let's say you have to use the models above in one of your classes.
I will add all of them in one class but you can use what you need only.

class MyClass extends SomeOtherClass
{
    protected $productCollectionFactory;
    protected $orderFactory;
    protected $categoryCollectionFactory;
    protected $customerFactory;
    protected $cart;
    protected $customerSession;
    protected $categorFactory;
    public function __construct(
       ... //you can have some other parameters here
       \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       ... //you can have other parameters here
    ) {
        ....
        $this->productCollectionFactory = $productCollectionFactory;
        $this->orderFactory = $orderFactory;
        $this->categoryCollectionFactory = $categoryCollectionFactory;
        $this->customerFactory = $customerFactory;
        $this->cart = $cart;
        $this->customerSession = $customerSession;
        $this->categoryFactory = $categorFactory;
        ....
    }
}

Then you can use them in your class like this.

To get the product collection you can do this:

$productCollection = $this->productCollectionFactory->create();

To get an isntance of the order model do this:

$order = $this->orderFactory->create();

Category collection

$categoryCollection = $this->categoryCollectionFactory->create();

Customer instance

$customer = $this->customerFactory->create();

cart/quote does not exist in magento 2.

for checkout cart you can simply use $this->cart as this is injectable.
same for customer session. These are singletons.

Get a category

 $category = $this->categoryFactory->create()->load($id);
like image 173
Marius Avatar answered Nov 15 '22 05:11

Marius