MassDelete动作期间Magento2中的例外情况

时间:2017-09-29 10:36:34

标签: php exception magento2 uicomponents

当我在网格中选择一些记录并试图删除它们时,我有异常。这是一个例外:

  

1个异常:异常#0(InvalidArgumentException):   Amiddio \ News \ Model \ News不扩展\ Magento \ Framework \ DataObject

Magento版本是2.1.8

应用\代码\ Amiddio \新闻\视图\ adminhtml \ ui_component \ news_grid_listing.xml

<argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="sticky" xsi:type="boolean">true</item>
        </item>
    </argument>
    <massaction name="listing_massaction">
        <action name="delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="news/actions/massDelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete items</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure you want to delete selected items?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>

应用\代码\ Amiddio \新闻\等\ di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="news_grid_listing_data_source" xsi:type="string">Amiddio\News\Model\ResourceModel\News\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

应用\代码\ Amiddio \新闻\模型\ News.php

namespace Amiddio\News\Model;

use Magento\Framework\Model\AbstractModel;

class News extends AbstractModel
{

    protected function _construct()
    {
        $this->_init('Amiddio\News\Model\ResourceModel\News');
    }

}

应用\代码\ Amiddio \新闻\模型\ ResourceModel \ News.php

namespace Amiddio\News\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class News extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('amiddio_news', 'news_id');
    }
}

应用\代码\ Amiddio \新闻\模型\ ResourceModel \新闻\ Collection.php

namespace Amiddio\News\Model\ResourceModel\News;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

    protected function _construct()
    {
        $this->_init(' Amiddio\News\Model\News', 'Amiddio\News\Model\ResourceModel\News');
    }

}

应用\代码\ Amiddio \新闻\模型\ ResourceModel \新闻\网格\ Collection.php

namespace Amiddio\News\Model\ResourceModel\News\Grid;

use Amiddio\News\Model\ResourceModel\News\Collection as GridCollection;
use Magento\Framework\Search\AggregationInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\Document;
use Amiddio\News\Model\ResourceModel\News;
use Magento\Framework\Api\SearchCriteriaInterface;

class Collection extends GridCollection implements SearchResultInterface
{
    protected $aggregations;

    protected function _construct()
    {
        $this->_init(Document::class, News::class);
    }

    public function getAggregations()
    {
        return $this->aggregations;
    }

    public function setAggregations($aggregations)
    {
        $this->aggregations = $aggregations;
    }

    public function getAllIds($limit = null, $offset = null)
    {
        return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
    }

    public function getSearchCriteria()
    {
        return null;
    }

    public function setSearchCriteria(SearchCriteriaInterface $searchCriteria = null)
    {
        return $this;
    }

    public function getTotalCount()
    {
        return $this->getSize();
    }

    public function setTotalCount($totalCount)
    {
        return $this;
    }

    public function setItems(array $items = null)
    {
        return $this;
    }
}

应用\代码\ Amiddio \新闻\控制器\ Adminhtml \操作\ MassDelete.php

namespace Amiddio\News\Controller\Adminhtml\Actions;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Amiddio\News\Model\ResourceModel\News\CollectionFactory;

/**
 * Class MassDelete
 */
class MassDelete extends \Magento\Backend\App\Action
{
    /**
     * @var Filter
     */
    protected $filter;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
    {
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;

        parent::__construct($context);
    }

    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $collectionSize = $collection->getSize();

        foreach ($collection as $page) {
            $page->delete();
        }

        $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('news/index/');
    }
}

我是Magento 2的初学者,我已经有几天试图解决这个问题了。也许有人有这个问题,他/她知道解决方案?!

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

在您的新闻模型(app \ code \ Amiddio \ News \ Model \ News.php)中,您的类应该实现IdentityInterface。

变化:

  

类新闻扩展了AbstractDb {...

为:

  

使用Magento \ Framework \ DataObject \ IdentityInterface;

     

类新闻扩展AbstractDb实现了IdentityInterface {....

答案 1 :(得分:0)

我发现了我的代码中的错误。 在文件&#39; ... ResourceModel \ News \ Collection.php&#39;我在空间。

问题解决了。