如何使用Magento2中的模型保存数据

时间:2015-08-10 13:23:43

标签: magento magento2

我已准备好基本模块,控制器和视图完美运行。现在,我正在尝试启动模型,以便在包含属性的表中使用自定义模型保存数据(问题标题,问题)。基本上,为了通过模型在自定义表中保存数据,我应该采取哪些步骤?

我该怎么做,任何帮助都会有很大的帮助。

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

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,如果您想查看,请查找以下网址:

FAQ with custom grid extension in Magento 2

1 个答案:

答案 0 :(得分:11)

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

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

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

更新了答案

按如下方式添加问题模型:

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

同样请刷新缓存。

请关注此处:In magento 2 what is the correct way for getModel?