【问题标题】:Magento 2.1 Custom module relationshipMagento 2.1 自定义模块关系
【发布时间】:2017-01-28 22:23:52
【问题描述】:

我为我的 Magento 2.1 商店开发了一些自定义模块,用于智能管理某些 CMS 页面中的内容。

我使用本教程 https://www.ashsmith.io/magento2/module-from-scratch-introduction/ 和本示例 https://github.com/ashsmith/magento2-blog-module-tutorial 来完成此操作。

现在,我在页面上有常见问题解答列表,但每个常见问题解答都属于常见问题解答类别(不是目录类别)。 所以这里有两个自定义模块(FAQ Category 和 FAQ Question)。 常见问题类别只有标题字段。 常见问题解答问题有标题字段、答案(文本编辑器)字段和常见问题解答问题下拉列表(包含所有可用常见问题解答类别列表的选择框)

我不知道如何做到这一点。

正确的做法是什么?尤其是管理部分。

谢谢

【问题讨论】:

标签: magento magento2


【解决方案1】:

我假设您想加入字段。您无法通过在 di.xml 中使用虚拟类型来执行此操作,因此您需要按照以下步骤更新文件

#File etc/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>

在您的资源模型文件 Model/Resource/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',
                '*'
            );
    }
}
?>

现在您的模型/资源/ModuleName/Grid/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;
    }
}

?>

现在您可以在网格中的任何位置以及调用集合时使用连接表列。

【讨论】:

  • 不确定我是否理解,我需要两个模块还是只有一个?
  • 您可能有一个模块,但您需要有两个模型,一个用于常见问题类别,另一个用于常见问题,您可以通过上述步骤创建关系。
  • 这段代码是你可以加入表格的方法。因此,如果您想将常见问题解答问题加入常见问题解答类别,请在您的问题模型中使用上述代码,当您打印问题模型时,您还将获得类别值。
  • 没关系,但我问的是如何为问题创建管理部分 (CRUD),我想在添加问题时选择将其分配给类别(选择框)跨度>
  • 这里的每一步都很难解释。您可以按照上面给出的参考教程来创建 CRUD 模型。你可能不得不改变我给你答案的事情。
猜你喜欢
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2013-09-13
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多