vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.15k stars 1.38k forks source link

jsTree conflict with postgreSQL #2630

Closed enCoreV1 closed 2 years ago

enCoreV1 commented 2 years ago

Hello, i tried to make tree from database and php scripts. This is my code whitch works well on MySQL database without any errors

case 'create_node':
   $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
   $nodeText = isset($_GET['text']) && $_GET['text'] !== '' ? $_GET['text'] : '';
   $statement = $pdo->prepare("INSERT INTO categories(category, parent_id) VALUES(?,?)");
   $statement->execute(array($nodeText, $node));
   $result = array('id' => $pdo->lastInsertId());
   break;
...
echo json_encode($result);

And it's ok. Add new node and rename it. But if change database to PostgreSQL i'll take error Code for PostgreSQL

case 'create_node':
   $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
   $nodeText = isset($_GET['text']) && $_GET['text'] !== '' ? $_GET['text'] : '';
   $sql = "INSERT INTO categories (id, category, parent_id) VALUES (DEFAULT, :name, :parent_id) RETURNING id";  //returning id <->lastinsertid from MySQL 
   $stmt = $pdo->prepare($sql);
   $stmt->bindParam(':name', $nodeText, PDO::PARAM_STR);
   $stmt->bindParam(':parent_id', $node, PDO::PARAM_INT);
   $stmt->execute();
   $id = $stmt->fetchColumn(); //PostgreSQL serial (auto_increment) fix
   $result = array('id' => $id);
break;
...
echo json_encode($result);

$result will be the same: {id: "51"} (51 for example)

but JS give me error. jsTree last version.

jstree.js:3683 Uncaught TypeError: Cannot read properties of undefined (reading 'toString')
    at $.jstree.plugins.types.set_id (jstree.js:3683:12)
    at Object.<anonymous> (vvst.js:41:31)
    at fire (jquery-3.5.1.min.js:3500:31)
    at Object.fireWith [as resolveWith] (jquery-3.5.1.min.js:3630:7)
    at done (jquery-3.5.1.min.js:9796:14)
    at XMLHttpRequest.<anonymous> (jquery-3.5.1.min.js:10057:9)

So new node added but i need to refresh my page for rename it. Thx for helping

aadrian commented 2 years ago

@enCoreV1 post here the two JSONs (from MySQL and from Postgres) since they're not the same.

enCoreV1 commented 2 years ago

@enCoreV1 post here the two JSONs (from MySQL and from Postgres) since they're not the same. 1

3

Only one idea - MySQL id in JSON as string with quotation marks and PostgreSQL JSON as integer. So, how return right JSON format from PGSQL? Or i make another mistake? Thx in advance

vakata commented 2 years ago

I am not sure there is something I can do - please provide the data in the format that is expected, jstree does not talk to a database directly. From looking at the error it seems like the node is never created - the ID is still the autogenerated j1_1. Usually the biggest difference when migrating to postgresql is types and the way the underlying library interprets them - have a look at that as well. I can assure you I have tens of projects using jstree and postgreSQL.