更新后使用嵌套树扩展的选项列表的错误排列 [英] Error arrangement of option-list using nested Tree extension after update
问题描述
我有一个实体类别,我使用Tree Gedmo扩展名对其进行管理。
I have an entity Category and I use Tree Gedmo extension to manage it.
我添加了一些类别的父母,子女和这样可以很好地显示:
I have added some categories parents and their children and it is displayed fine like this :
如您所见,类别 Man 没有孩子。为了测试更新是否正常,我将类别衣服设置为类别 Man 的子项,但选项列表的排列变为false:
As you see , Category Man has no chlidren. To test if update work fine or no I Have set Category dresses as a child of Category Man but the arrangment of option-list becomes false :
简单礼服是连衣裙的子级,但显示在 Hauts 下,而 Hauts 是的子级>妇女,但您看到它显示在类别男人中。
Simple dresses is a child of dresses but it is displayed under Hauts, and Hauts is a child of women but as you see it is displayed in Category Man .
我该如何解决?
这是代码:
CategoryRepository
<?php
namespace Project\StoreBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
/**
* CategoryRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CategoryRepository extends NestedTreeRepository
{
public function getCategories($store)
{
$qb = $this->createQueryBuilder('c');
$qb = $this->whereCurrentStore($qb, $store)
->leftJoin('c.children', 'child', 'WITH', 'child.parent = c');
$qb-> orderBy('c.rgt', 'DESC');
return $qb ;
}
public function whereCurrentStore (\Doctrine\ORM\QueryBuilder $qb, $store)
{
$qb->where('c.store = :store')
->setParameter('store', $store);
return $qb;
}
public function getIndentedTitle()
{
return str_repeat("--", $this->lvl).$this->name;
}
}
CategoryType
<?php
namespace Project\StoreBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
class CategoryType extends AbstractType
{
private $store;
public function __construct($store)
{
$this->store = $store;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$store = $this->store;
$builder
//.........
->add('parent', 'entity', array(
'required' => false,
'label' => 'Category parent',
'class' => 'ProjectStoreBundle:Category',
'attr' => array('class' => 'col-sm-8'),
'empty_value' => 'Select one category',
'property' => 'indentedName',
'multiple' => false,
'expanded' => false ,
'query_builder' => function (\Project\StoreBundle\Entity\CategoryRepository $r) use ($store)
{
return $r->getCategories($store);
}
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Project\StoreBundle\Entity\Category'
));
}
/**
* @return string
*/
public function getName()
{
return 'project_storebundle_category';
}
}
实体类别
<?php
namespace Project\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Category
* @Gedmo\Tree(type="nested")
* @ORM\Table()
* @ORM\Entity(repositoryClass="Project\StoreBundle\Entity\CategoryRepository")
* @ORM\HasLifeCycleCallbacks()
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*
*@Assert\NotBlank(message="Please enter the name of categorie.")
*/
private $name;
//.....
/**
* @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;
/**
*non mapped property
*/
private $indentedName;
/**
* @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Store", inversedBy="categories", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $store ;
/**
* Constructor
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Get IndentedName
*
*/
public function getIndentedName()
{
return str_repeat("-----", $this->lvl).$this->name;
}
//.........
}
推荐答案
我不知道您能否成功解决问题,但我找到了解决方案:-)
很简单,只是在您的CategoryRepository中,而不是
I don't know do you success to solve your problem, but I found solution :-) It's very simple, just in your CategoryRepository instead of
$qb-> orderBy('c.rgt', 'DESC');
添加
$qb->add('orderBy','c.root ASC, c.lft ASC');
PS。非常感谢您的问题,这有助于我与Gedmo Tree一起工作:-)
PS. Thanks so much for your problem, it's help me to work with Gedmo Tree :-)
这篇关于更新后使用嵌套树扩展的选项列表的错误排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!