Error arrangement of option-list using nested Tree extension after update

半世苍凉 提交于 2019-12-11 22:50:02

问题


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 :

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 :

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 .

How can I solve that ?

This is the code :

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';
}
}

Entity 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;
}

//.........

}

回答1:


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');

add

$qb->add('orderBy','c.root ASC, c.lft ASC');

PS. Thanks so much for your problem, it's help me to work with Gedmo Tree :-)



来源:https://stackoverflow.com/questions/25144223/error-arrangement-of-option-list-using-nested-tree-extension-after-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!