1

I use Magento 2.1.9, I came across these 2 that I could not find their definitions anywhere in the code:

Interface: \Magento\CatalogInventory\Api\Data\StockItemInterface

Class: \Magento\CatalogInventory\Model\Stock\Item

Method: setStockStatusChangedAutomaticallyFlag()

Interface: \Magento\Quote\Api\Data\CartInterface

Class: \Magento\Quote\Model\Quote

Method: getInventoryProcessed() and setInventoryProcessed()

Why magento hides these, or does not include documentation.

Thanks

3

PHP comes with so called "magic methods". All Magento models utilize them, and in your case - __call

As you can see in source: vendor/magento/framework/DataObject.php

public function __call($method, $args)
{
    switch (substr($method, 0, 3)) {
        case 'get':
            $key = $this->_underscore(substr($method, 3));
            $index = isset($args[0]) ? $args[0] : null;
            return $this->getData($key, $index);
        case 'set':
            $key = $this->_underscore(substr($method, 3));
            $value = isset($args[0]) ? $args[0] : null;
            return $this->setData($key, $value);
        case 'uns':
            $key = $this->_underscore(substr($method, 3));
            return $this->unsetData($key);
        case 'has':
            $key = $this->_underscore(substr($method, 3));
            return isset($this->_data[$key]);
    }
    throw new \Magento\Framework\Exception\LocalizedException(
        new \Magento\Framework\Phrase('Invalid method %1::%2', [get_class($this), $method])
    );
}

Any class that extends DataObject class has defined __call method for setters and getters, therefore methods mentioned by you are not explicitly defined in classes but utilize this logic.

Some of them (I do not know if most of them or just a few) are usually described in class docblock however some are not.

Personally I do not like all that "magic" because it leads to questions like your.

  • can you please show me where is "setInventoryProcessed" coming from? it's not even defined in the table or interface. – Mac A. Dec 13 '17 at 23:24
  • As far as I can see in code there are only 2 occurrences of this method in code and method is not defined neither in interface nor in class. Because it is called on quote model and all models extends DataObjects then mentioned method is a "magic" setter which is the case "set" in __call method. Though you have asked this question, I think than you have missed something in documentations about "where is these methods come from". Please, re-read docs and look again in snippet above. – Bartosz Herba Dec 14 '17 at 5:57
  • so these key/values never get in the database, they live in the object within the lifetime of the instance? – Mac A. Dec 15 '17 at 1:04
  • In practice this is probably true but in theory these are two distinct things. In the end there could be no database at all. But all methods that you have listed work as flags setters and data they set are volatile. – Bartosz Herba Dec 15 '17 at 5:50

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.