Categories
Tutorials

Using Flag Models in Magento 2

Last Updated on 2020-11-14 by aeno

To save a few bits of data you don’t have to create a specialized entity each time. Magento 2, just like its predecessor Magento 1, offers a Flag model. Flag models implement a small key-value store inside the Magento database.

In Magento 1 you could use the Flag model directly. For Magento 2, we have to create a class that extends the framework Flag class so we are able to set the correct FlagCode.

Flag module structure

In order to do this, create a new Magento module with a corresponding module.xml and a model we will call Flag.
A reference on how to create a new module is available in the official Magento2 Documentation.

Our module.xml does not need any special configuration. You can keep it short and sweet:

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magegyver_Sample" setup_version="1.0.0" /> </config>

The Flag model will extend \Magento\Framework\Flag and looks like this:

<?php namespace Magegyver\Sample\Model; class Flag extends \Magento\Framework\Flag { /** * Flag code * * @var string */ protected $_flagCode = 'magegyver_sample_flag'; }

That’s how we define the FlagCode for our flag. And now we can already use it in an observer, for example:

<?php namespace Magegyver\Sample\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class SaveImportantThings implements ObserverInterface { protected $flagFactory; public function __construct( \Magegyver\Sample\Model\FlagFactory $flagFactory ) { $this->flagFactory = $flagFactory; } public function execute(Observer $observer) { // initialize flag: /** @var \Magegyver\Sample\Model\Flag $flag */ $flag = $this->flagFactory->create(); $flag->loadSelf(); // set arbitrary data $flag->setFlagData( [ 'time' => time(), 'foo' => 'bar' ] ); // save data $flag->save(); } }

By the way, setFlagdata() automatically serializes our data! 🙂

After saving we can read the information quite easily:

<?php namespace Magegyver\Sample\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class LoadImportantThings implements ObserverInterface { protected $flagFactory; public function __construct( \Magegyver\Sample\Model\FlagFactory $flagFactory ) { $this->flagFactory = $flagFactory; } public function execute(Observer $observer) { // initialize flag: /** @var \Magegyver\Sample\Model\Flag $flag */ $flag = $this->flagFactory->create(); $flag->loadSelf(); // read flag data $importantThings = $flag->getFlagData(); } }

When reading data with getFlagData() it also gets deserialized automatically, so we get returned an intact PHP array.

That’s it. But please keep in mind: if you want to save complex data or you want to be able to filter or read partially, you’re better off using entities. Use flags only for small, simple bits of data 😉

Leave a Reply

Your email address will not be published.