How to build a tree?
We take mongodb and a document with a tree from it
//     mongodb     //       /** * @return \MongoDB\Driver\Manager * */ static function getConnect() { if(!is_null(self::$_connect)) { return self::$_connect; } self::$_connect = new \MongoDB\Driver\Manager(Config_Db::getConf()['mongodb']['connect']); return self::$_connect; } //   /** * add comment action */ public function addCommentAction() { $time = time(); //     //insert new comment to the page $arrData = array( 'page' => $_POST['page_id'], // id   mongo 'time' => $time, //    'name' => $_POST['name'], //   'comment' => $_POST['comment'] //   ); $connect = Core_Model_Mongo::getConnect(); //    //              //           $write = new MongoDB\Driver\BulkWrite(); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY); //  ,     - ,     reply  id  if(isset($_POST['reply']) && !empty($_POST['reply'])) { // id  $reply = $_POST['reply']; //     //  ,        $path = ''; if(isset($_POST['path']) && !empty($_POST['path'])) { $path = $_POST['path']; } else { $path = 'replies'; } // ,     $write->update( array('_id' => new MongoDB\BSON\ObjectID($reply)), //   array('$push' => array($path => $arrData) ) //  push,   ,  path ); } else { $write->insert($arrData); //       } //   $connect->executeBulkWrite(Config_Db::getConf()['mongodb']['db'] . '.comments', $write, $writeConcern); //    header('Location:' . $_POST['back_url']); }   array ( 'name' => 'test', 'comment' => 'test comment', );   array ( 'name' => 'test', 'comment' => 'test comment', 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' ) ) );   array ( 'name' => 'test', 'comment' => 'test comment', 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' 'reply' => array ( [0] => array( 'name' => 'test reply test' 'comment' => 'test comment reply' ) ) ) ) ); Source: https://habr.com/ru/post/279915/
All Articles