且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在magento 2中,getModel的正确方法是什么?

更新时间:2023-11-30 10:36:34

以下是在Magento 2模块中创建模型的步骤:

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

  1. 在问题"模型的模型"文件夹中创建Question.php,如下所示:

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');
}
}

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

  • 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');
    }
    }
    

  • 在ResourceModel/问题文件夹中为问题收集模型创建Collection.php,如下所示:

  • 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();
    

    对于设置脚本:

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

    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种不同的安装程序类:

    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