3

Magento\Framework\Model\ResourceModel\Db\AbstractDb declares two methods as follows:

/**
 * After load
 *
 * @param \Magento\Framework\Model\AbstractModel $object
 * @return void
 */
public function afterLoad(\Magento\Framework\Model\AbstractModel $object)
{
    $this->_afterLoad($object);
}

/**
 * Perform actions after object load
 *
 * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\DataObject $object
 * @return $this
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
protected function _afterLoad(\Magento\Framework\Model\AbstractModel $object)
{
    return $this;
}

In a custom resource model, if I need to manipulate some data as it comes out of the database, before exposing it to the rest of the application, which of these is it best to overwrite?

If _afterLoad (with the underscore) was private, rather than protected, I'd have my answer, but I'm not sure.

| improve this question | | | | |
2

None of them.

Magento2 use models only as DTO.

If You need Some business logic you need to place it in the Module Service layer.

If you need custom persistence logic place it in the Resource Model.

| improve this answer | | | | |
  • I asked about the resource model, but I pasted code, incorrectly, from the model tree to illustrate the question. I've fixed this. Same question applies on the resource model now. – Aaron Pollock Jun 14 '16 at 11:53
  • I see that _afterLoad does nothing at all in the ResourceModel, so that probably answers it actually? – Aaron Pollock Jun 14 '16 at 11:54
  • @AaronPollock, Why it should do something? – KAndy Jun 14 '16 at 14:12
  • What I'm guessing is that if it does nothing, and its an abstract class, then that's the best one to overwrite. – Aaron Pollock Jun 14 '16 at 17:10
0

To me and according to the native Magento files you should rewrite afterLoad

Here are the reasons:

  • whenever you setup a custom backend model (or if you check the native Magento backend models) the afterLoad is always the method used
  • on top of that the _afterLoad method does not contain any Database related logic it only dispatches events

Suggestions:

  • think about using a backend model if it's only for one entity
  • depending on your needs you may want to use an observer using the events dispatched in the _afterLoad method
| improve this answer | | | | |
  • And - call the parent's method before or after your custom code so you don't override anything at all! – Robbie Averill Jun 11 '16 at 22:34
0

IN Magento 2 when whenever you need to manipulate data you should create a plug-in. Plugin's are intended to be used when you want to do some change in core class(In your case you need some data manipulation).

You create a di.xml in your module and write some ref code mentioned below

<type name="Magento\Checkout\Model\someModelGoesHere">
            <plugin name="save-delivery-details" type="Namespace\Module\Model\Checkout\someClassNameGoesHere" sortOrder="10"/>
</type>

Now once this code is written Magento will call your file when it need the Magento\Checkout\Model\someModelGoesHere

Make sure you read till bottom :-

Now in you someClassNameGoesHere file you will create a function with name of your original function that is in your parent class precedding with after/before keyword(Camel Casing).

beforeFunctionName will call before your parent function call and afterFunctionName will be called after your parent function call

You may need to set some variable dependencies in your new file so that parent function can have all its required params and this should be your approach as per Magento2, wrote more clarification so that it can help others as well when they land on this.

| improve this answer | | | | |
  • My question is regarding a custom resource model – an entirely new entity – and not a built-in Magento resource model. – Aaron Pollock Jun 14 '16 at 12: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.