1、model/ResourceModel/Material.php
1)继承Db下的AbstractDb
2)通过init方法把表格和主键传给resourcemodel,就可以处理数据。
<?php
namespace Exercises\Redo\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Material extends AbstractDb{
protected function _construct(){
$this->_init('reclothing_materials','material_id');
}
}
2、model/ResourceModel/Material/Collection.php
1)use中的as是设置别名(因为类名与前面重复)
2)继承abstractCollection
3)设置主键
4)init方法把Material的class和MaterialResource的calss作为参数传给collection
class Collection extends AbstractCollection{
protected $_idFieldName='material_id';
protected function _construct(){
$this->_init(Material::class, MaterialResource::class)
}
}
3、model/MaterialRepository.php
1)用来实现MaterialRepositoryInterface
2)通过构造函数引入collectionFactory(magento自动生成)
3)create返回一个collection对象
private $collectionFactory;
private $materialFactory;
public function __construct(CollectionFactory $collectionFactory, MaterialFactory $materialFactory)
{
$this->collectionFactory = $collectionFactory;
$this->materialFactory = $materialFactory;
}
public function getList()
{
return $this->collectionFactory->create()->getItems();
}
}
4、Api/MaterialRepositoryInterface.php
1)service contracts进行请求时,对外只展示getList()方法
2)getList返回实例全部继承与MaterialInterface
<?php
namespace Exercises\Redo\Api;
interface MaterialRepositoryInterface{
public function getList();
}
5、Api/Data/MaterialInterface.php
1)getList方法返回实例全部继承于此
<?php
namespace Exercises\Redo\Api\Data;
interface MaterialInterface
{
/**
* @return string
*/
public function getMaterialId();
/**
* @return string
*/
public function getMaterial();
public function setMaterialId($id);
public function setMaterial($material);
6、etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference type="Exercises\Redo\Model\Material" for="Exercises\Redo\Api\Data\MaterialInterface"/>
<preference type="Exercises\Redo\Model\MaterialRepository" for="Exercises\Redo\Api\MaterialRepositoryInterface"/>
</config>
7、model/Source/Material.php
getAllOptions()和getOptionText($value)均为公共函数;getAllOptions提供所有可用的选项列表
<?php
/**
* Copyright ? Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Exercises\Redo\Model\Source;
use Exercises\Redo\Api\MaterialRepositoryInterface;
use Psr\Log\LoggerInterface;
class Material extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
protected $materialRepository;
private $logger;
public function __construct(MaterialRepositoryInterface $materialRepository,LoggerInterface $logger)
{
$this->materialRepository = $materialRepository;
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function getAllOptions()
{
$results = $this->materialRepository->getList();
//$this->logger->debug("Router class: ".$results);
foreach($results as $material){
$this->_options[] = [
'value' => $material->getMaterialId(),
'label' => $material->getMaterial(),
];
}
array_unshift($this->_options, ['value' => '', 'label' => __('None')]);
return $this->_options;
/*array_unshift($options, ['value' => '', 'label' => __('No layout updates')]);
$this->_options = $options;
return $options;*/
/*$this->logger->info('333222$results');*/
}
public function getOptionText($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option['value'] == $value) {
return $option['label'];
}
}
return false;
}
}
8、应该在1-2中间的过程
model/Material.php
1)init唯一要传入的就是resourcemodel是哪一个
2)resourcemodel需要知道自己的database是哪个
3)只能处理单个实体
protected function _construct()
{
$this->_init(\Exercises\Redo\Model\ResourceModel\Material::class);
}
over