What are the Slack Archives?

Itโ€™s a history of our time together in the Slack Community! Thereโ€™s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you wonโ€™t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Good morning, is there any example of `groupBy`-queries? I tried the automatically generated functio

U016ZPC5480
U016ZPC5480 Posts: 24 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

Good morning, is there any example of groupBy-queries? I tried the automatically generated functions, but they have no effect on the result of the query. E.g:

public function findUniqueImportReports(): ObjectCollection
{
    $query = $this->getFactory()->createImportReportRepository()
        ->groupByType()
        ->groupByErrorMessage();

    return $query->find();
}

Adding count() or max() - selects unfortunately does not change the result either.

Comments

  • sprymiker
    sprymiker Sprykee Posts: 781 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    If you use PostgreSQL, Propel adds all columns in GROUP BY statement. So obviously it wonโ€™t work as expected.

  • sprymiker
    sprymiker Sprykee Posts: 781 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    By SQL standard, all non-aggregating columns must be added in GROUP BY.
    MySQL does not follow it and does not require it. PGSQL does.

  • sprymiker
    sprymiker Sprykee Posts: 781 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    And it is no good way to do it with Propel.

    However, you can make the query like this:

    $query = $this->getFactory()->createImportReportRepository()
            ->addSelfSelectColumns()
            ->clearSelectColumns()
            ->withColumn(sprintf('COUNT(*)'))
            ->select(['type', 'error_message'])
            ->groupByType()
            ->groupByErrorMessage();
    
  • U016ZPC5480
    U016ZPC5480 Posts: 24 ๐Ÿง‘๐Ÿปโ€๐Ÿš€ - Cadet

    Perfect, thanks for the explanation.