- According to acquire category_ids product_id:
/** * @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(); }
- Get 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; }
More than simply call the name of the column.
FAQ:
1, if you use an incorrect CategoryFactory categories :
-
'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
Is correct: \ Magento \ Cataog \ Model \ CategoryFactory
ResourceModel and Model difference:
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, if you use incorrect generic 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;
Correct 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;