0

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: SELECT catalog_category_flat.* FROM catalog_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.

2 Answers 2

1

You can try this code to add category programmatically :
http://www.zexperto.com/magento2x/create-category-magento2-programmatically

1
  • I have tried the same solution like that. Please, read carefully my question.
    – Toan Tam
    Jul 26, 2017 at 13:53
0

Here is the working code to add product

http://www.octonism.com/blog/cat/magento2/post/magento2-programmatically-create-product/

1
  • I have tried the same solution like that. Please, read carefully my question.
    – Toan Tam
    Jul 26, 2017 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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