MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Php Secara rekursif memasukkan kategori ke MongoDB

EDIT:Dengan semua data baru, saya sangat memahami apa yang ingin Anda capai.

Jadi saya mengedit fungsi lama saya, ini adalah fungsi baru yang seharusnya berfungsi dengan baik dalam kode Anda. Beri tahu saya agar saya dapat menyesuaikan jika diperlukan.

public function setCategories($user_id)
{
    $api = new ApiCategory($user_id);
    $cats = $api->getCategories(); // retrieves structure above

    $newCats = null;
    self::recursiveCatTree($newCats, $cats);
    $this->categories = $newCats;

    $this->save(); // save the entire array of embedded documents

    if($this->getErrors())
        var_dump($this->getErrors);

}

public static function recursiveCatTree(&$result, $parent)
{
    $children = $parent['children'];

    //we unset the children so we dont have manually set every other variable
    unset( $parent['children']);
    $result = new Category();
    $result->attributes = $parent;

    //then loop the children, if no children it wont loop
    // so it will just be an empty    array
    foreach($children as $child)
    {
       $baby = null;
       self::recursiveCatTree($baby, $child);
       $result->children[] = $baby;
    }

}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Masukkan elemen ke dalam array bersarang di Mongodb

  2. meteor $tarik hapus dari array

  3. MongoDB:Masalah saat menggunakan mongoexport dengan opsi --query

  4. otentikasi rmongodb selalu gagal

  5. Bagaimana cara menemukan mongodb ke item terakhir dari sebuah array?