In Magento 2, the model and database play crucial roles in the functionality and data storage of the platform. Here’s an overview of these concepts in Magento 2:
Model: In Magento 2, a model represents a single entity or object within the system. Models are responsible for managing data and performing operations related to a specific entity, such as a product, customer, order, or category. Each model typically corresponds to a database table or a group of related tables.
Models in Magento 2 follow the MVC (Model-View-Controller) architectural pattern. They encapsulate business logic, perform data validation, and interact with the database through resource models. Models handle the retrieval, manipulation, and storage of data for their respective entities.
Database: The database in Magento 2 is where the system stores and manages structured data. Magento employs a relational database management system (RDBMS) to store information related to various entities, such as products, customers, orders, and more. By default, Magento 2 uses MySQL/MariaDB as its preferred RDBMS, but it also supports other databases like Oracle and PostgreSQL.
The database schema in Magento 2 consists of a set of tables, each representing a specific entity or relationship. Magento’s database structure is designed to provide flexibility, scalability, and extensibility. It utilizes the EAV (Entity-Attribute-Value) model for dynamic entity attribute handling, allowing merchants to define custom attributes for their products and other entities.
Magento 2 employs the Magento ORM (Object-Relational Mapping) system, which abstracts the database operations and provides a convenient way to interact with the database through models and resource models. The ORM simplifies tasks such as querying, inserting, updating, and deleting records in the database.
Overall, the model and database in Magento 2 work together to handle data storage, retrieval, and manipulation. Models encapsulate business logic and interact with the database using the ORM, ensuring efficient data management and consistent interactions between the application and underlying data storage.
Create Model
The template is the class that will allow you to interact easily with your database. The contact class “Contact” with its methods. Be careful, this class should not contain SQL directly.
Create a Model directory at the root of your module (Hikmadh/Mymodule/Model) and add the Contact.php file with the following content:
_init(\Hikmadh\Mymodule\Model\ResourceModel\Contact::class);
}
}
You will notice that in the constructor, the model class will call the ResourceModel class
Create ResourceModel
The resourceModel is the class that allows you to “store” your SQL queries, it is used by the model to interract in sql with the database. Create in your Template folder a ResourceModelfolder with a Contact.php class that will contain the following code:
_init('hikmadh_mymodule', 'hikmadh_mymodule_id');
}
}
hikmadh_mymodule is the name of the hikmadh_mymodule table we created in the previous Magento2 Installer tutorial, and hikmadh_mymodule_id is the primary key to this table.
Create Collection
Let’s create the collection now. A collection, ie you have the ability to search multiple objects in the database. Basically if you create a collection of objects then you add filters that will let you load only certain objects from your table. We will come back to this later.
In the ResourceModel folder, create the Contact folder and in this folder, the Collection.php
_init('Hikmadh\Mymodule\Model\Contact', 'hikmadh\mymodule\Model\ResourceModel\Contact');
}
}
Create a contact from the Model
To test all this, go into our Index action, located in the Hikmadh\Mymodule\Controller\Test namespace. Edit this file as follows:
_objectManager->create('Hikmadh\Mymodule\Model\Contact');
$contact->setName('Hikmadh Noor');
$contact->save();
die('test');
}
}
The extended class\Magento\Framework\App\Action\Action that contains the _objectManager object that will let you instantiate your Model. So you instantiate your Model Contact, set a name with setName, and save it with the save() method.
If I had a “toto” property, I would setToto(‘my_value’). In short you understand the principle 😉
Then visit the url www.example.com/index.php/contacts/test. You should see a blank page with “test” written on it.
Creating objects
I change the code to add several contacts:
_objectManager->create('Pfay\Contacts\Model\Contact');
$contact->setName('Hikmadh Noor');
$contact->save();
$contact = $this->_objectManager->create('Pfay\Contacts\Model\Contact');
$contact->setName('Hikmadh Harun');
$contact->save();
$contact = $this->_objectManager->create('Pfay\Contacts\Model\Contact');
$contact->setName('Hikamdh Mohamed');
$contact->save();
die('test');
}
}
Visit the page and hop your 3 Contacts have been added! Then go to your database and you will see that your contact Hikmadh Noor, Hikmadh Harun and Hikmadh Mohamed was added well 😉 go and check your mysql data