-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix root option on TaxonChoiceType #7254
Fix root option on TaxonChoiceType #7254
Conversation
Pitoune
commented
Jan 13, 2017
•
edited
Loading
edited
Q | A |
---|---|
Bug fix? | yes |
New feature? | no |
BC breaks? | yes |
Related tickets | |
License | MIT |
@@ -81,7 +80,14 @@ public function configureOptions(OptionsResolver $resolver) | |||
$resolver | |||
->setDefaults([ | |||
'choices' => function (Options $options) { | |||
$taxons = $this->taxonRepository->findNodesTreeSorted(); | |||
if (null !== $options['root']) { | |||
if ($options['root'] instanceof TaxonInterface) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it too much magic? Why don't we stay with only taxon code in root
?
@@ -81,7 +80,14 @@ public function configureOptions(OptionsResolver $resolver) | |||
$resolver | |||
->setDefaults([ | |||
'choices' => function (Options $options) { | |||
$taxons = $this->taxonRepository->findNodesTreeSorted(); | |||
if (null !== $options['root']) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be extracted to private method
@@ -43,6 +42,24 @@ public function findChildren($parentCode) | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function findChildrenByRootCode($rootCode) | |||
{ | |||
$queryBuilder = $this->createQueryBuilder('o'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant
783f707
to
910bb12
Compare
->leftJoin('o.root', 'root') | ||
->andWhere('root.code = :code') | ||
; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid this nasty else
statement. Would be a good to extract this to private method. But also there is a lot of logic in this if
... So maybe two different methods, findChildren
and findChildrenForRootParent
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I did in the first place but @lchrusciel said the method findChildrenForRootParent
was redundant with the findChildren
one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I wasn't clear enough... I meant that the variable assignment is redundant... Sorry about that :)
return $this->createQueryBuilder('o')
->addSelect('child')
->leftJoin('o.children', 'child')
->leftJoin('o.parent', 'parent')
->andWhere('parent.code = :code')
;
|
||
/** | ||
* @param null|string $root | ||
* @param null|callable $filter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string|null
and callable|null
. Also we should not align parameters vertically.
* @param null|string $root | ||
* @param null|callable $filter | ||
* | ||
* @return array|TaxonInterface[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TaxonInterface[]
would be sufficient, array|
indicates as there could be something else ;)
{ | ||
if (null !== $root) { | ||
$taxons = $this->taxonRepository->findChildren($root, true); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that another extraction would not be a good option (two methods for a simple case), but else
does not look good too :/ How could we make it shiny and perfect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding a nullable argument $rootCode
to method findNodesTreeSorted
? It means putting some logic in the repository but we only need an if
without else
.
@@ -25,10 +25,11 @@ | |||
{ | |||
/** | |||
* @param string $parentCode | |||
* @param bool $isParentRoot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, do not indent parameters vertically.
@@ -23,20 +22,30 @@ class TaxonRepository extends EntityRepository implements TaxonRepositoryInterfa | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function findChildren($parentCode) | |||
public function findChildren($parentCode, $isParentRoot = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findChildren('CODE', true)
shouldn't return grandchildren etc.
It should be rather findChildren($parentCode)
and findDescendants($rootCode)
.
5308eda
to
5f907b2
Compare
} | ||
|
||
return $taxons; | ||
return $this->getTaxons($options['root'], $options['filter']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rename this option to root_code
to avoid confusion.
5f907b2
to
d17ad26
Compare
d17ad26
to
8e9a2bb
Compare
@michalmarcinkowski did you have time to have a look on my last commit? |
I'm not a fan of optional arguments, but creating a separate method for that doesn't seem a better choice. Thanks Pierre! 👍 |