0

Magento 2 Coding standard provide me Warning

Only dependency assignment operations are allowed

My Code is here

namespace Namespace\Modulename\Model;

use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Ui\DataProvider\AbstractDataProvider;
use Namespace\Modulename\Model\ResourceModel\Customoption\CollectionFactory;

class CustomoptionProvider extends AbstractDataProvider
{
    public $collection;
    public $dataPersistor;
    public $loadedData;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $pageCollectionFactory,
        DataPersistorInterface $dataPersistor,
        array $meta = [],
        array $data = []
    ) {
        $this->collection    = $pageCollectionFactory->create();
        $this->dataPersistor = $dataPersistor;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    public function getData()
    {
        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $page) {
            ...
        }
        $data = $this->dataPersistor->get('module_messages');
        if (!empty($data)) {
            $page = $this->collection->getNewEmptyItem();
            $page->setData($data);
            $this->loadedData[$page->getId()] = $page->getData();
            $this->dataPersistor->clear('module_messages');
        }
        return $this->loadedData;
    }
}

I had use this

$this->collection = $pageCollectionFactory;

$this->collection = $this->collection->create();

but it provide me error

Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught Error: Call to undefined method Namespace\Modulename\Model\ResourceModel\Customoption\CollectionFactory::addFieldToFilter() in D:\xampp72\htdocs\mage231\vendor\magento\module-ui\Component\Wrapper\UiComponent.php on line 0

if have any solution then provide for this issue.

1

Add this member to your class

protected $collectionFactory;

In your constructor add this

$this->collectionFactory = $pageCollectionFactory;

and remove this

$this->collection    = $pageCollectionFactory->create();

then add this method

public function getCollection()
{
    if ($this->collection === null) {
        $this->collection = $this->collectionFactory->create();
    }
    return $this->collection;
}

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.