Answer the question
In order to leave comments, you need to log in
Doctrine2-nestedset best practice?
Model:
<?php namespace Api\Model; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\GeneratedValue; use Doctrine\ORM\Mapping\Index; use DoctrineExtensions\NestedSet\MultipleRootNode; /** * Class User * @ORM\Entity * @ORM\Table(name="category", * indexes={@Index(name="idx", columns={"root", "lft", "rgt"})}) * * @see http://www.getinfo.ru/article610.html */ class Category implements MultipleRootNode { /** * Category constructor. * @param string $name */ public function __construct(string $name = '') { $this->setName($name); } /** * @ORM\Id * @GeneratedValue * * @ORM\Column(type="integer") * @var integer */ private $id; /** * @var string * @ORM\Column(type="string", length=255) */ private $name; /** * @var int * @ORM\Column(type="integer", options={"default" : 0}) */ private $root; /** * @var int * @ORM\Column(type="integer", options={"default" : 0}) */ private $lft; /** * @var int * @ORM\Column(type="integer", options={"default" : 0}) */ private $rgt; /** * gets a unique identifier for this node * * @return mixed */ public function getId() { return $this->id; } /** * gets a string representation of the Node * * @return string */ public function __toString() { return $this->name; } /** * gets Node's left value * * @return int */ public function getLeftValue() { return $this->lft; } /** * sets Node's left value * * @param int $lft */ public function setLeftValue($lft) { $this->lft = $lft; } /** * gets Node's right value * * @return int */ public function getRightValue() { return $this->lft; } /** * sets Node's right value * * @param int $rgt */ public function setRightValue($rgt) { $this->rgt = $rgt; } /** * @return string */ public function getName(): string { return $this->name; } /** * @param string $name */ public function setName(string $name): void { $this->name = $name; } /** * gets Node's root value * * @return mixed */ public function getRootValue() { return $this->root; } /** * sets Node's root value * * @param mixed $root */ public function setRootValue($root) { $this->root = $root; } }
// @see https://github.com/blt04/doctrine2-nestedset
$config = new Config($this->em, Category::class);
$nsm = new Manager($config);
if ($this->request->has('root')){
$root = $nsm->createRoot(new Category($this->request->mixed('name')));
} else {
$rootNode = $nsm->fetchTree((int)$this->request->mixed('parent'), 1);
$rootNode->addChild(new Category($this->request->mixed('name')));
}
// @see https://github.com/blt04/doctrine2-nestedset
$config = new Config($this->em, Category::class);
$nsm = new Manager($config);
$data = new ResponseSchema();
$data->setCount(1000);
$data->setItems($nsm->fetchTreeAsArray((int)$this->request->mixed('root')));
$json = $this->serializer->serialize($data, 'json', ['attributes' => [
'count',
'items' => [
'id',
'node',
]
]]);
$this->response->jsonSend($json);
{
"count": 1000,
"items": [
{
"node": {
"id": 3958,
"leftValue": 1,
"rightValue": 1,
"name": "Child 1",
"rootValue": 3955
},
"id": 3958
},
{
"node": {
"id": 3955,
"leftValue": 3,
"rightValue": 3,
"name": "Root 1",
"rootValue": 3955
},
"id": 3955
}
]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question