magento2.2.3 根据产品ID获取栏目名称的正确调用方式
- 根据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()andload().Resource Model : A resource model is where your main C.R.U.D happens (Create, Read, Updateand 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获取栏目名称的正确调用方式的更多相关文章
- 数据表自增Id获取时IDENTITY的正确使用方式
在SQLServer中很多表主键会设置为自增列,有的业务需求需要知道新插入的自增Id是多少,一般我们会用SELECT @@IDENTITY来获取,可由于@@IDENTITY是个全局变量作用据较大,所以 ...
- 简单的 通过ID获取文件名称
模型中的方法class 模型名{ /** * 通过ID获取文件名称 */ public static function getNameById($id) { $model = self::findOn ...
- 帝国cms在任意位置调用指定id的栏目名称和链接
注意,这个代码无须放在灵动标签中,直接写入模板相应的位置就行了.[1]调用栏目名称: <?=$class_r[栏目ID]['classname']?> 示例:<?=$class_ ...
- 齐博x1.3通用栏目名称及参数调用接口
对于全站的频道可以使用下面的方法取出相应的栏目名称及参数http://qb.net/index.php/cms/wxapp.sorts.html注意,只需要把qb.net换成你的域名,cms 换成其它 ...
- dedecms织梦副栏目名称和链接调用
https://blog.csdn.net/qq_41805834/article/details/79552859
- dede织梦栏目页和文章页中获取当前栏目名称方法
一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...
- dedecms提取某栏目及子栏目名称到首页怎么弄
我们建网站时有不同的需求,例如为页面创建一个栏目导航,用dedecms如何提取某栏目及子栏目名称和链接呢?如下图所示,先列出指定的顶级栏目,在下方再列出此栏目的所有子栏目. 之前ytkah说过dede ...
- Kubernetes 教程:根据 PID 获取 Pod 名称
原文链接:https://fuckcloudnative.io/posts/find-kubernetes-pod-info-from-process-id/ 在管理 Kubernetes 集群的过程 ...
- magento获取当前栏目ID号与栏目名称函数
Magento获取当前栏目ID:$_cat= new Mage_Catalog_Block_Navigation();$curent_cat= $_cat->getCurrentCategory ...
随机推荐
- Linux内核单链表
主要说明Linux内核中单链表操作的关键思想,需要注意的地方 1. 假设 为了说明关键思想,对数据结构进行了精简 2. 数据结构定义 struct ListNode { int val; ListNo ...
- Codeforces_844
A.统计字母个数. #include<bits/stdc++.h> using namespace std; string s; int n; map<char,int> mp ...
- CCF_201403-1_相反数
按绝对值排序,因为没相同的数,直接遍历比较一遍即可. #include<iostream> #include<cstdio> #include<algorithm> ...
- LeetCode 218. The Skyline Problem 天际线问题(C++/Java)
题目: A city's skyline is the outer contour of the silhouette formed by all the buildings in that city ...
- android项目上传github
很简单
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- 版本控制工具-svn
两个疑问: 1.什么是版本控制? 2.为什么要用版本控制工具? 银联卡的特征: 1.受保护的 2.受约束的 如何与银联卡对应? 1.个人的代码--口袋里的钱 2.版本控制工具中的代码--银联卡里的钱 ...
- My introduction
Vistors 访客统计
- 分组密码CBC加密缺陷
title: 分组密码CBC加密缺陷 date: 2017-05-15 10:04:47 tags: ["密码学"] --- 关于密码学的种种漏洞以及利用网上也有不少,但是比较零散 ...
- Solr搜索解析及查询解析器用法概述
一.简介 大多数查询都使用 了标准的Solr语法.这种语法是Solr最常见的,由默认查询解析器负责处理.Solr的默认查询解析器是Lucene查询解析器[LuceneQParserPlugin类实现] ...