3

So I've been following along with these questions :

https://stackoverflow.com/questions/31983546/in-magento-2-what-is-the-correct-way-for-getmodel/31984198

https://stackoverflow.com/questions/31920769/how-to-save-data-using-model-in-magento2?noredirect=1&lq=1

I'm trying to create a model for a map in the database. I've created the table successfully using the Install and Update scripts. It looks something like this :

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| key   | varchar(255) | NO   |     | NULL    |                |
| value | mediumtext   | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

Now I've created the Model for this using the examples in the above questions like this :

<?php

namespace TestExtension\MyExtension\Model;

class Entry extends \Magento\Framework\Model\AbstractModel
{
  protected $_logger;

  public function __construct(
          \Magento\Framework\Model\Context $context,
          \Magento\Framework\Registry $registry,
          \Psr\Log\LoggerInterface $loggerInterface,
          \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
          \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
          array $data = []
  ) {
    $this->_logger = $loggerInterface;
    $this->_logger->debug('In model big construct.');

    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  }

  public function _construct()
  {
    $this->_logger->debug('In model small construct.');

    $this->_init( 'TestExtension\MyExtension\Model\ResourceModel\Entry' );
  }

  public function setKey( $key ) {
    $this->setData( 'key', $key );
  }

  public function setValue( $value ) {
    $this->setData( 'value', $value );
  }

  public function getKey() {
    $this->getData( 'key' );
  }

  public function getValue() {
    $this->getData( 'value' );
  }

  public function getID() {
    $this->getData( 'id' );
  }
}

And it's Resource Model like this :

<?php

namespace TestExtension\MyExtension\Model\ResourceModel;

class Entry extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
  protected $_logger;

  public function __construct ( \Psr\Log\LoggerInterface $loggerInterface ) {
    $this->_logger = $loggerInterface;
    $this->_logger->debug('In Resource Model big construct.');

    $this->_init('my_entries', 'id');
  }

  public function _construct() {
    $this->_logger->debug('In ResourceModel small construct.');
  }
}

However the example says that to save a model in the database you should to something like this :

$entry = $this->_objectManager->create('TestExtension\MyExtension\Model\Entry');
$this->_logger->debug('created.');
$entry->setKey('key1');
$this->_logger->debug('set key.');
$entry->setValue('value1');
$this->_logger->debug('set value.');
$this->_entityManager->save($entry);
$this->_logger->debug('saved.');

However the example used the model to save it, the methods save() and load() of the model were deprecated and removed in Magento 2.1. That's why I used entityManager. Both of them are injected in the constructor of the class I use for this like this :

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager, 
    \Magento\Framework\EntityManager\EntityManager $entityManager, 
    \Psr\Log\LoggerInterface $loggerInterface 
  ) {
    $this->_logger = $loggerInterface;
    $this->_objectManager = $objectManager;
    $this->_entityManager = $entityManager;
  }

The logs show :

[2016-08-05 07:17:35] main.DEBUG: In model big construct. {"is_exception":false} []
[2016-08-05 07:17:35] main.DEBUG: In model small construct. {"is_exception":false} []
[2016-08-05 07:17:35] main.DEBUG: In Resource Model big construct. {"is_exception":false} []
[2016-08-05 07:17:35] main.DEBUG: created. {"is_exception":false} []
[2016-08-05 07:17:35] main.DEBUG: set key. {"is_exception":false} []
[2016-08-05 07:17:35] main.DEBUG: set value. {"is_exception":false} []

The exception I get on the interface is :

1 exception(s):
Exception #0 (Exception): Unable to determine data interface for TestExtension\MyExtension\Model\Entry

I can't find a decent example on how to save and retrieve a model from a custom table in the database for Magento 2 that will work both for 2.0 and 2.1.

Any help would be greatly appreciated.

EDIT : I was wrong, I don't need to use EntityManager since AbstractModel does have save() : https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Model/AbstractModel.php#L629

But when I call save() on my model, it doesn't work. Any guesses as to why?

LATER EDIT AND FIX:

It was because my resource model had a __construct and it probably broke something. Left only the _construct part and it worked.

  • 2
    You can have a __construct method but you'd need to call parent::__construct inside it, passing in the correct parameters ($context, $connectionName). – Aaron Allen Aug 5 '16 at 8:41
  • @AaronAllen Ooooh. That explains it. Thank you! – Sebastian-Laurenţiu Plesciuc Aug 5 '16 at 9:42

Your Answer

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

Browse other questions tagged or ask your own question.