<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                # Magento2語法講解 M2不是一個標準的MVC架構。 ![](https://box.kancloud.cn/0388fdc35b6131ecaad47b434ae4c0f8_324x834.png) 這是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里才是真正的邏輯功能代碼。 ![](https://box.kancloud.cn/07e24f060407c112a09939cf7c4ba21a_1638x1110.png) 到處都是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提供的模型數據資源類,就可以從容優雅的進行增刪改查。 先賣個關子,具體的我們在第四章做插件的時候 會細講。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看