stof / StofDoctrineExtensionsBundle

Integration bundle for DoctrineExtensions by l3pp4rd in Symfony
https://symfony.com/bundles/StofDoctrineExtensionsBundle/current/index.html
MIT License
1.89k stars 380 forks source link

Getting leaves in Nested Tree - problem #85

Closed masterspambot closed 12 years ago

masterspambot commented 12 years ago

Hi. To get all the leaves from the tree i implemented simple function extending NestedTreeRepository.php

<?php
...
use Doctrine\ORM\EntityRepository;
...
public function getLeaves()
    {
        $em = $this->getEntityManager();

        $leaves = $em->createQuery('SELECT p 
                                    FROM CatalogerCatalogBundle:Category p 
                                    WHERE p.id NOT IN(
                                            SELECT DISTINCT l.parent_id 
                                            FROM CatalogerCatalogBundle:Category l 
                                            WHERE l.parent_id IS NOT NULL
                                    )'
                                );      

            return $leaves->getResult();
    }

To retrive parent_id I added extra field to my Category.php class, which is not accessible by default:

<?php
    /**
    * @ORM\Column(name="parent_id", type="integer", nullable=true)
    */
    private $parent_id;  

    /**
     * Set parent_id
     *
     * @param integer $parentId
     */
    public function setParentId($parentId)
    {
        $this->parent_id = $parentId;
    }

    /**
     * Get parent_id
     *
     * @return integer 
     */
    public function getParentId()
    {
        return $this->parent_id;
    }

Everything works great in dev environtment, but if i'm trying to get nodes in production env. i got error 500 issue. I'm not realy sure if it's StofDoctrineExtensionsBundle error or Doctrine error, because if I remove subquery from selset everything works fine in both environments. Please chceck this and if u find any solution share it with me.

stof commented 12 years ago

First point, you should not have a field with the id in the PHP code but a relation to the parent node. It would be far much cleaner.

And did you cleared your prod cache and warmed it up so that proxies are regenerated ? php app/console cache:clear --env=prod --no-debug

masterspambot commented 12 years ago

I know my method is not the finest but it works quite fast. at least. But how to transform this select to use the 'parent' instead of _'parentid'? Maybe my knowledge is not sufficient, so that I used the parent id.

And of course i cleared the cache. No help. I'm really upset.

stof commented 12 years ago

Did you cleared it using --no-debug ? When running the command in debug mode, the cache is warmed up in debug mode and as the debug mode will generate the proxies for each request anyway, it does not run the cache warmer.

the doc of the extensions sets the relation to the parent in the example: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/tree.md (and this is even needed by the Tree extension). And the query is already implemented in a more efficient way (without using a subquery) in the NestedTreeRepository: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php#L381

masterspambot commented 12 years ago

Ok. I did it with --no-debug. Didn't make any change.

What helped? :) sudo rm -R app/cache/ app/log/

And btw thanks for links to documentation. I thought it wasn't implemented... So I was trying to make a workaround. My bad.

Thanks for help.

I'm closing the issue.