6

Today, I have reviewed the the catalog-rule modules and found the a method named loadPost on controller during save. I don't know how it works but it saved the rule data in serialized form in database. Before save, it converts the actions or conditions array data into serialized form.

Can anyone explain in brief that What is use of this method and why does magento use this method ?

Thanks in advance.

2
  • My answer was helpful for you? Do you have any other questions about the loadPost? Oct 11, 2017 at 7:27
  • I'm glad to help you :) Oct 11, 2017 at 7:29

1 Answer 1

3

This method is used for one simple purpose: to convert a text-typed values ​​to the corresponding objects, if necessary. Typically, these objects are serialized data, such as conditions or actions or fields that store the date (as a string).

If you do not do this conversion, then in the model the date field will be represented exclusively in text form (as a string), and some methods will not be able to work with it correctly, because expect not the text but the date object.

If you have any data that can be submitted as text and sent through a form, you can also add their conversion in this method, and you can be sure that they will always be presented in the desired form (as an object).

Example:

  1. Conditions as an array when form was submitted (in controller, before the loadPost()):

    Conditions as an array when form was submitted

  2. from_date and to_date (in controller, before the loadPost()):

    from and to date in controller

  3. Conditions after _convertFlatToRecursive() in the loadPost() method:

    Conditions after _convertFlatToRecursive

    as you can see format is different.

  4. Conditions inside the object (rule) before they was set:

    Conditions inside the rule model

  5. Conditions inside the object (rule) after they was set:

    Conditions inside the rule model

    as you can see, it's completely working object with own data and methods, is not just array.

  6. Date field after the loadPost(), it is DateTime object:

    Date field

So, in case you want to work with a rule-model somewhere in the afterSave method or in observer you can use this objects and should not worry about "How should I convert these values?".

5
  • Thanks.Got the point. It simply converts the data objects in to the string to save in database. Right ? Oct 9, 2017 at 6:35
  • 1
    @zedBlackbeard No, it is converts a form-data to the objects to give you ability work with them and save them later in the database using own methods to convert an objects to database field values. Oct 9, 2017 at 6:45
  • @SiarheyUchukhlebau in my controller I save post data using the repository, in the repository there is no method like loadpost so how can I convert condition rule array in to serialize form and save to database. Thanks:) Aug 31, 2019 at 4:46
  • 1
    @ChiragPatel You can use the loadArray method of the Condition object, like in the original loadPost method: $this->getConditions()->setConditions([])->loadArray($arr['conditions'][1]); Sep 2, 2019 at 9:49
  • Thanks for the valuable time, Actually I have an issue is loadPost method is not providing a serialized value. can you please look into my questions magento.stackexchange.com/questions/287102/… Sep 2, 2019 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.