Possibly a more robust category list in your select dropdowns

54 views
Skip to first unread message

keymaster

unread,
Apr 2, 2012, 11:02:12 AM4/2/12
to syliu...@googlegroups.com

Was looking at your code in CategorizerBundle, in particular the updateAction() in CategoryController.

I haven't tried it out (mainly because I am still on Symfony2.0 and there is a 2.1 dependancy, as per https://github.com/Sylius/SyliusCategorizerBundle/issues/6), but it appears the code doesn't prevent assigning a category's parent fto itself or even to a node in it's own subtree (which would be a logical inconsistency).

Is this correct?

If so, you may want to have a look at the editAction() in this categoryController: https://github.com/l3pp4rd/gediminasm.org/blob/master/src/Gedmo/DemoBundle/Controller/CategoryController.php

You'll see the CategoryEntityLoader seems to what we need with it's query. The main part of the query is in the getEntities() function.

In the code below, $this->basedOnNode is the node being edited. In your updateAction() it would be the equivalent of your $category variable.

public function getEntities()
    {
        $qb = $this->em
            ->createQueryBuilder()
            ->select('c')
            ->from('GedmoDemoBundle:Category', 'c')
        ;
        if (!is_null($this->basedOnNode)) {
            $qb->where($qb->expr()->notIn(
                'c.id',
                $this->em
                    ->createQueryBuilder()
                    ->select('n')
                    ->from('GedmoDemoBundle:Category', 'n')
                    ->where('n.root = '.$this->basedOnNode->getRoot())
                    ->andWhere($qb->expr()->between(
                        'n.lft',
                        $this->basedOnNode->getLeft(),
                        $this->basedOnNode->getRight()
                    ))
                    ->getDQL()
            ));
        }
        $q = $qb->getQuery();
        $this->categoryController->setTranslatableHints($q);
        return $q->getResult();
    }


You may want to implement a similiar query in your categorizer bundle (and perhaps open the possibility of removing the Symfony 2.1 dependancy while you're at it).

Hope this helps.

keymaster

unread,
Apr 2, 2012, 11:06:56 AM4/2/12
to syliu...@googlegroups.com
Here is the demo, so you can see how it works: http://gediminasm.org/demo/en/category/edit/2

When you edit the Cars category, you are only able to set the parent to semantically valid nodes, not to itself or any node in the Car's subtree.

Paweł Jędrzejewski

unread,
Apr 2, 2012, 4:16:57 PM4/2/12
to syliu...@googlegroups.com
Hey! Thanks for the info, yes indeed that is an issue. Quite serious flaw... What do you think about adding another type, let's say ParentCategoryChoiceType.php? Which would handle all checks about parent, and exclude the edited category? We should keep CategoryChoiceType as it is.
 

keymaster

unread,
Apr 3, 2012, 2:07:44 AM4/3/12
to syliu...@googlegroups.com

As I see it, these are the situations which need to be handled:

- assigning a new product to a category.
- assigning a new product to no category (ie. null).
- assigning a new product to multiple categories.
- assigning an existing product to a different category
- assigning an existing product to multiple categories.

- assigning a new category to a parent
- assigning an existing category to no parent (ie. null parent)
- assigning an existing category to a new parent

The problem exists only in the last two cases.

So, I think your approach makes sense.

CategoryChoiceType would be used when assigning products to a category.
ParentCategoryChoiceType would be used when assigning categories to a parent category.


Paweł Jędrzejewski

unread,
Apr 3, 2012, 2:46:24 PM4/3/12
to syliu...@googlegroups.com
Yes I will work on that as soon as I find time, still not sure how to handle padding in choice list. It should be determined by Category.treeLevel. 
Reply all
Reply to author
Forward
0 new messages