• 根据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;

magento2.2.3 根据产品ID获取栏目名称的正确调用方式的更多相关文章

  1. 数据表自增Id获取时IDENTITY的正确使用方式

    在SQLServer中很多表主键会设置为自增列,有的业务需求需要知道新插入的自增Id是多少,一般我们会用SELECT @@IDENTITY来获取,可由于@@IDENTITY是个全局变量作用据较大,所以 ...

  2. 简单的 通过ID获取文件名称

    模型中的方法class 模型名{ /** * 通过ID获取文件名称 */ public static function getNameById($id) { $model = self::findOn ...

  3. 帝国cms在任意位置调用指定id的栏目名称和链接

    注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?>   示例:<?=$class_ ...

  4. 齐博x1.3通用栏目名称及参数调用接口

    对于全站的频道可以使用下面的方法取出相应的栏目名称及参数http://qb.net/index.php/cms/wxapp.sorts.html注意,只需要把qb.net换成你的域名,cms 换成其它 ...

  5. dedecms织梦副栏目名称和链接调用

    https://blog.csdn.net/qq_41805834/article/details/79552859

  6. dede织梦栏目页和文章页中获取当前栏目名称方法

    一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...

  7. dedecms提取某栏目及子栏目名称到首页怎么弄

    我们建网站时有不同的需求,例如为页面创建一个栏目导航,用dedecms如何提取某栏目及子栏目名称和链接呢?如下图所示,先列出指定的顶级栏目,在下方再列出此栏目的所有子栏目. 之前ytkah说过dede ...

  8. Kubernetes 教程:根据 PID 获取 Pod 名称

    原文链接:https://fuckcloudnative.io/posts/find-kubernetes-pod-info-from-process-id/ 在管理 Kubernetes 集群的过程 ...

  9. magento获取当前栏目ID号与栏目名称函数

    Magento获取当前栏目ID:$_cat= new Mage_Catalog_Block_Navigation();$curent_cat= $_cat->getCurrentCategory ...

随机推荐

  1. [JavaScript]AO对象

    1, 形式参数 2, 局部变量 3, 函数声明表达式

  2. UVA5913 Dictionary Sizes(字典树)(转载)

    题目大意:给出n个旧单词,要从这n个旧单词中构造新单词.构造条件是 S = Sa + Sb,其中Sa为某个旧单词的非空前缀,Sb为某个单词的非空后缀.求所有的新单词和旧单词中有多少个不同的单词. 思路 ...

  3. 详解c++中对二维数组下标[][]的重载

    首先定义一个矩阵类,我用一个二维数组存储矩阵中的数据,矩阵详细定义如下 class Matrix { public: Matrix(int rows, int cols) { _rows = rows ...

  4. P3387缩点(tarjan+拓扑排序+线性dp)

    题目描述 给定一个 n个点 m 条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 输入 ...

  5. 通过 python 处理 email - Email via Python

    Email via Python 1 MIME - Multipurpose Internet Mail Extensions SMTP - Simple Message Transport Prot ...

  6. 浅谈CC攻击原理与防范

    概念         CC攻击的原理就是攻击者控制某些主机不停地发大量数据包给对方服务器造成服务器资源耗尽,一直到宕机崩溃.CC主要是用来攻击页面的,每个人都有这样的体验:当一个网页访问的人数特别多的 ...

  7. Nginx总结(八)启用Nginx Status及状态参数详解

    前面讲了如何配置Nginx虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天简 ...

  8. 网易MuMu模拟器不显示Menu(菜单)键的解决办法

    解决方法一: 前提:需要一个键盘 步骤: 1.直接按下键盘上的Menu键. 解决方法二: 前提:需要Root之后的文件浏览器 步骤: 1.在文件管理器中打开 /System 文件夹: 2.复制 bui ...

  9. mysql中EXPLAIN 的作用

    (一)id列: (1).id 相同执行顺序由上到下 mysql> explain -> SELECT*FROM tb_order tb1 -> LEFT JOIN tb_produc ...

  10. 你没有见过的【高恪】船新版本(SX3000 NAT1 X86魔改)

    最近魔改了高恪SX3000 X86,做了如下更改: 开启了SSH 集成了插件(酸酸乳.V2RXY.SMB等等) 开启了NAT1 DIY了主题 精简了官方内置的无用应用和模块 截图(建议右击图片,在新标 ...