Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php magento 2.x中magento 1.x模型的等效物是什么_Php_Magento_Zend Framework_Magento2 - Fatal编程技术网

Php magento 2.x中magento 1.x模型的等效物是什么

Php magento 2.x中magento 1.x模型的等效物是什么,php,magento,zend-framework,magento2,Php,Magento,Zend Framework,Magento2,我是magento2的新手,我发现在新版本中很难获得正常的代码片段。所以,请帮我解释一下magento2中以下代码片段的等价物: Mage::getModel('catalog/product')->getCollection(); Mage::getModel('sales/order'); Mage::getModel('catalog/category')->getCollection(); Mage::getModel('customer/customer'); Mage::

我是magento2的新手,我发现在新版本中很难获得正常的代码片段。所以,请帮我解释一下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);

我希望这个问题能帮助所有新的magento 2开发人员在一个地方找到相关的查询。

在magento 2中,没有更多的静态方法来实例化模型。
您必须使用依赖项注入。
对于不可注入的模型,您可以使用将实例化模型的工厂。
不可注入的是例如产品模型、订单模型……通常可以称之为加载。这包括收藏。
对于可注入项,您可以只注入构造函数。
例如,客户会话是可注入的

假设您必须在其中一个课程中使用上述模型。
我将在一个类中添加所有这些内容,但您只能使用您需要的内容

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;
        ....
    }
}
然后你可以像这样在课堂上使用它们

要获取产品集合,您可以执行以下操作:

$productCollection = $this->productCollectionFactory->create();
要获取订单模型的信息,请执行以下操作:

$order = $this->orderFactory->create();
类别集合

$categoryCollection = $this->categoryCollectionFactory->create();
客户实例

$customer = $this->customerFactory->create();
购物车/报价单在magento 2中不存在

对于结帐购物车,您只需使用
$this->cart
,因为这是可注入的。
客户会话也一样。这些是单身汉

获取类别

 $category = $this->categoryFactory->create()->load($id);