Skip to content

Do not use getResource()->save() in Model #187

Closed
@lbajsarowicz

Description

@lbajsarowicz

Rule

getResource() use is deprecated and Resource Model should be injected with DI to the Model class.

Reason

Most of the Mageto 2 tutorials and stackoverflow answers:

  1. https://www.mage2.tv/content/setup-scripts/adding-a-custom-eav-attribute/adding-an-input-filter-to-a-customer-or-address-attribute/
  2. https://magento.stackexchange.com/questions/163933/good-practice-how-to-save-many-objects
  3. https://magento.stackexchange.com/questions/46369/loading-just-an-attribute-from-a-model-and-saving-it

provide getResource()->save($object) as recommended way to persist data changes on model - from model itself. As use of getResource() is deprecated, and ResourceModel should be initialized using Dependency Injection, we should introduce inspection for PHP CS to avoid using getResource() in models and setup scripts.

Implementation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions