0

These are my php files. magento\app\code\Cosmo\Basemodule\Model\DatiInterface.php

<?php
namespace Cosmo\Basemodule\Model;
interface DatiInterface
{

}

magento\app\code\Cosmo\Basemodule\Model\Dati.php

namespace Cosmo\Basemodule\Model;

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\DataObject\IdentityInterface;
class Dati extends AbstractModel implements DatiInterface, IdentityInterface
{
    protected function _construct()
    {
        $this->_init('Cosmo\Basemodule\Model\ResourceModel\Dati');
    }

    public function getIdentities()
    {
       //todo
    }
}

magento\app\code\Cosmo\Basemodule\Model\ResourceModel\Dati.php

<?php
namespace Cosmo\Basemodule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class News extends AbstractDb
{
    /**
     * Define main table
     */
    protected function _construct()
    {
        $this->_init('exampletable', 'entity_id');
    }
}

magento\app\code\Cosmo\Basemodule\Model\ResourceModel\Dati\Collection.php

<?php
namespace Cosmo\Basemodule\Model\ResourceModel\Dati;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    /**
     * Define model & resource model
     */
    protected function _construct()
    {
        $this->_init(
            'Cosmo\Basemodule\Model\Dati',
            'Cosmo\Basemodule\Model\ResourceModel\Dati'
        );
    }
}

And then block where i inject the factory magento\app\code\Cosmo\Basemodule\Block\Main.php

 <?php
 namespace Cosmo\Basemodule\Block;

 use Magento\Framework\View\Element\Template\Context;
 use Cosmo\Basemodule\Model\DatiFactory;

 class Main extends \Magento\Framework\View\Element\Template
 {
    protected $_datiFactory;

    public  function __construct(Context $context, DatiFactory $datiFactory)
    {
        parent::__construct($context);
        $this->_datiFactory = $datiFactory;
    }

    public function _prepareLayout()
    {
        echo 'pippo<br \>';
        $dati = $this->_datiFactory->create();
        var_dump($dati->getCollection());
        exit();
    }
}

I tried in many ways but create() fails and then nothing is executed.

1 Answer 1

1

you clearly missed the interface for model

Cosmo\Basemodule\Model\DatiInterface.php

  <?php
    namespace Cosmo\Basemodule\Model;
    interface DatiInterface 
    {

    }

and your model will be

magento\app\code\Cosmo\Basemodule\Model\Dati.php

namespace Cosmo\Basemodule\Model;

use Magento\Framework\Model\AbstractModel;

class Dati extends AbstractModel implements DatiInterface, \Magento\Framework\DataObject\IdentityInterface
{
    /**
     * Define resource model
     */
    protected function _construct()
    {
    $this->_init('Cosmo\Basemodule\Model\ResourceModel\Dati');
    }
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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