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