T
T
Think With Your Head2016-03-14 12:24:45
symfony
Think With Your Head, 2016-03-14 12:24:45

How to make automatic insertion of categories in doctrine tree from xml?

Hello!
There is an xml, a file for unloading goods from 1c, categories of goods are specified in the form level0=name, level1=name and so on.
Structure of a DB
e89906dd27b947528e06ddb592865f81.png
Now I write a parser for entering in a database. Created doctrine entities. With all entities it is clear, except for categories.
Here's how it works with an example in the tree documentation

/** Inserting node in different positions **/

<?php
$food = new Category();
$food->setTitle('Food');

$fruits = new Category();
$fruits->setTitle('Fruits');

$vegetables = new Category();
$vegetables->setTitle('Vegetables');

$carrots = new Category();
$carrots->setTitle('Carrots');

$treeRepository
    ->persistAsFirstChild($food)
    ->persistAsFirstChildOf($fruits, $food)
    ->persistAsLastChildOf($vegetables, $food)
    ->persistAsNextSiblingOf($carrots, $fruits);

$em->flush();

/**
Tree example:

/Food
    /Vegetables
        /Onions
        /Carrots
        /Cabbages
        /Potatoes
    /Fruits
**/

https://github.com/Atlantic18/DoctrineExtensions/b...
But the bottom line is that I do not need to enter each category manually, they are already written in xml with nesting and title.
I do not understand what algorithm should be written

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2016-03-14
@Vyad

Recursively iterate through the category xml, simply specifying the parent for each category.
Here is the section in the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question