Symfony2 - How to implement nested records and recursive functions in Entity Field or Twig Layout?

I have a serious doubt that you are making a combo box with nested entries from an object in Symfony2. I read about the nested tree extension for Doctrine 2 at http://gediminasm.org/article/tree-nestedset-behavior-extension-for-doctrine-2 , it seems interesting, but it does not specify how to implement this nested tree in the field object in shape.

In addition, I read more about recursive functions in PHP, and I found an interesting blog where it is analyzed, here is the link http://www.sitepoint.com/hierarchical-data-database/ , it specifically explains this recursive function:

function display_children($parent, $level) { // Retrieve all children of $parent $result = mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'"'); // Display each child while ($row = mysql_fetch_array($result)) { // Indent and display the title of this child echo str_repeat(' ',$level).$row['title']."\n"; // Call this function again to display this child children display_children($row['title'], $level+1); } } 

Someone knows how to translate this code into Symfony2 and where it will be stored (Controller, Entity, etc.). If anyone has any other ideas on working with nested posts with Twig Extensions, it would also be helpful.

Many thanks for your help.

+4
source share
2 answers

So we implemented a nested tree for categories (indented) for use in the product editing form:

  • Define the entity class of your category as shown in the documentation

  • Add a method to the category entity class that shows the indented name using the nesting level

     /** * @ORM\Table() * @ORM\Entity(repositoryClass="CP\YourBundle\Entity\CategoryRepository") * @Gedmo\Tree(type="nested") */ class Category { public function getOptionLabel() { return str_repeat( html_entity_decode(' ', ENT_QUOTES, 'UTF-8'), ($this->getLevel() + 1) * 3 ) . $this->getName(); } 
  • Define the relationship between a product object and category objects using Doctrine2 annotations (in our case, we support several categories for one product)

     class Product { /** * @var ArrayCollection * @ORM\ManyToMany(targetEntity="Category", cascade={"persist", "remove"}) */ private $categories; ... 
  • Now you need to add the following to the ProductType form class

     class ProductType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('categories', null, array('property' => 'optionLabel')); } 

The form should now display a drop-down list with the right indentation. List of categories

+7
source

You can take a look at this tree implementation, which is not based on nested sets, but on materialized paths: https://github.com/KnpLabs/materialized-path .

You can imagine your API to get a flat set of tree results, for example, in a code snippet:

 $root = $repo->find($id); $repo->buildTree($root); $flatArray = $root->toFlatArray(function(NodeInterface $node) { $pre = $node->getLevel() > 1 ? implode('', array_fill(0, $node->getLevel(), '--')) : ''; return $pre.(string)$node; }); return $this->get('templating')->render('::tree.html.twig', $flatArray); 
+2
source

Source: https://habr.com/ru/post/1394404/


All Articles