Skip to content

New category saved from the repository has an invalid path #39738

Open
@thomas-kl1

Description

@thomas-kl1

Preconditions and environment

  • Magento version : 2.4.7

Steps to reproduce

Simplified version:

<?php

declare(strict_types=1);

namespace Foo;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class Bar
{
    public function __construct(
        private CollectionFactory $collectionFactory,
        private CategoryRepositoryInterface $categoryRepository
    ) {}

    /**
     * @throws LocalizedException
     */
    public function apply(): void
    {
        /** @var Collection $collection */
        $collection = $this->collectionFactory->create();

        /** @var Category $category */
        $category = $collection->getNewEmptyItem();
        $category->setName($name);
        $category->setIsActive(true);
        $category->setParentId(Category::TREE_ROOT_ID);

        // If we use the repository, the path is not correctly setup on 
        $this->categoryRepository->save($category);

        // If we use the save method of the model, the path is correctly set
        //$category->save();
    }
}

Expected result

Path is like : 1/256 (where 256 is the freshly created category ID)

Actual result

Path is: 1

Additional information

It seems that

$object->setPath($object->getPath() . '/');
is never reached.

$object->setPath($object->getPath() . '/');

So the following is called

if (substr((string)$object->getPath(), -1) == '/') {

        /**
         * Add identifier for new category
         */
        if (substr((string)$object->getPath(), -1) == '/') {
            $object->setPath($object->getPath() . $object->getId());
            $this->_savePath($object);
        }

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

Labels

Issue: needs updateAdditional information is require, waiting for responseReported on 2.4.7Indicates original Magento version for the Issue report.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

Status

Needs Update

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions