1

I make and installData to define new categories (I have a lot of categories and sub-categories and I don't know why but they are nor added, in the database I see they are added but it's strange see the picture).

In my installData I create a new Category object from Magento\Catalog\Model\Category

In my install function I put this :

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  {
    $setup->startSetup();

    // get datas to insert the categories
    $categories_data = $this->getCategoriesData();

    // Insert all main categories
    foreach ($categories_data as $category_data)
    {
      $this->insertCategory($category_data);
    }


    $setup->endSetup();
  }

So I get my datas by the getCategoriesData function which return me an array like :

[
  'data' => ['name' => 'A category', 'parent_id' => 3],
  'childs' => [
    [
      'data' => ['name' => 'A sub category 1'],
      'childs' => [],
    ],
    [
      'data' => ['name' => 'A sub category 2'],
      'childs' => [
        [
          'data' => ['name' => 'A sub category of sub category 2'],
          'childs' => [],
        ],
      ],
    ],
  ]
],
[
  'data' => ['name' => 'A category not in menu', 'parent_id' => 3, 'include_in_menu' => false],
  'childs' => [],
]

And in my insertCategory function I put :

private function insertCategory($category_data)
  {
    $category = $this->_category->setData($category_data['data'])->save();
    if (!empty($category_data['childs']))
    {
      foreach ($category_data['childs'] as $category_child)
      {
        $category_child['data']['parent_id'] = $category->getId();
        $this->insertCategory($category_child);
      }
    }
  }

So if the category have an array child which is not empty, I add a new data 'parent_id' to the child data key. But I don't know why, this not work. Look at the screen of my database, the parent_id of the Childs is the id of the child, it's not possible, I dont understand why... I define a parent_id = 3 to put theses in a root category test I created but they are not showed in the back office. (I have run bin/magento setup:upgrade and bin/magento cache:flush).

enter image description here

0

Be aware of addData and setData methods.

The magento\Framework\DataObject said:

/**
 * Add data to the object.
 *
 * Retains previous data in the object.
 *
 * @param array $arr
 * @return $this
 */
public function addData(array $arr)
{
    foreach ($arr as $index => $value) {
        $this->setData($index, $value);
    }
    return $this;
}

/**
 * Overwrite data in the object.
 *
 * The $key parameter can be string or array.
 * If $key is string, the attribute value will be overwritten by $value
 *
 * If $key is an array, it will overwrite all the data in the object.
 *
 * @param string|array  $key
 * @param mixed         $value
 * @return $this
 */
public function setData($key, $value = null)
{
    if ($key === (array)$key) {
        $this->_data = $key;
    } else {
        $this->_data[$key] = $value;
    }
    return $this;
}

Illustration:

$data = [
    'a' => 'b',
    'c' => 'd',
];

addData(['a' => 'z']);
// data['a'] replaced by 'z', result: ['a' => 'z', 'b' => 'd']

setData(['a' => 'z']);
// data is cleared and filled by given parameter, result: ['a' => 'z']

Try to replace your setData with addData:

private function insertCategory($category_data)
{
    // $category = $this->_category->setData($category_data['data'])->save();
    // change above code with this one, and give a try
    $category = $this->_category->addData($category_data['data'])->save();
    if (!empty($category_data['childs'])) {
        foreach ($category_data['childs'] as $category_child) {
            $category_child['data']['parent_id'] = $category->getId();
            $this->insertCategory($category_child);
        }
    }
}

And see if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.