0

I created categories using REST api by below code.

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
$rootPath  =  $directory->getRoot();
$filename = $rootPath . '/import.csv';
//$file = fopen('/var/www/html/magento2/correct.csv', 'wb');
$result = array();
$keys = array("id","name","parent_id","is_active","is_anchor","include_in_menu","custom_use_parent_settings","description","meta_title","meta_keywords","meta_description");

   if(file_exists($filename)){
    foreach (file($filename) as $key => $str)
    {    
        if ($key == 0)
        continue;
        $values = str_getcsv($str,",",'"');
        $result[] = array_combine($keys, $values);    
    }

    foreach($result as $res){
        ?>

        <script>
            var data = JSON.stringify({
                "category": {
                    "parent_id": '<?php echo $res['parent_id'] ?>',
                    "name": '<?php echo $res['name'] ?>',
                    "is_active": true,
                    "include_in_menu": true
                }
                });

                var xhr = new XMLHttpRequest();
                xhr.withCredentials = true;

                xhr.addEventListener("readystatechange", function () {
                if (this.readyState === 4) {
                    console.log(this.responseText);
                }
                });

                xhr.open("POST", "http://192.168.0.231/magento2/rest/V1/categories/");
                xhr.setRequestHeader("Content-Type", "application/json");
                xhr.setRequestHeader("Authorization", "Bearer xzplmqaz1elygqov5b5sq7odjygd6m0i");
                xhr.setRequestHeader("Cache-Control", "no-cache");
                xhr.setRequestHeader("cache-control", "no-cache");

                xhr.send(data);
        </script>
        <?php
    }
   }

Categories created successfully but when I opened the category page in frontend I get below SQL issue.

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) 
AND (category_ids_index.category_id = 13)) AS `main_select` GROUP BY `entity_' at line 3, query was: INSERT INTO `search_tmp_5e216438c82bd8_73111656` 
SELECT `main_select`.`entity_id`, SUM(score) AS `relevance` FROM (SELECT DISTINCT `search_index`.`entity_id`, (((0) + (0)) * 1) AS `score`
FROM `catalog_product_index_eav` AS `search_index` INNER JOIN `cataloginventory_stock_status` AS `stock_index` ON stock_index.product_id = search_index.entity_id 
AND `stock_index`.`website_id` = 0 AND `stock_index`.`stock_status` = 1 AND `stock_index`.`stock_id` = 1 INNER JOIN `catalog_category_product_index_store1` 
AS `category_ids_index` ON search_index.entity_id = category_ids_index.product_id AND category_ids_index.store_id = '1' WHERE (search_index.store_id = '1') 
AND (`search_index`.`attribute_id` = 99 AND `search_index`.`value` in ('2', '4') AND `search_index`.`store_id` = '1') AND (search_index.entity_id IN ()) 
AND (category_ids_index.category_id = 13)) AS `main_select` GROUP BY `entity_id` ORDER BY `relevance` DESC, `entity_id` DESC LIMIT 10000

Your Answer

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

Browse other questions tagged or ask your own question.