2016-09-20 103 views
0

我爲我的Magento 2.1存儲開發了一些定製模塊,用於在某些CMS頁面中實現智能內容隱藏。Magento 2.1自定義模塊關係

我用這個教程https://www.ashsmith.io/magento2/module-from-scratch-introduction/和這個例子https://github.com/ashsmith/magento2-blog-module-tutorial爲了做到這一點。

現在,我在頁面上列出了常見問題解答,但每個FAQ都屬於常見問題解答類別(不是目錄類別)。 所以這裏有兩個自定義模塊(常見問題類別和常見問題解答)。 FAQ類別只有標題字段。 常見問題有標題字段,答案(文本編輯器)字段和常見問題解答問題下拉列表(所有可用的常見問題類別列表中的選擇框)

我不知道如何實現此操作。

什麼是正確的做法?特別是管理部分。

謝謝

+0

由於Stack Overflow是一個[編程相關](http://stackoverflow.com/help/on-topic)問答站點,因此我正在投票關閉此問題。你的問題不是關於編程。也許你應該在http://magento.stackexchange.com上發佈它呢? – Enigmativity

回答

0

我假設你想加入字段。你不能在di.xml使用虛擬類型做到這一點,所以你需要遵循這些步驟,更新你的文件

#FILE等/ di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"> 
     <arguments> 
      <argument name="collections" xsi:type="array"> 
       <item name="namespace_modulename_listing_data_source" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename\Grid\Collection</item> 
      </argument> 
     </arguments> 
</type> 
<type name="Namespace\Modulename\Model\Resource\Modulename\Grid\Collection"> 
    <arguments> 
     <argument name="mainTable" xsi:type="string">tablename</argument> 
     <argument name="eventPrefix" xsi:type="string">namespace_modulename_grid_collection</argument> 
     <argument name="eventObject" xsi:type="string">namespace_grid_collection</argument> 
     <argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\Resource\Modulename</argument> 
    </arguments> 
</type> 

在資源模型文件型號/資源/ MODULENAME/Collection.php

<?php 
namespace Namespace\Modulename\Model\Resource\Modulename; 

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; 

class Collection extends AbstractCollection 
{ 
    /** 
    * Define model & resource model 
    */ 
    const YOUR_TABLE = 'tablename'; 

    public function __construct(
     \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, 
     \Psr\Log\LoggerInterface $logger, 
     \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, 
     \Magento\Framework\Event\ManagerInterface $eventManager, 
     \Magento\Store\Model\StoreManagerInterface $storeManager, 
     \Magento\Framework\DB\Adapter\AdapterInterface $connection = null, 
     \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null 
    ) { 
     $this->_init(
      'Namespace\Modulename\Model\Modulename', 
      'Namespace\Modulename\Model\Resource\Modulename' 
     ); 
     parent::__construct(
      $entityFactory, $logger, $fetchStrategy, $eventManager, $connection, 
      $resource 
     ); 
     $this->storeManager = $storeManager; 
    } 
    protected function _initSelect() 
    { 
     parent::_initSelect(); 

     $this->getSelect()->joinLeft(
       ['join_table' => $this->getTable('tablename')], 
       'main_table.columnname = join_table.column_name', 
       '*' 
      ); 
    } 
} 
?> 

現在你的模型/資源/模塊名/表格/ Collection.php

<?php 
namespace Namespace\Modulename\Model\Resource\Modulename\Grid; 

use Magento\Framework\Api\Search\SearchResultInterface; 
use Magento\Framework\Search\AggregationInterface; 
use Namespace\Modulename\Model\Resource\Modulename\Collection as ModulenameCollection; 

/** 
* Class Collection 
* Collection for displaying grid 
*/ 
class Collection extends ModulenameCollection implements SearchResultInterface 
{ 
    /** 
    * Resource initialization 
    * @return $this 
    */ 
    public function __construct(
     \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, 
     \Psr\Log\LoggerInterface $logger, 
     \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, 
     \Magento\Framework\Event\ManagerInterface $eventManager, 
     \Magento\Store\Model\StoreManagerInterface $storeManager, 
     $mainTable, 
     $eventPrefix, 
     $eventObject, 
     $resourceModel, 
     $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document', 
     $connection = null, 
     \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null 
    ) { 
     parent::__construct(
      $entityFactory, 
      $logger, 
      $fetchStrategy, 
      $eventManager, 
      $storeManager, 
      $connection, 
      $resource 
     ); 
     $this->_eventPrefix = $eventPrefix; 
     $this->_eventObject = $eventObject; 
     $this->_init($model, $resourceModel); 
     $this->setMainTable($mainTable); 
    } 

    /** 
    * @return AggregationInterface 
    */ 
    public function getAggregations() 
    { 
     return $this->aggregations; 
    } 

    /** 
    * @param AggregationInterface $aggregations 
    * 
    * @return $this 
    */ 
    public function setAggregations($aggregations) 
    { 
     $this->aggregations = $aggregations; 
    } 


    /** 
    * Get search criteria. 
    * 
    * @return \Magento\Framework\Api\SearchCriteriaInterface|null 
    */ 
    public function getSearchCriteria() 
    { 
     return null; 
    } 

    /** 
    * Set search criteria. 
    * 
    * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria 
    * 
    * @return $this 
    * @SuppressWarnings(PHPMD.UnusedFormalParameter) 
    */ 
    public function setSearchCriteria(
     \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null 
    ) { 
     return $this; 
    } 

    /** 
    * Get total count. 
    * 
    * @return int 
    */ 
    public function getTotalCount() 
    { 
     return $this->getSize(); 
    } 

    /** 
    * Set total count. 
    * 
    * @param int $totalCount 
    * 
    * @return $this 
    * @SuppressWarnings(PHPMD.UnusedFormalParameter) 
    */ 
    public function setTotalCount($totalCount) 
    { 
     return $this; 
    } 

    /** 
    * Set items list. 
    * 
    * @param \Magento\Framework\Api\ExtensibleDataInterface[] $items 
    * 
    * @return $this 
    * @SuppressWarnings(PHPMD.UnusedFormalParameter) 
    */ 
    public function setItems(array $items = null) 
    { 
     return $this; 
    } 
} 

?> 

現在,您可以使用連接表列中的任何位置在網格中以及調用集合時。

+0

不知道我明白,我需要有2個模塊或只有一個? – Nir

+0

您可能有一個模塊,但您需要兩個模型用於FAQ類別,另一個用於FAQ問題,您可以通過上述步驟創建關係。 – Priyank

+0

此代碼適用於哪種型號? – Nir