【问题标题】:In magento 2 what is the correct way for getModel?在 magento 2 中,getModel 的正确方法是什么?
【发布时间】:2016-07-20 21:44:27
【问题描述】:

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

【问题讨论】:

标签: magento


【解决方案1】:

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

  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');
    }
    }
    
  2. 在 ResourceModel 文件夹中为问题资源模型创建Question.php,如下所示:

    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. 在资源模型/问题文件夹中为问题收集模型创建Collection.php,如下所示:

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

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

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

设置脚本:

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

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

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

    `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

不再有单独的版本设置文件,每个操作只有一个类。

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

【讨论】:

  • 非常感谢您的帮助.. 但是在 phpmyadmin 数据库中无法创建
  • 在 magento 1.x 中,调用 config.xml 文件后会自动创建数据库。对吗?那么在 magento 2 中怎么可能呢?
  • 我在本地 cmd php bin/magento setup:upgrade 中使用了这个命令。模​​块升级后 ..magento 会将条目放入 setup_module 表中。但我的表不是在 phpmyadmin 中创建的跨度>
  • 此行中定义的 $this->_objectManager '_objectManager' 在哪里,我得到 Magento\Framework\ObjectManager\ObjectManager 类的对象无法转换为字符串....这是我的代码
  • $sku='OUTIPCAM1'; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $currentproduct=$this->$objectManager->create('Vendor\Module\Model\Queue'); $currentproduct->setEntity_Id('1'); $currentproduct->save();
猜你喜欢
  • 2015-02-10
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多