6

I've seen at several sites having the method $this->hasData('test'); in their Block classes (not template files).

As an example, here's an extract of a method in a Block class from a tutorial site:

public function getTestModel()
{
    if (!$this->hasData('test')) {
        if ($this->getTestId()) {
            /** @var \Aion\Test\Model\Test $test */
            $test = $this->testFactory->create();
            $test->load($this->getTestId());
        } else {
            $test = $this->test;
        }
        $this->setData('test', $test);
    }
    return $this->getData('test');
}

I've searched around for this method, but I can't exactly figure out what this function does. Why would I want to store $test through $this->setData('test', $test);? I could just store it in a class variable like $this->tmpTest = $test and return the class variable, couldn't I?

Looking up the Magento source, this is the comment docs of this method in DataObject.php:

/**
 * If $key is empty, checks whether there's any data in the object
 * Otherwise checks if the specified attribute is set.
 *
 * @param string $key
 * @return bool
 */

But what's $key and what attribute is this referring to?

What am I actually checking with the hasData() method? The example I saw took in the word test as its parameter but I have no idea what the word "test" relates to.

What is that parameter the $this->hasData() method actually takes in?

Why would I even want to use this method?

7

every class that extends the DataObject class contains a member called _data.
From here you can get the values using $this->getData('key_here').
That will return _data['key_here'].
The hasData method checks if that key is set.
For example $this->hasData('test') is an equivalent for array_key_exists('test', $this->_data).
Here is where the method is defined.

If your model is a flat table model (all values are kept in a single table) the key should be one of the column names.
For example when having a cms page model, $this->hasData('identifier') will check if the page identifier is set.
If the model is an EAV model, the key should be one of the entity attributes.
For example for products, $this->hasData('name') will check if the product name is set.

But, in general you can set anything to the _data member on any model using $this->setData('some_key', 'some_value').
In this case, the key can be any string.
Then you can check if the data for a certain key is set with $this->hasData('some_key');

  • Is the key any arbitrary string? Or does this "key" relate to any thing such as id or model names, etc in my code? – xenon Nov 16 '16 at 8:32
  • See my update to the answer. – Marius Nov 16 '16 at 8:37
  • So, let's say I have a flat table model. And in my table, I have a column called "test_id". When I call $this->hasData('test_id'), I should be getting a true value from it? – xenon Nov 16 '16 at 8:59
  • yes. If that value is set you get true out of it. – Marius Nov 16 '16 at 9:00
  • umm... sorry, the part u said "if that value is set..." got me stumble a bit. I actually have a test_id column in my table but when I call $this->hasData('test_id'), I get a false value. Is there something else I have to call to get all the table values set so that when I call hasData('test_id') will return me a true value? Sorry if I'm sounding silly; I'm very new to magento. – xenon Nov 16 '16 at 9:03

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.