{
"require": {
"silex/silex": "~1.2",
"smarty/smarty": "dev-trunk",
"cboden/Ratchet": "0.3.*",
"react/zmq": "0.2.*",
"doctrine/mongodb-odm": "1.0.0-BETA10",
"neutron/mongo-odm-silex-provider": "*",
"symfony/console": "2.6.*@dev"
},
"autoload": {
"psr-0": {
"Acme": "src/"
}
}
}
svscorp@ubu14:/var/www/silex$ ls vendor/
autoload.php cboden composer doctrine evenement guzzle neutron pimple psr react silex smarty symfony
console debug event-dispatcher http-foundation http-kernel routing
Т.е.:
* HTTPFoundation - это компонент пакета symfony vendor'a symfony (symfony/symfony)
* config или console - это пакет vendor'a symfony
Т.е. config мог бы быть в vendor/symfony/symfony/src/Symfony/Component/, если бы являлся компонентом пакета symfony/symfony. Но он является отдельным пакетом symfony.
class CategoryForm extends AbstractType
{
private $em;
public function __construct($em) {
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$transformer = new CategoryToChoiceTransformer($this->em);
$builder->add(
$builder->create('categories', 'collection',
array(
'type' => new CategoryCollectionType($this->em)
)
)->addModelTransformer($transformer));
$builder->add('save', 'submit');
}
public function getDefaultOptions(array $options)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Item',
));
}
class CategoryCollectionType extends AbstractType
{
private $em;
public function __construct($em) {
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('categories', 'collection', array(
'type' => 'entity',
'allow_add' => true,
'allow_delete' => true,
'prototype' => false,
'show_legend' => true,
'widget_add_btn' => array('label' => _('Добавить категорию')),
'options' => array(
'widget_control_group' => false,
'label_render' => false,
'show_legend' => false,
'class' => 'AcmeDemoBundle:Category',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('c.id', 'ASC');
},
'property' => 'name',
'empty_value' => _('Выберите категорию'),
),
)
);
}
public function getName()
{
return 'category_collection_type';
}
}
class CategoryToChoiceTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
/**
*
* @param Issue|null $issue
* @return string
*/
public function transform($categories)
{
$categoriesExpanded = new ArrayCollection();
if (!empty($categories)) {
$categoryRepository = $this->om->getRepository('AcmeDemoBundle:Category');
foreach ($categories as $category) {
$path = new ArrayCollection($categoryRepository->getPath($category));
$categoriesExpanded->add($path);
}
}
return $categoriesExpanded;
}
public function reverseTransform($f) {
...
}
}
array(
[0] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., ),
[1] => array( ObjectCategory#..., ObjectCategory#..., ObjectCategory#..., )
<?php
// src/Acme/DemoBundle/Entity/Category.php
namespace Acme\DemoBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="categories")
* use repository for handy tree functions
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class Category
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @Gedmo\Translatable
* @ORM\Column(name="title", type="string", length=64)
*/
private $title;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
*
* @Gedmo\Translatable
* @Gedmo\Slug(fields={"title"})
* @ORM\Column(name="slug", type="string", length=128)
*/
private $slug;
public function getId()
{
return $this->id;
}
public function getSlug()
{
return $this->slug;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function setParent(Category $parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
}
use Acme\DemoBundle\Entity\Category as CategoryEntity;
// ... your code before
$em = $this->getDoctrine()->getManager();
$cat1= new CategoryEntity();
$cat1->setTitle('Фрукты');
$subcat = new CategoryEntity();
$subcat->setTitle('Экзотические');
$subcat->setParent($cat1);
$cat2 = new CategoryEntity();
$cat2->setTitle('Овощи');
$em->persist($cat1);
$em->persist($cat2);
$em->persist($subcat);
$em->flush();
// ...
$categoryEntity = $this->em->getRepository('Acme\DemoBundle\Entity\Category');
$categories = $categoryEntity->childrenHierarchy();
// ...