如何在Magento2中使用模型保存数据 [英] How to save data using Model in Magento2

查看:306
本文介绍了如何在Magento2中使用模型保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经准备好一个带有控制器的基本模块,并且视图工作正常.现在,我正在尝试初始化模型,以便使用包含属性(问题标题,问题)的表中的自定义模型保存数据.基本上,为了通过模型在自定义表中保存数据,我应该执行哪些步骤?

I have a basic module ready with controller and view working perfectly. Now, I am trying to initiate model in order to save data using custom model in table containing attributes (question Title, Question). Basically, what steps I should proceed in order to save data through model in a custom table ?

我应该怎么做,将不胜感激.

How should I do it, any help would be greatly appreacited.

我的操作文件中包含以下代码:

I have below code in my action file :

class Post extends \Magento\Framework\App\Action\Action

{
  protected $_objectManager;

  public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager) 
  {
    $this->_objectManager = $objectManager;

  }

 public function execute()
  {
     $post = $this->getRequest()->getPostValue();
     $model = $this->_objectManager->create('Chirag\Mygrid\Model\Question');
     $model->setData('question_title', $post['question_title']);
     $model->setData('question', $post['question']);
     $model->save();


  }
}

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\Mygrid\Model;

class Question extends \Magento\Framework\Model\AbstractModel
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('Chirag\Mygrid\Model\Resource\Question');
    }
}


<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\Mygrid\Model\Resource;

class Question extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
    /**
     * Initialize resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('questions_and_answers', 'question_id');
    }
}


<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\Mygrid\Model\Resource\Question;

class Collection extends \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
{
    protected function _construct()
    {
        $this->_init('Chirag\Mygrid\Model\Question', 'Chirag\Mygrid\Model\Resource\Question');
        $this->_map['fields']['page_id'] = 'main_table.page_id';
    }

    /**
     * Prepare page's statuses.
     * Available event cms_page_get_available_statuses to customize statuses.
     *
     * @return array
     */
    public function getAvailableStatuses()
    {
        return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
    }
}

第一次修改:

我已成功完成此操作,并将整个设置上传到Git,请在下面的URL中查找,以防万一:

I had successfully completed this and have uploaded the whole setup to Git, please find below URL in case you want to check it out :

在Magento 2中使用自定义网格扩展的常见问题解答

推荐答案

您可以像这样在Magento 2中保存数据:

You can save Data like this way in Magento 2:

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

这是您将在特定操作中添加的代码

this is the code that you will add in your particular action

更新后的答案

添加您的问题模型,如下所示:

Add your question model as follows:

namespace Chirag\Mygrid\Model;

class Question extends \Magento\Framework\Model\AbstractModel
{
public function __construct(
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Model\Resource\AbstractResource $resource = null,
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
        array $data = []
) {
    parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}

public function _construct()
{
    $this->_init('Chirag\Mygrid\Model\Resource\Question');
}
}

还请刷新缓存.

请也遵循此处:在magento 2 getModel的正确方法是什么?

这篇关于如何在Magento2中使用模型保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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