skip to Main Content

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):
AmiddioNewsModelNews does not extend MagentoFrameworkDataObject

Magento version is 2.1.8

appcodeAmiddioNewsviewadminhtmlui_componentnews_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>

appcodeAmiddioNewsetcdi.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="news_grid_listing_data_source" xsi:type="string">AmiddioNewsModelResourceModelNewsGridCollection</item>
            </argument>
        </arguments>
    </type>
</config>

appcodeAmiddioNewsModelNews.php

namespace AmiddioNewsModel;

use MagentoFrameworkModelAbstractModel;

class News extends AbstractModel
{

    protected function _construct()
    {
        $this->_init('AmiddioNewsModelResourceModelNews');
    }

}

appcodeAmiddioNewsModelResourceModelNews.php

namespace AmiddioNewsModelResourceModel;

use MagentoFrameworkModelResourceModelDbAbstractDb;

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

appcodeAmiddioNewsModelResourceModelNewsCollection.php

namespace AmiddioNewsModelResourceModelNews;

class Collection extends MagentoFrameworkModelResourceModelDbCollectionAbstractCollection
{

    protected function _construct()
    {
        $this->_init(' AmiddioNewsModelNews', 'AmiddioNewsModelResourceModelNews');
    }

}

appcodeAmiddioNewsModelResourceModelNewsGridCollection.php

namespace AmiddioNewsModelResourceModelNewsGrid;

use AmiddioNewsModelResourceModelNewsCollection as GridCollection;
use MagentoFrameworkSearchAggregationInterface;
use MagentoFrameworkApiSearchSearchResultInterface;
use MagentoFrameworkViewElementUiComponentDataProviderDocument;
use AmiddioNewsModelResourceModelNews;
use MagentoFrameworkApiSearchCriteriaInterface;

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;
    }
}

appcodeAmiddioNewsControllerAdminhtmlActionsMassDelete.php

namespace AmiddioNewsControllerAdminhtmlActions;

use MagentoFrameworkControllerResultFactory;
use MagentoBackendAppActionContext;
use MagentoUiComponentMassActionFilter;
use AmiddioNewsModelResourceModelNewsCollectionFactory;

/**
 * Class MassDelete
 */
class MassDelete extends MagentoBackendAppAction
{
    /**
     * @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 MagentoBackendModelViewResultRedirect
     * @throws MagentoFrameworkExceptionLocalizedException|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 MagentoBackendModelViewResultRedirect $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!

2

Answers


  1. Chosen as BEST ANSWER

    I found the bug im my code. In the file '...ResourceModelNewsCollection.php' I have space before 'AmiddioNewsModelNews'.

    The problem solved.


  2. In your News model (appcodeAmiddioNewsModelNews.php), your class should implement IdentityInterface.

    change:

    class News extends AbstractDb{ …

    to:

    use MagentoFrameworkDataObjectIdentityInterface;

    class News extends AbstractDb implements IdentityInterface{ ….

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search