I am very new to Magento, i created a CRUD custom module, when I input some data in form, and hit save button, I got an error like this:
Fatal error: Uncaught Error: Undefined class constant 'CACHE_TAG' in
C:\xampp\htdocs\test-local\app\code\Testing\SimpleNews\Model\News.php:9
Stack trace: #0 C:\xampp\htdocs\test-local\vendor\magento\framework\App\Cache\Tag\Strategy\Identifier.php(25):Testing\SimpleNew\Mode\News->getIdentities()
#1 C:\xampp\htdocs\test-local\vendor\magento\framework\App\Cache\Tag\Resolver.php(43): Magento\Framework\App\Cache\Tag\Strategy\Identifier->getTags(Object(Testing\SimpleNews\Model\News))
#2 C:\xampp\htdocs\test-local\vendor\magento\module-page-cache\Observer\FlushCacheByTags.php(64): Magento\Framework\App\Cache\Tag\Resolver->getTags(Object(Testing\SimpleNews\Model\News))
#3 C:\xampp\htdocs\test-local\vendor\magento\framework\Event\Invoker\InvokerDefault.php(72): Magento\PageCache\Observer\FlushCacheByTags->execute(Object(Magento\Framework\Event\Observer))
#4 C:\xampp\htdocs\test-local\vendor\magento\framework\Event\Invoker\InvokerDefault.php(60): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Magento\ in C:\xampp\htdocs\test-local\app\code\Testing\SimpleNews\Model\News.php on line 9
This is my htdocs\test-local\app\code\Testing\SimpleNews\Model\News.php
code:
<?php
namespace Testing\SimpleNews\Model;
class News extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
public function getDefaultValues()
{
$values = [];
return $values;
}
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('Testing\SimpleNews\Model\Resource\News');
}
}
Best Answer
In the function
getIdentities()
in your class you can see that the class constant CACHE_TAG is being used. You can read all about class constants here:I would suggest defining it at the top of your class, this cache tag let's magento know where to store cached data. An example would be: