sql >> Base de Datos >  >> NoSQL >> MongoDB

Php ingresando categorías recursivamente a MongoDB

EDITAR:Con todos los datos nuevos, entiendo claramente lo que está tratando de lograr.

Así que edité mi función anterior, esta es una nueva que debería funcionar directamente en su código. Hágamelo saber para que pueda ajustarla si es necesario.

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;
    }

}