本文整理汇总了PHP中Magento\Catalog\Model\Category::getResource方法的典型用法代码示例。如果您正苦于以下问题:PHP Category::getResource方法的具体用法?PHP Category::getResource怎么用?PHP Category::getResource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Model\Category
的用法示例。
在下文中一共展示了Category::getResource方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getChildrenIds
/**
* @param \Magento\Catalog\Model\Category $category
* @param boolean $recursive
* @return int[]
*/
public function getChildrenIds(Category $category, $recursive = false)
{
$cacheKey = $category->getId() . '_' . (int) $recursive;
if (!isset($this->childrenIds[$cacheKey])) {
$connection = $category->getResource()->getReadConnection();
$select = $connection->select()->from($category->getResource()->getEntityTable(), 'entity_id')->where($connection->quoteIdentifier('path') . ' LIKE :c_path');
$bind = ['c_path' => $category->getPath() . '/%'];
if (!$recursive) {
$select->where($connection->quoteIdentifier('level') . ' <= :c_level');
$bind['c_level'] = $category->getLevel() + 1;
}
$this->childrenIds[$cacheKey] = $connection->fetchCol($select, $bind);
}
return $this->childrenIds[$cacheKey];
}
示例2: processDelete
/**
* @param Category $category
* @return void
*/
public function processDelete(Category $category)
{
/** @var \Magento\Catalog\Model\ResourceModel\Category $resourceModel */
$resourceModel = $category->getResource();
/**
* Update children count for all parent categories
*/
$parentIds = $category->getParentIds();
if ($parentIds) {
$childDecrease = $category->getChildrenCount() + 1;
// +1 is itself
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$where = ['entity_id IN(?)' => $parentIds];
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
}
}
示例3: _getCategoryAttribute
/**
* Retrieve category attributes
*
* @param string $attributeCode
* @param int|array $categoryIds
* @param int $storeId
* @return array
*/
protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
{
$linkField = $this->metadataPool->getMetadata(CategoryInterface::class)->getLinkField();
$identifierFiled = $this->metadataPool->getMetadata(CategoryInterface::class)->getIdentifierField();
$connection = $this->getConnection();
if (!isset($this->_categoryAttributes[$attributeCode])) {
$attribute = $this->_catalogCategory->getResource()->getAttribute($attributeCode);
$this->_categoryAttributes[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'is_static' => $attribute->isStatic()];
unset($attribute);
}
if (!is_array($categoryIds)) {
$categoryIds = [$categoryIds];
}
$attributeTable = $this->_categoryAttributes[$attributeCode]['table'];
$select = $connection->select();
$bind = [];
if ($this->_categoryAttributes[$attributeCode]['is_static']) {
$select->from($this->getTable('catalog_category_entity'), ['value' => $attributeCode, 'entity_id' => 'entity_id'])->where('entity_id IN(?)', $categoryIds);
} elseif ($this->_categoryAttributes[$attributeCode]['is_global'] || $storeId == 0) {
$select->from(['t1' => $this->getTable('catalog_category_entity')], [$identifierFiled])->joinLeft(['e' => $attributeTable], "t1.{$linkField} = e.{$linkField}", ['value'])->where("t1.{$identifierFiled} IN(?)", $categoryIds)->where('e.attribute_id = :attribute_id')->where('e.store_id = ?', 0);
$bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
} else {
$valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
$select->from(['t1' => $attributeTable], [$identifierFiled => 'e.' . $identifierFiled, 'value' => $valueExpr])->joinLeft(['t2' => $attributeTable], "t1.{$linkField} = t2.{$linkField} AND t1.attribute_id = t2.attribute_id AND t2.store_id = :store_id", [])->joinLeft(['e' => $this->getTable('catalog_category_entity')], "e.{$linkField} = t1.{$linkField}", [])->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where("e.entity_id IN(?)", $categoryIds)->group('e.entity_id');
$bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
$bind['store_id'] = $storeId;
}
$rowSet = $connection->fetchAll($select, $bind);
$attributes = [];
foreach ($rowSet as $row) {
$attributes[$row[$identifierFiled]] = $row['value'];
}
unset($rowSet);
foreach ($categoryIds as $categoryId) {
if (!isset($attributes[$categoryId])) {
$attributes[$categoryId] = null;
}
}
return $attributes;
}
示例4: _getCategoryAttribute
/**
* Retrieve category attributes
*
* @param string $attributeCode
* @param int|array $categoryIds
* @param int $storeId
* @return array
*/
protected function _getCategoryAttribute($attributeCode, $categoryIds, $storeId)
{
$adapter = $this->_getWriteAdapter();
if (!isset($this->_categoryAttributes[$attributeCode])) {
$attribute = $this->_catalogCategory->getResource()->getAttribute($attributeCode);
$this->_categoryAttributes[$attributeCode] = ['entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getId(), 'table' => $attribute->getBackend()->getTable(), 'is_global' => $attribute->getIsGlobal(), 'is_static' => $attribute->isStatic()];
unset($attribute);
}
if (!is_array($categoryIds)) {
$categoryIds = [$categoryIds];
}
$attributeTable = $this->_categoryAttributes[$attributeCode]['table'];
$select = $adapter->select();
$bind = [];
if ($this->_categoryAttributes[$attributeCode]['is_static']) {
$select->from($this->getTable('catalog_category_entity'), ['value' => $attributeCode, 'entity_id' => 'entity_id'])->where('entity_id IN(?)', $categoryIds);
} elseif ($this->_categoryAttributes[$attributeCode]['is_global'] || $storeId == 0) {
$select->from($attributeTable, ['entity_id', 'value'])->where('attribute_id = :attribute_id')->where('store_id = ?', 0)->where('entity_id IN(?)', $categoryIds);
$bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
} else {
$valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
$select->from(['t1' => $attributeTable], ['entity_id', 'value' => $valueExpr])->joinLeft(['t2' => $attributeTable], 't1.entity_id = t2.entity_id AND t1.attribute_id = t2.attribute_id AND t2.store_id = :store_id', [])->where('t1.store_id = ?', 0)->where('t1.attribute_id = :attribute_id')->where('t1.entity_id IN(?)', $categoryIds);
$bind['attribute_id'] = $this->_categoryAttributes[$attributeCode]['attribute_id'];
$bind['store_id'] = $storeId;
}
$rowSet = $adapter->fetchAll($select, $bind);
$attributes = [];
foreach ($rowSet as $row) {
$attributes[$row['entity_id']] = $row['value'];
}
unset($rowSet);
foreach ($categoryIds as $categoryId) {
if (!isset($attributes[$categoryId])) {
$attributes[$categoryId] = null;
}
}
return $attributes;
}
示例5: getObject
public function getObject(Category $category)
{
/** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
$productCollection = $category->getProductCollection();
$productCollection = $productCollection->addMinimalPrice();
$category->setProductCount($productCollection->getSize());
$transport = new DataObject();
$this->eventManager->dispatch('algolia_category_index_before', ['category' => $category, 'custom_data' => $transport]);
$customData = $transport->getData();
$storeId = $category->getStoreId();
$category->getUrlInstance()->setStore($storeId);
$path = '';
foreach ($category->getPathIds() as $categoryId) {
if ($path != '') {
$path .= ' / ';
}
$path .= $this->getCategoryName($categoryId, $storeId);
}
$image_url = null;
try {
$image_url = $category->getImageUrl();
} catch (\Exception $e) {
/* no image, no default: not fatal */
}
$data = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['category'], 'popularity' => 1, 'product_count' => $category->getProductCount()];
if (!empty($image_url)) {
$data['image_url'] = $image_url;
}
foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) {
$value = $category->getData($attribute['attribute']);
$attribute_resource = $category->getResource()->getAttribute($attribute['attribute']);
if ($attribute_resource) {
$value = $attribute_resource->getFrontend()->getValue($category);
}
if (isset($data[$attribute['attribute']])) {
$value = $data[$attribute['attribute']];
}
if ($value) {
$data[$attribute['attribute']] = $value;
}
}
$data = array_merge($data, $customData);
foreach ($data as &$data0) {
$data0 = $this->try_cast($data0);
}
return $data;
}
示例6: validateCategory
/**
* Validate category process
*
* @param Category $category
* @return void
* @throws \Magento\Framework\Model\Exception
*/
protected function validateCategory(Category $category)
{
$useConfigFields = [];
foreach ($this->useConfigFields as $field) {
if (!$category->getData($field)) {
$useConfigFields[] = $field;
}
}
$category->setData('use_post_data_config', $useConfigFields);
$validate = $category->validate();
if ($validate !== true) {
foreach ($validate as $code => $error) {
if ($error === true) {
$attribute = $category->getResource()->getAttribute($code)->getFrontend()->getLabel();
throw new \Magento\Framework\Model\Exception(__('Attribute "%1" is required.', $attribute));
} else {
throw new \Magento\Framework\Model\Exception($error);
}
}
}
$category->unsetData('use_post_data_config');
}
示例7: getResource
/**
* {@inheritdoc}
*/
public function getResource()
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'getResource');
if (!$pluginInfo) {
return parent::getResource();
} else {
return $this->___callPlugins('getResource', func_get_args(), $pluginInfo);
}
}