Magento getData() vs getFieldName() in model
-
09-04-2021 - |
문제
After load('id')
function in model, we can get data by either model->getData('field_name')
or model->getFieldName()
.
I know that both functions are provided by DataObject
class but what I want to know is
- Is there any different between them ?
- What should I use ?
해결책
Yes there is a bit of different, model->getFieldName() is a magic method so this will be public and in Magento 2 we can create plugins to manipulate output of public methods
(i.e) Lets assume
- value of field_name is foo
- there is plugin(https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html) created which changes the value to bar
- model->getData('field_name') will return foo
- model->getFieldName() will return bar
It is always advisable to use model->getFieldName() so can take advantage of plugins in Magento 2.
I Hope this explanation helps.
제휴하지 않습니다 magento.stackexchange