Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In magento 2 what is the correct way for getModel?

Tags:

magento

i was make helloworld simple module in Magento 2 successfully.. Now i want to get Model data from the database...so please help me for getting model in Magento 2. Any help would be appreciated.

like image 233
Niks Avatar asked Dec 08 '22 02:12

Niks


1 Answers

Here is the steps for creating Model in Magento 2 Module:

  1. Create Question.php in Model Folder For Question Model as follows:

    namespace Ecom\HelloWorld\Model;
    
    class Question extends \Magento\Framework\Model\AbstractModel
    {
    public function __construct(
            \Magento\Framework\Model\Context $context,
            \Magento\Framework\Registry $registry,
            \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
            \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
            array $data = []
    ) {
        parent::__construct($context, $registry, $resource, $resourceCollection, $data);
    }
    
    public function _construct()
    {
        $this->_init('Ecom\HelloWorld\Model\ResourceModel\Question');
    }
    }
    
  2. Create Question.php in ResourceModel Folder For Question Resource Model as follows:

    namespace Ecom\HelloWorld\Model\ResourceModel;
    
    class Question extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
    {
    public function _construct()
    {
        $this->_init('question_table_name', 'question_id');
    }
    }
    
  3. Create Collection.php in ResourceModel/Question Folder For Question Collection Model as follows:

    namespace Ecom\HelloWorld\Model\ResourceModel\Question;
    
    class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
    {
    public function _construct()
    {
    $this->_init('Ecom\HelloWorld\Model\Question', 'Ecom\HelloWorld\Model\ResourceModel\Question');
    }
    }
    

Now you can call the Model in the following way:

$question = $this->_objectManager->create('Ecom\HelloWorld\Model\Question');
$question->setTitle('Simple Question');
$question->setDescription('Question Description');
$question->save();

For Set Up Script:

There are 2 different types of install scripts. A schema install and a data install. A schema install is used to install database structures like new tables, columns, relations. A data install or upgrade is used to add data to the database like a setting, page etc.

If Module is already created you need to craete 'UpgradeSchema.php' file in set up folder and add new database structure for update. If module is not installed you need to create 'InstallSchema.php' to add new database structure.

To simplify, in Magento 2 you can have 6 different Setup classes in your module:

    `Setup/InstallSchema` - Script that needs to run to create database schema when module installed
    `Setup/UpgradeSchema` - Script that needs to run to update or createdatabase schema when module upgraded 
    `Setup/InstallData` - Data Import when module installed
    `Setup/UpgradeData` - Data Import when module upgraded
    `Setup/Recurring` - Script run everytime when module upgrade
    `Setup/Uninstall` - Script run when Module uninstalled

There are no separate version setup files anymore, only one class per action.

After Making all changes you need to run the command: php bin/magentosetup:upgrade

like image 166
Pankaj Pareek Avatar answered Jan 01 '23 18:01

Pankaj Pareek