1

I am new to magento and learning to create a module in magento.

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught TypeError: Argument 1 passed to Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider::searchResultToOutput() must implement interface Magento\Framework\Api\Search\SearchResultInterface, instance of Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber\Collection given, called in E:\wamp64\www\utt\vendor\magento\framework\View\Element\UiComponent\DataProvider\DataProvider.php on line 284 in E:\wamp64\www\utt\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0

my di.xml file is as below :

</p>

<virtualType name="Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">wk_grid_records</argument>
        <argument name="resourceModel" xsi:type="string">Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber</argument>
    </arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="productserialnumber_record_productserialnumber_list_data_source" xsi:type="string">Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber\Collection</item>
        </argument>
    </arguments>
</type>
<type name="Chirpn\ProductSerialNumber\Logger\Handler">
    <arguments>
        <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
    </arguments>
</type>
<type name="Chirpn\ProductSerialNumber\Logger\Logger">
    <arguments>
        <argument name="name" xsi:type="string">customLogHandler</argument>
        <argument name="handlers"  xsi:type="array">
            <item name="system" xsi:type="object">Chirpn\ProductSerialNumber\Logger\Handler</item>
        </argument>
    </arguments>
</type>

Amit Naraniwal
  • 1,298
  • 10
  • 20
  • Please check this link:https://magento.stackexchange.com/questions/162220/ui-template-adminhtml-issue – Vishal Feb 27 '19 at 07:36

1 Answers1

0

You Need To create Collection File For Grid

<?php

namespace Vendorname\modulename\Model\ResourceModel\ProductSerialNumber\Grid;

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Framework\Api\Search\AggregationInterface;
use Psr\Log\LoggerInterface;
use vendorname\modulename\Model\ResourceModel\ProductSerialNumber\Collection as ProductSerialNumberCollection;


class Collection extends ProductSerialNumberCollection implements SearchResultInterface
{
    /**
     * Aggregations
     *
     * @var AggregationInterface
     */
    protected $aggregations;

    /**
     * Collection constructor.
     * @param EntityFactory $entityFactory
     * @param LoggerInterface $logger
     * @param FetchStrategyInterface $fetchStrategy
     * @param EventManagerInterface $eventManager
     * @param $mainTable
     * @param $eventPrefix
     * @param $eventObject
     * @param $resourceModel
     * @param AdapterInterface|null $connection
     * @param AbstractDb|null $resource
     * @param string $model
     */
    public function __construct(
        EntityFactory $entityFactory,
        LoggerInterface $logger,
        FetchStrategyInterface $fetchStrategy,
        EventManagerInterface $eventManager,
        $mainTable,
        $eventPrefix,
        $eventObject,
        $resourceModel,
        AdapterInterface $connection = null,
        AbstractDb $resource = null,
        $model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document'
    )
    {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $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 SearchResultInterface|void
     */
    public function setAggregations($aggregations)
    {
        $this->aggregations = $aggregations;
    }


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

    /**
     * @return \Magento\Framework\Api\Search\SearchCriteriaInterface|null
     */
    public function getSearchCriteria()
    {
        return null;
    }

    /**
     * Set search criteria.
     *
     * @param SearchCriteriaInterface $searchCriteria
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setSearchCriteria(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;
    }
}

di.xml

From

<arguments>
            <argument name="mainTable" xsi:type="string">wk_grid_records</argument>
            <argument name="resourceModel" xsi:type="string">Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber</argument>
</arguments>

To

    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="wk_grid_records" xsi:type="string">Chirpn\ProductSerialNumber\Model\ResourceModel\ProductSerialNumber\Grid\Collection</item>
        </argument>
    </arguments>
Ronak Rathod
  • 6,322
  • 17
  • 42