l3pp4rd/DoctrineExtensions — Issue #172: [Tree] tree with inheritance showing too much
Author: ibizaman
Created: 2011-10-15 13:25:55+00:00
State: open

# PROBLEM

I have 3 entities : **BaseMetier**, **ImmoMetier** and **WorkMetier**. The last two extends **BaseMetier**.
The nested tree fields are set on the parent class.

The problem : when I use `childrenQueryBuilder()` from the `NestedTreeRepository` on my **ImmoMetier** class, it retreives me every rows from **ImmoMetier** *AND* from **WorkMetier**.

This happens when I call `childrenTranslated()` from `MetierRepository`, see below.

# ONE SOLUTION

I narrowed the problem to line 241 of file NestedTreeRepository.php :

```php
_em->createQueryBuilder();
/*240.*/ $qb->select('node')
/*241.*/ ->from($config['useObjectClass'], 'node')
/*242.*/ ;
```

In fact, the `'useObjectClass'` is the string where the nested tree is defined, so it's *BaseMetier* class.
But it should be *ImmoMetier* for the query to work as expected.

To get only **ImmoMetier**'s rows, you can change line 241 to :

```php
from($meta->name, 'node')
```

Now, `$meta->name` retreives the name of the calling class, so it's *ImmoMetier* and I get only the correct rows.

It works, but I don't know if I done something bad...

# SQL

## SQL - before correction
```sql
SELECT b0_.id AS id0,
b0_.enabled AS enabled1,
b0_.lft AS lft2,
b0_.rgt AS rgt3,
b0_.lvl AS lvl4,
b0_.root AS root5,
b0_.nom AS nom6,
b0_.nom AS nom7,
b0_.nom AS nom8,
b0_.type AS type9, -
b0_.parent_id AS parent_id10
FROM BaseMetier b0_
WHERE (b0_.parent_id IS NULL)
AND b0_.type IN ('immo', 'work')
ORDER BY b0_.root ASC
```

## SQL - after correction
```sql
SELECT b0_.id AS id0,
b0_.enabled AS enabled1,
b0_.lft AS lft2,
b0_.rgt AS rgt3,
b0_.lvl AS lvl4,
b0_.root AS root5,
t1_.content AS nom6,
b0_.type AS type7,
b0_.parent_id AS parent_id8
FROM BaseMetier b0_
LEFT JOIN ext_translations t1_
ON t1_.locale = 'fr'
AND t1_.object_class = 'Kieski\\MetierBundle\\Entity\\WorkMetier'
AND t1_.field = 'nom'
AND t1_.foreign_key = b0_.id
WHERE (b0_.parent_id IS NULL)
AND b0_.type IN ('work')
ORDER BY b0_.root ASC
```

# CONFIG

## BaseMetier
```php
getNestedPrint();}

public function getNestedPrint()
{
$prefix = '';
for ($i = 0; $i < $this->getLvl(); $i++) {
$prefix .= '-';
}
return $prefix . ' ' . $this->getNom();
}

public function getId() {return $this->id;}

public function setNom($nom) {$this->nom = $nom;}
public function getNom() {return $this->nom;}

public function setEnabled($enabled = true) {$this->enabled = $enabled;}
public function isEnabled() {return $this->enabled;}

public function setLvl($lvl) {$this->lvl = $lvl;}
public function getLvl() {return $this->lvl;}

public function setRoot($root) {$this->root = $root;}
public function getRoot() {return $this->root;}

public function setParent($parent) {$this->parent = $parent;}
public function getParent() {return $this->parent;}

public function getChildren() {return $this->children;}

abstract public function getType();
}
```

## ImmoMetier
```php
getQuery();
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);

return $query->getResult();
}
}
```