APY / APYDataGridBundle

Symfony Datagrid Bundle
MIT License
493 stars 343 forks source link

Alias names conflict #239

Closed stanislavprokopov closed 12 years ago

stanislavprokopov commented 12 years ago

I have found an alias name conflict. Entity has these 2 fields:

/**
* @GRID\Column(field="affiliate.personal.company_name")
*
* @ORM\OneToOne(targetEntity="XXX\UserBundle\Entity\User")
* @ORM\JoinColumn(name="affiliate_id", referencedColumnName="id")
*
*/
protected $affiliate;

/**
* @GRID\Column(field="distributor.personal.company_name")
*
* @ORM\OneToOne(targetEntity="XXX\UserBundle\Entity\User")
* @ORM\JoinColumn(name="distributor_id", referencedColumnName="id")
*
*/
protected $distributor;

When i try to create the grid, the sql fails if i have both affiliate.personal.company_name and distributor.personal.company_name. If i have only one of these fields, the query works ok. The problem is that when building alias names, the script takes "personal" and creates an alias "_personal". So when the second field is processed and it`s alias is created the first one is overwritten.

Fast fix, in the Grid\Source\Entity class, line 128-131:

if($parent != $previousParent) {
    $name = substr($parent.$element, 1);
} else {
    $name = $element;
}

$alias = '_' . $name;
$this->joins[$alias] = $parent . '.' . $element;
$previousParent = $parent;
$parent = $alias;

This fix will create alias names with the parent prefix: _affiliatepersonal AND _distributorpersonal

Abhoryo commented 12 years ago

oki, I'll take a look, thanks.

Abhoryo commented 12 years ago

Your code couldn't work for all cases.

clubdesarrolladores commented 12 years ago

this commit d5eaf96 break some of my grids

Example:

generated DQL:

SELECT 
_a.id, _a.confirmed, _a.type, _a.number, _a.date, 
_customer.name as customer::name, 
sum(_planned_charges.ammount) as planned_charges::ammount:sum, 
_a.currency, _a.conversion, _a.due_date 
FROM WebFactory\SaleBundle\Entity\Invoice _a 
LEFT JOIN _a.customer _customer 
LEFT JOIN _a.planned_charges _planned_charges 
GROUP BY planned_charges 
ORDER BY _a.date desc

GROUP BY planned_charges <-- alias problem

[Semantical Error] line 0, col 326 near 'planned_charges': Error: Cannot group by undefined identification or result variable.

clubdesarrolladores commented 12 years ago

Entity.php

line 123

$previousParent = ''; // <--- introduce bug (maybe)

line 151

if ($withAlias) {
    // Group by the primary field of the previous entity
    $previousParent = '_' . $previousParent; // <---- possible solution prepend '_'
    $this->query->addGroupBy($previousParent);
    $this->querySelectfromSource->addGroupBy($previousParent);

    return $functionWithParameters.' as '.substr($parent, 1).'::'.$matches['all'];
}
Abhoryo commented 12 years ago

Is it better now ?

stanislavprokopov commented 12 years ago

Does not work.

Error:

[Semantical Error] line 0, col 60 near '__user.email': Error: '__user' is not defined.

The error is because the alias is:

LEFT JOIN _a.user _user 

But in the query it uses __user.

UPDATED: After updating to latest fix 6501696, works ok.

Abhoryo commented 12 years ago

Not really ok because more than two level joined mapping isn't mapped into the result.

Can you try again ?

stanislavprokopov commented 12 years ago

Updated, works ok for me.

clubdesarrolladores commented 12 years ago

Work again! Thanks!