1

I have exception when I selected some records in grid and trying to delete them. Here is that exception:

1 exception(s): Exception #0 (InvalidArgumentException): Amiddio\News\Model\News does not extend \Magento\Framework\DataObject

Magento version is 2.1.8

app\code\Amiddio\News\view\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>

app\code\Amiddio\News\etc\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>

app\code\Amiddio\News\Model\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');
    }

}

app\code\Amiddio\News\Model\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');
    }
}

app\code\Amiddio\News\Model\ResourceModel\News\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');
    }

}

app\code\Amiddio\News\Model\ResourceModel\News\Grid\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;
    }
}

app\code\Amiddio\News\Controller\Adminhtml\Actions\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/');
    }
}

I'm beginner in Magento 2, and I'm trying to resolve this issue some days already. Maybe someone had that problem and he/she knows solution?!

Thanks very much for any help!

3

I found the bug im my code. In the file 'ResourceModel\News\Collection.php'. I have space before 'Amiddio\News\Model\News'.

The problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.