- Magento 2 Developer's Guide
- Branko Ajzele
- 320字
- 2021-07-30 09:53:24
Chapter 4. Models and Collections
Like most modern frameworks and platforms, these days Magento embraces an Object Relational Mapping (ORM) approach over raw SQL queries. Though the underlying mechanism still comes down to SQL, we are now dealing strictly with objects. This makes our application code more readable, manageable, and isolated from vendor-specific SQL differences. Model, resource, and collection are three types of classes working together to allow us full entity data management, from loading, saving, deleting, and listing entities. The majority of our data access and management will be done via PHP classes called Magento models. Models themselves don't contain any code for communicating with the database.
The database communication part is decoupled into its own PHP class called resource class. Each model is then assigned a resource class. Calling load
, save
, or delete
methods on models get delegated to resource classes, as they are the ones to actually read, write, and delete data from the database. Theoretically, with enough knowledge, it is possible to write new resource classes for various database vendors.
Next to the model and resource classes, we have collection classes. We can think of a collection as an array of individual model instances. On a base level, collections extend from the \Magento\Framework\Data\Collection
class, which implements \IteratorAggregate
and \Countable
from Standard PHP Library (SPL) and a few other Magento-specific classes.
More often than not, we look at model and resource as a single unified thing, thus simply calling it a model. Magento deals with two types of models, which we might categorize as simple and EAV models.
In this chapter, we will cover the following topics:
- Creating a miniature module
- Creating a simple model
- The EAV model
- Understanding the flow of schema and data scripts
- Creating an install schema script (
InstallSchema.php
) - Creating an upgrade schema script (
UpgradeSchema.php
) - Creating an install data script (
InstallData.php
) - Creating an upgrade data script (
UpgradeData.php
) - Entity CRUD actions
- Managing collections
- 數(shù)據(jù)庫系統(tǒng)教程(第2版)
- Python從小白到大牛
- Access 數(shù)據(jù)庫應(yīng)用教程
- Python計算機(jī)視覺編程
- Access 2016數(shù)據(jù)庫管
- Mastering ServiceNow(Second Edition)
- Linux Device Drivers Development
- Cocos2d-x學(xué)習(xí)筆記:完全掌握Lua API與游戲項目開發(fā) (未來書庫)
- Mastering Web Application Development with AngularJS
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- C指針原理揭秘:基于底層實(shí)現(xiàn)機(jī)制
- Delphi開發(fā)典型模塊大全(修訂版)
- 從零開始:C語言快速入門教程
- 現(xiàn)代CPU性能分析與優(yōu)化
- After Effects CC技術(shù)大全