在 magento 2 中,getModel 的正确方法是什么? [英] In magento 2 what is the correct way for getModel?

查看:19
本文介绍了在 magento 2 中,getModel 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地在 Magento 2 中制作了 helloworld 简单模块..现在我想从数据库中获取模型数据...所以请帮助我在 Magento 2 中获取模型.任何帮助,将不胜感激.

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.

推荐答案

以下是在 Magento 2 Module 中创建 Model 的步骤:

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

  1. 在模型文件夹中为问题模型创建Question.php如下:

namespace EcomHelloWorldModel;

class Question extends MagentoFrameworkModelAbstractModel
{
public function __construct(
        MagentoFrameworkModelContext $context,
        MagentoFrameworkRegistry $registry,
        MagentoFrameworkModelResourceModelAbstractResource $resource = null,
        MagentoFrameworkDataCollectionAbstractDb $resourceCollection = null,
        array $data = []
) {
    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

public function _construct()
{
    $this->_init('EcomHelloWorldModelResourceModelQuestion');
}
}

  • 在ResourceModel文件夹中为问题资源模型创建Question.php如下:

    namespace EcomHelloWorldModelResourceModel;
    
    class Question extends MagentoFrameworkModelResourceModelDbAbstractDb
    {
    public function _construct()
    {
        $this->_init('question_table_name', 'question_id');
    }
    }
    

  • 在ResourceModel/Question文件夹中为问题集合模型创建Collection.php如下:

    namespace EcomHelloWorldModelResourceModelQuestion;
    
    class Collection extends MagentoFrameworkModelResourceModelDbCollectionAbstractCollection
    {
    public function _construct()
    {
    $this->_init('EcomHelloWorldModelQuestion', 'EcomHelloWorldModelResourceModelQuestion');
    }
    }
    

  • 现在您可以通过以下方式调用模型:

    Now you can call the Model in the following way:

    $question = $this->_objectManager->create('EcomHelloWorldModelQuestion');
    $question->setTitle('Simple Question');
    $question->setDescription('Question Description');
    $question->save();
    

    对于设置脚本:

    有两种不同类型的安装脚本.架构安装和数据安装.模式安装用于安装数据库结构,如新表、列、关系.数据安装或升级用于向数据库添加数据,如设置、页面等.

    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.

    如果模块已经创建,您需要在设置文件夹中创建UpgradeSchema.php"文件并添加新的数据库结构以进行更新.如果未安装模块,则需要创建InstallSchema.php"以添加新的数据库结构.

    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.

    为了简化,在 Magento 2 中,您的模块中可以有 6 个不同的 Setup 类:

    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.

    进行所有更改后,您需要运行以下命令:php bin/magentosetup:upgrade

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

    这篇关于在 magento 2 中,getModel 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆