This is my code saving category:
$category = $this->_categoryFactory->create();
$category->setData($this->getCategoryData("Test Test"));
$category->save();
protected function getCategoryData($name)
{
$data = [
'store_id' => "0",
'path_ids' => [
'0' => ""
],
'use_default' => [
'url_key' => false
],
'use_config' => [
'available_sort_by' => true,
'default_sort_by' => true,
'filter_price_range' => true
],
'id' => "",
'parent' => "2",
'path' => "1/2",
'name' => $name,
'filter_price_range' => null,
'url_key' => "",
'meta_title' => "",
'is_active' => "1",
'include_in_menu' => "1",
'is_anchor' => "1",
'custom_use_parent_settings' => "0",
'custom_apply_to_products' => "0",
'url_key_create_redirect' => "0",
'description' => "",
'display_mode' => "PRODUCTS",
'default_sort_by' => null,
'meta_keywords' => "",
'meta_description' => "",
'custom_layout_update' => "",
'custom_design_from' => "",
'custom_design_to' => "",
'image' => null,
'parent_id' => "2",
'available_sort_by' => null,
'attribute_set_id' => "3",
'form_key' => 'AUact7MhifCvdSAD'
];
return $data;
}
I get the exac $data from category insert form in admin dashboard, But it still throws the exception:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
catalog_category_flat
doesn't exist, query was: SELECTcatalog_category_flat
.* FROMcatalog_category_flat
WHERE (catalog_category_flat
.entity_id
='2')
Is anyone have the solution for fixing this bug ?
Solution: I have found the reason why it thrown error. It is because i turn the Use Flat Catalog Category and Use Flat Catalog Product to yes.
If you're in the situation like me, turn two configuraions above off, you will be good to go.