APYDataGridBundle icon indicating copy to clipboard operation
APYDataGridBundle copied to clipboard

Problem using aggregation functions (string_agg), hasDQLFunction too restrictive

Open plfort opened this issue 9 years ago • 4 comments

Hello, How do you use the "string_agg" function with Postgresql ? I tried like this :

 @GRID\Source(columns="id,tag,description,roles.role:string_agg:,",filterable=true,groupBy={"id"})
 @GRID\Column(title="rolesstring",field="roles.role:string_agg:,")

or

 @GRID\Source(columns="id,tag,description,roles.role:string_agg:','",filterable=true,groupBy={"id"})
 @GRID\Column(title="rolesstring",field="roles.role:string_agg:','")

It requires the glue character at second argument like ',' ';' or what ever, but the pattern described in hasDQLFunction only accept "word" as second argument :

    public function hasDQLFunction(&$matches = null)
    {
        $regex = '/(?P<all>(?P<field>\w+):(?P<function>\w+)(:)?(?P<parameters>\w*))$/';

        return ($matches === null) ? preg_match($regex, $this->field) : preg_match($regex, $this->field, $matches);
    }

Moreover, the generated alias triggers DQL error :

//APY\DataGridBundle\Grid\Source\Entity line 170
 $alias = str_replace('.', '::', $column->getId());

QueryException: [Syntax Error] line 0, col 60: Error: Expected Doctrine\ORM\Query\Lexer::T_FROM, got ':'

SELECT _a.id, _a.tag, _a.description, _roles.role:string_agg:';' as roles::role:string_agg:';' FROM Options _a LEFT JOIN _a.roles _roles  GROUP BY _a.id ORDER BY _roles.role:string_agg:';' asc

I think it would be great to force the alias with annotation or improve the generation mechanism, and modify the regex to allow other characters than word.

I did some tests to allow second parameter between quote with this regex :

 $regex = '/(?P<all>(?P<field>\w+):(?P<function>\w+)(:)?([\'"]?)(?P<parameters>[^\5]*)\5)$/';

plfort avatar Oct 21 '14 12:10 plfort

Great regex, you can push a PR for that.

I think it would be great to force the alias with annotation You can add to your query manuals fields.

Abhoryo avatar Oct 22 '14 12:10 Abhoryo

@plfort it'll be very nice if you purpose a PR for this :)

hmert avatar Dec 14 '15 19:12 hmert

Ok, I will do it

plfort avatar Dec 14 '15 19:12 plfort

@plfort maybe this will work for you?

DonCallisto avatar Mar 20 '18 17:03 DonCallisto