У меня возникают проблемы после этого урока: https://wildlyinaccurate.com/simple-nested-sets-in-doctrine-2
Это мой код. Как вы можете видеть в комментариях, я не могу повторять.
<?php
class Item
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="Item", mappedBy="parent", cascade={"remove"})
* @ORM\OrderBy({"position" = "ASC"})
*/
private $children;
/**
* @Gedmo\SortableGroup
* @ORM\ManyToOne(targetEntity="Item", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*/
private $parent;
...
///////////////////////////////////////
namespace Project\BackendBundle\Entity;
use Doctrine\Common\Collections\Collection;
class RecursiveCategoryIterator implements \RecursiveIterator
{
private $_data;
public function __construct(Collection $data)
{
$this->_data = $data;
}
public function hasChildren()
{
return ( ! $this->_data->current()->getChildren()->isEmpty());
}
public function getChildren()
{
return new RecursiveCategoryIterator($this->_data->current()->getChildren());
}
public function current()
{
return $this->_data->current();
}
public function next()
{
$this->_data->next();
}
public function key()
{
return $this->_data->key();
}
public function valid()
{
return $this->_data->current() instanceof Project\BackendBundle\Entity\Item;
}
public function rewind()
{
$this->_data->first();
}
}
/////////////////////////////////
$repository = $this->getDoctrine()->getRepository('ProjectBackendBundle:Item');
$root_categories = $repository->findBy(array('parent' => null));
var_dump(count($root_categories));// this returns 2
$collection = new \Doctrine\Common\Collections\ArrayCollection($root_categories);
$category_iterator = new RecursiveCategoryIterator($collection);
$recursive_iterator = new \RecursiveIteratorIterator($category_iterator);
var_dump($recursive_iterator->hasChildren()); //returns true
foreach ($recursive_iterator as $index => $child_category)
{
die("jfks"); //this is not shown
}
То, что показано в этой статье, не является вложенным. Лучший способ обработать любые деревья в symfony2 - это реализовать дерево доктрины-расширений.
Запустите один из следующих примеров: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md
В статье, которую вы связали, показан простой смежный набор и применим к любому типу приложений, а не только к Symfony. Но в Symfony2 с Doctrine2 вы можете зависеть от doctrineExtensionBundle.