public function testNestedCategories() { $category = new Category('Root'); $child1 = $category->addChild('Child 1'); $child2 = $child1->addChild('Child 2'); $this->dm->persist($category); $this->dm->flush(); $this->dm->clear(); $category = $this->dm->find(__NAMESPACE__ . '\\Category', $category->getId()); $this->assertNotNull($category); $category->setName('Root Changed'); $children = $category->getChildren(); $children[0]->setName('Child 1 Changed'); $children[0]->getChild('Child 2')->setName('Child 2 Changed'); $category->addChild('Child 2'); $this->dm->flush(); $this->dm->clear(); $category = $this->dm->find(__NAMESPACE__ . '\\Category', $category->getId()); $children = $category->getChildren(); $this->assertEquals('Child 1 Changed', $children[0]->getName()); $this->assertEquals('Child 2 Changed', $children[0]->getChild(0)->getName()); $this->assertEquals('Root Changed', $category->getName()); $this->assertEquals(2, count($category->getChildren())); $test = $this->dm->getDocumentCollection(__NAMESPACE__ . '\\Category')->findOne(); $this->assertFalse(isset($test['children'][0]['children'][0]['children'])); }
/** * @param \Category $parent * @param bool $crossLinking */ public function setParent(Category $parent, $crossLinking = true) { if ($this->parent) { $this->parent->removeChild($this, false); } $this->parent = $parent; if ($crossLinking && $parent) { $parent->addChild($this, false); } }
require 'classes/StockItem.php'; /* * Строим простое дерево категорий: * Collection * | * `-- Pants * `-- Jackets * `-- Blouses * `-- Shirts */ $rootCategory = new Category('Collection'); $pantsCategory = new Category('Pants'); $jacketsCategory = new Category('Jackets'); $blousesCategory = new Category('Blouses'); $shirtsCategory = new Category('Shirts'); $rootCategory->addChild($pantsCategory); $rootCategory->addChild($jacketsCategory); $rootCategory->addChild($blousesCategory); $rootCategory->addChild($shirtsCategory); // Создаем пару брендов $adidas = new Brand('Adidas'); $nike = new Brand('Nike'); // Создаем продукты... $pants = new Product('Jogging Pants', 79.95); $pants->setColor('black'); $pants->setBrand($adidas); $jeans = new Product('Midi - handcrafted - Jeans', 129.95); $jeans->setColor('blue'); $jeans->setImage('http://example.com/some-iamge.jpg'); $jeans->setBrand($nike); $leggings = new Product('Leggings', 99.95);