• 根据product_id 获取 category_ids :

        /**
    * @param $product_id
    * @return array
    */
    public function mc_getCategoryIds($product_id)
    {
    // +++ get product the category id
    $registry = $this->mc_C['product'];
    $getObj = $this->mc_get_obj($registry);
    /**
    * @var \Magento\Catalog\Model\Product $getObj
    * \Magento\Catalog\Model\Product
    */
    $product = $getObj->load($product_id); /**
    * @var \Magento\Catalog\Model\Product $product
    * \Magento\Catalog\Model\Product
    */ return $product->getCategoryIds();
    }
  • 获取 category_name :
    /**
    * @param $product_id
    * @return mixed
    */
    public function mc_getCategoryName($product_id)
    {
    $cats = $this->mc_getCategoryIds($product_id);
    if(count($cats) ){
    $firstCategoryId = $cats[(count($cats)-1)];
    $registry = $this->mc_C['category'];
    $getObj = $this->mc_get_obj($registry); /**
    * @var \Magento\Catalog\Model\CategoryFactory $getObj
    */
    $_category = $getObj->create();
    /**
    * @var \Magento\Catalog\Model\CategoryFactory $_category
    */
    $data = $_category->load($firstCategoryId);
    /**
    * @var \Magento\Catalog\Model\CategoryFactory $data
    */
    return $data->getName();
    }
    return 0;
    }

以上简单的调用了栏目的名称。

FAQ:

1,如果使用不正确的 CategoryFactory 类

  • 're_category_c'=>\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class,//fixme This class \Magento\Catalog\Model\ResourceModel\Category\Collection
  • 're_category_f'=>\Magento\Catalog\Model\ResourceModel\CategoryFactory::class,//fixme This class\Magento\Catalog\Model\ResourceModel\Category
  • 're_category'=>\Magento\Catalog\Model\ResourceModel\Category::class,//fixme This  Catalog category model

  正确的是:\Magento\Catalog\Model\CategoryFactory

  ResourceModel 与 Model 的区别:

 

 

Models : Models are where your main business logic should be handled and is a single instance of an object. The model will use the resource model to talk to the database and get/set data for it on save() and load().

Resource Model : A resource model is where your main C.R.U.D happens (CreateReadUpdateand delete). The resource model shouldn’t contain business logic however it will talk to the adapters and basically talk to the database.

 

2,如果使用不正确的通用SQL : 

  SQL:

select value from catalog_product_entity_varchar left join eav_attribute on
eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id
where
eav_attribute.attribute_code='name' and
catalog_product_entity_varchar.entity_id=2082

  CODE:

$_connection = $this->mc_get_obj(\Magento\Framework\App\ResourceConnection::class);//get class
$db_connection = $_connection->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);//connection
$category = $db_connection->fetchAll('select * from '.'`catalog_product_entity_varchar` '.'left join '.'`eav_attribute` '.'on '.
'`eav_attribute`.'.'`attribute_id`='.'`catalog_product_entity_varchar`.'.'`attribute_id` '.
'where '.'`eav_attribute`.'.'`attribute_code`='.'"name" '.'and '. '`catalog_product_entity_varchar`.'.'`entity_id`='.$vi['product_id']); var_dump($category[0]['value']);die;

  

  正确的SQL: 
  

SELECT
e.entity_id AS product_id
, e.type_id AS product_type
, e.sku,
(
SELECT
GROUP_CONCAT(DISTINCT(cv.value))
FROM
catalog_category_entity_varchar AS cv, catalog_category_product AS at_category_id
WHERE
at_category_id.category_id = cv.entity_id
AND (at_category_id.product_id = e.entity_id)
AND cv.attribute_id = 41 and cv.store_id = 0
) AS category_name
FROM catalog_product_entity AS e where e.entity_id=2082;

最新文章

  1. [转载]敏捷开发之Scrum扫盲篇
  2. 实现Discuz论坛客户端应用源码
  3. js 刷新窗口
  4. 我的Android最佳实践之—— 常用的Intent.Action(转)
  5. 21、javascript 基础
  6. How to configure CDB in Qt Creator(使用VC调试器)
  7. HTML Dom操作数据表
  8. C++ STL pair详解
  9. 新版知乎登录之post请求
  10. Android为TV端助力:自定义view之太阳
  11. 在spring中如何生成一个bean (一个对象,比如jedis的连接池对象)【我】
  12. 解析 .Net Core 注入——注册服务
  13. node.js初识06
  14. HDU 5414 CRB and String (2015年多校比赛第10场)
  15. java.lang.verifyerror:bad type on orerand stack
  16. ZOJ 3981 && 2017CCPC秦皇岛 A:Balloon Robot(思维题)
  17. iOS-数据库sqlite的使用
  18. 统计svn 代码提交情况
  19. Linux_x86_Pwn溢出漏洞
  20. Maple重点知识总结

热门文章

  1. Android整理:SQlite数据库的使用以及通过listView显示数据
  2. SpringMVC 进阶
  3. java设计模式3——建造者模式
  4. Codeforces_842
  5. CSS选择器世界
  6. 20200110--python学习第八天
  7. phpmyadmin配置文件详解
  8. 【阿里云IoT+YF3300】15.阿里云物联网小程序构建
  9. 修饰符 public、 private 和 protected和区别
  10. django 中 cookie与session 相关的知识