Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento2语法讲解

M2不是一个标准的MVC架构。

这是m2的app/code/Magento/Catalog插件代码。
可以看到,它有Controller,也有Model,也有view.
奇怪的是,在Controller找不到调用模版的代码。
这是因为我们之前讲过,m2的页面都是用xml写的,xml里是由若干个block组成的。block里调用的template就是view下面的template。

所以,这个Controller加载的是该页面的xml文件。xml再解析输出成html。

细心的你会发现,这个插件里也有一个Block目录,没错,就是它。
xml里的就是这个block,这个block里才是真正的逻辑功能代码。

到处都是block。

M2有自己的语法,封装了很多类。

增删改查

一个标准的php类如下:

<?php
namespace Zou\Test\Block;
class Demo extends \Magento\Framework\View\Element\Template{
    
    protected $_storeManager;
    protected $_scopeConfig;
    
    protected $_productFactory;
    protected $_productCollectionFactory;

    protected $_categoryFactory;
    protected $_categoryCollectionFactory;

    protected $_customerFactory;
    protected $_customerCollectionFactory;

    protected $_orderFactory;
    protected $_orderCollectionFactory;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,

        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,

        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->_storeManager = $storeManager;
        
        $this->_productFactory = $productFactory;
        $this->_productCollectionFactory = $productCollectionFactory;

        $this->_categoryFactory = $categoryFactory;
        $this->_categoryCollectionFactory = $categoryCollectionFactory;

        $this->_customerFactory = $customerFactory;
        $this->_customerCollectionFactory = $customerCollectionFactory;

    }
    
    //通过产品id,获取产品的name(属性)
    public function getProductName($pid=1){
    	$product = $this->_productFactory->create()->load($pid);
        return $product->getName();
    }

    //获取价格大于100的产品
    public function getProductsByPrice($price=100){
        $productCollection = $this->_productCollectionFactory->create();
        $productCollection->addAttributeToSelect('price');
        $productCollection->addAttributeToFilter('price', array('gt'=>$price));
        foreach ($productCollection as $product) {
            echo $product->getPrice();
        }
        return $productCollection;
    }

    //把id为10的产品价格修改为50
    public function setProduct(){
        $price = 50;
        $pid = 10;
        $product = $this->_productFactory->create()->load($pid);
        $product->setPrice(50);
        $product->save();
    }


    //删掉id为1的产品
    public function deleteProduct($pid=1){
        $product = $this->_productFactory->create()->load($pid);
        $product->delete();
    }

}
?>

namespace对phper应该不陌生了,现在php7新框架基本上都是用的命名空间。

在__construct里的声明的需要的类。

比如

  1. \Magento\Catalog\Model\ProductFactory是产品模型类,通过这个类你可以得到单个产品的任何信息(比如属性)。
  2. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory是产品数据集,通过这个类,你可以任意按条件(比如属性)搜索过滤产品
  3. \Magento\Catalog\Model\CategoryFactory是分类模型类,通过这个类你可以得到单个分类的任何信息(比如属性)。
  4. \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory是分类数据集,通过这个类,你可以任意按条件(比如属性)搜索过滤分类
  5. \Magento\Customer\Model\CustomerFactory $customerFactory是客户联系人模型类,通过这个类你可以得到单个Customer的任何信息(比如属性)。
  6. \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory是联系人数据集,通过这个类,你可以任意按条件(比如属性)搜索过滤联系人
  7. \Magento\Sales\Model\OrderFactory是订单模型类,通过这个类你可以得到单个订单的任何信息(比如属性)。
  8. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory是订单数据集,通过这个类,你可以任意按条件(比如属性)搜索过滤订单

通过上面这个简易的php代码,你就学会了增删改查,是不是非常简单粗暴?

通过M2提供的模型数据资源类,就可以从容优雅的进行增删改查。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论