2

I am studying/developing a module for Magento 2. I created an observer for a specific event and now I'd like to collect some data such as Customer information and Product information. I know I can use the magic metods get*Something* to navigate through the $data array and get specific values but how do I know what values are stored in the array? Is there a resource/documentation that lists at least the default parameters? I know it contains a lot of custom parameters from the different modules.

Thank you!

3

Like many big applications, Magento uses Object Relational Mapping(ORM) approach (https://en.wikipedia.org/wiki/Object-relational_mapping). We will use Model to get the data Object. The Model of Magento can be divided into two types: Simple Model and EAV model.

1) Simple Model: one object to one table. The attributes of Object map each fields of our table. So, in this case, when calling $yourObjectModel->getData(), it will return the field values of your table.

2) Entity Attribute Value(EAV): (really complicated) This used to present the entity with a dynamic number of attributes.

**For the EAV Model, we also can get the attributes from $objectEAVModel->getData() to check the current attributes.


Magento ORM uses a PHP's magic class methods- Magento\Framework\DataObject::__call- for Setter/Getter attribute wrapper. This is an answer to the question why we can use getData() or getData('attribute') or getCustomAttribute().

  • 1
    This is some really interesting information! Thank you for your answer! But what I am trying to understand is how do I know what I CAN get or set when my observer is triggered by an event? $observer->getData()->getWHAT?(); I'm not sure if I'm totally clear here... – Tony Morello Jan 4 '17 at 1:02
  • Thank you! I just came home from mine, so I will look it up tomorrow morning! Appreciate your help! – Tony Morello Jan 4 '17 at 1:17
  • Tomorrow, I will answer your question. I was busy. – Khoa TruongDinh Jan 4 '17 at 15:24
  • All right! Thank you I am still experimenting with it... I am trying to dump the content of $observer->getData() to see the structure... but it's HUGE... I think I am missing some concept. – Tony Morello Jan 4 '17 at 15:25
  • 1
1

Ok so I found a way to read the content of the data passed during the execution of Magento via xdebug.

Here is an interesting article on how to do it on Sublime and JetBrains (PHPstorm or IntelliJ).

Thank you Khoa for your great explanation though!

Debug screenshot

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.