Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lbajsarowicz opened this issue May 19, 2020 · 3 comments
Closed

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

lbajsarowicz opened this issue May 19, 2020 · 3 comments
Labels
accepted New rule is accepted Progress: done

Comments

@lbajsarowicz
Copy link

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

@lbajsarowicz lbajsarowicz added the proposal New rule proposal label May 19, 2020
@lenaorobei lenaorobei added accepted New rule is accepted and removed proposal New rule proposal labels May 19, 2020
@milindsingh milindsingh self-assigned this May 20, 2020
@milindsingh
Copy link
Member

milindsingh commented May 21, 2020

@lbajsarowicz what should be the severity for this case?

@lbajsarowicz
Copy link
Author

@milindsingh I'm really sorry, but I have no idea :-(

@lenaorobei
Copy link
Contributor

@milindsingh it should be 8 like for other Magento specific issues. Please see https://github.com/magento/magento-coding-standard/wiki/Magento-Marketplace-Extensions-Verification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted New rule is accepted Progress: done
Projects
None yet
Development

No branches or pull requests

3 participants