In magento 2 what is the correct way for getModel?

In magento 2 what is the correct way for getModel?

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
In magento 2 what is the correct way for getModel?

Getting Started with Magento 2: Accessing Database Models

Hello! Welcome back to Laravel Company’s Magento tutorials series. Today, we’re going to tackle one of the most common questions new Magento developers face: how do you access your module’s models and fetch data from the database? Let’s dive right in.

Understanding Magento 2 Models

In Magento 2, a model is essentially a wrapper around your database table that provides methods for interacting with your data. It handles all the heavy lifting of connecting to the database, executing queries, and mapping results back into PHP objects.

When you create a new module, Magento automatically generates a corresponding model for you based on your etc/db_schema.xml file. This is where you define your table structure, primary keys, indexes, etc.

Retrieving Your Model

Now let’s say you have a simple module called ‘HelloWorld’ with a single entity type defined in the etc/extension_attributes.xml file:

<extension_attributes for="Magento_HelloWorld_API_Model_Entity" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <attribute code="message" type="varchar" sort_order="1" group="general" label="Message"/>
</extension_attributes>

And you want to fetch data from this entity. Here’s how you can get an instance of your model:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$entityModel = $objectManager->create('Magento_HelloWorld\Model\Entity');

Note: Using the object manager directly like this is usually not recommended in production code. It’s better to use dependency injection through your constructor or use a factory class. But for simplicity’s sake, we’ll keep it simple here.

Fetching Data from the Database

Once you have your $entityModel instance, you can start calling its methods to interact with the database:

$collection = $entityModel->getCollection();

The getCollection() method returns a collection of all records currently in the table. You can add filters, order by fields, or even join other tables using this collection.

Example: Fetching All HelloWorld Messages

For example, let’s say you want to fetch all the ‘message’ attributes from your HelloWorld entities:

$collection = $entityModel->getCollection();
$messages = [];

foreach ($collection as $item) {
    $messages[] = $item->getMessage();
}

print_r($messages);

This will print an array of all messages currently in the database.

Conclusion

That’s it! You now know how to access your Magento 2 models and fetch data from them. Remember, though:

  • Always try to use dependency injection when possible.
  • Use Magento’s built-in repositories instead of directly accessing the object manager (learn more here).
  • Be mindful of potential security risks and performance implications.

If you found this tutorial helpful, consider checking out our other Magento 2 tutorials at Laravel Company. We cover everything from module development to theme customization. Happy coding!

Magento 2 Database API Extensions

Our network