Skip to content

Instantly share code, notes, and snippets.

@cosmomathieu
Last active July 31, 2018 20:17
Show Gist options
  • Save cosmomathieu/d7a56ed7f3eae7b12181614afbe62641 to your computer and use it in GitHub Desktop.
Save cosmomathieu/d7a56ed7f3eae7b12181614afbe62641 to your computer and use it in GitHub Desktop.
Models in PS

Resource Models

Executing all CRUD (create, read, update, delete) requests. The resource model contains the SQL code for completing these requests.

Performing additional business logic. For example, a resource model could perform data validation, start processes before or after data is saved, or perform other database operations.

Resource Model Collections

If you are expecting to return more than one record from a database then you should use a resource model collection.

Inspiration: https://docs.zendframework.com/tutorials/getting-started/database-and-models/

<?php
class ArticleModel extends ResourceModel
{
}
class BlogCollectionModel extends ResourceModelCollection
{
}
class BlogModel extends EavModel
{
}
class AbstractBaseModel
{
}
class BaseModel extends AbstractBaseModel
{
public function __construct()
{
parent::__construct();
}
}

Persistence layer

Magento uses an active record pattern strategy for persistence. In this system, the model object contains a resource model that maps an object to one or more database rows. A resource model is responsible for performing functions such as:

Executing all CRUD (create, read, update, delete) requests. The resource model contains the SQL code for completing these requests.

Performing additional business logic. For example, a resource model could perform data validation, start processes before or after data is saved, or perform other database operations.

If you expect to return multiple items from a database query, then you would implement a special type of resource model known as a collection. A collection is a class that loads multiple models into an array-like structure based on a set of rules. This is similar to a SQL WHERE clause.

A simple resource model defines and interacts with a single table.

However, some objects have a vast number of attributes, or they could have a set related objects that have varying numbers of attributes. In these cases, the objects are constructed using Entity-Attribute-Value (EAV) models.

Any model that uses an EAV resource has its attributes spread out over a number of MySQL tables.

The Customer,Catalog and Order resource models use EAV attributes.

More Info

https://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment