自学内容网 自学内容网

Magento2如何创建CRUD Models

Mageno2 Model的创建不同于其他框架,需要3个不同目录层级的文件

例如需要为表hello_test创建model:

1、app/code/Hello/Test/Model/Test.php

<?php

namespace Hello\Test\Model;

class Test extends \Magento\Framework\Model\AbstractModel
{
    protected $_idFieldName = 'id';

    protected function _construct()
    {
        $this->_init('\Hello\Test\Model\ResourceModel\Test');
    }
}

2、app/code/Hello/Test/Model/ResourceModel/Test.php

<?php

namespace Hello\Test\Model\ResourceModel;

class Test extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    public function _construct()
    {
        // TODO: Implement _construct() method.
        $this->_init('hello_test', 'id');
    }
}

3、app/code/Hello/Test/Model/ResourceModel/Test/Collection.php

<?php

namespace Hello\Test\Model\ResourceModel\Test;

use \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    public function _construct()
    {
        $this->_init('\Hello\Test\Model\Test', '\Hello\Test\Model\ResourceModel\Test');
    }
}


原文地址:https://blog.csdn.net/T_Struggle/article/details/144253340

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!