Skip to content

Commit

Permalink
Fix doctrine#7286: StringPrimary no longer accepts aggregate function…
Browse files Browse the repository at this point in the history
…s as argument
  • Loading branch information
Majkl578 committed Jul 4, 2018
1 parent 3cfcd6a commit e4189c0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Doctrine/ORM/Query/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,10 @@ public function StringPrimary()
case Lexer::T_COALESCE:
case Lexer::T_NULLIF:
return $this->CaseExpression();
default:
if ($this->isAggregateFunction($lookaheadType)) {
return $this->AggregateExpression();
}
}

$this->syntaxError(
Expand Down
81 changes: 81 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7286Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\OrmFunctionalTestCase;

final class GH7286Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();

$this->setUpEntitySchema(
[
GH7286Entity::class,
]
);
}

public function testLockModeIsRespected()
{
$entityA = new GH7286Entity('foo', 1);
$entityB = new GH7286Entity('foo', 2);
$entityC = new GH7286Entity('bar', 3);

$this->_em->persist($entityA);
$this->_em->persist($entityB);
$this->_em->persist($entityC);
$this->_em->flush();
$this->_em->clear();

$query = $this->_em->createQuery(
'SELECT CONCAT(e.type, MIN(e.version)) pair'
. ' FROM ' . GH7286Entity::class . ' e'
. ' GROUP BY e.type ORDER BY e.type'
);

self::assertSame(
[
['pair' => 'bar3'],
['pair' => 'foo1'],
],
$query->getArrayResult()
);
}
}

/**
* @Entity
*/
class GH7286Entity
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
* @var int
*/
public $id;

/**
* @Column
* @var string
*/
public $type;

/**
* @Column(type="integer")
* @var int
*/
public $version;

public function __construct(string $type, int $version)
{
$this->type = $type;
$this->version = $version;
}
}
7 changes: 7 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/LanguageRecognitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ public function testNewLiteralWithSubselectExpression()
{
$this->assertValidDQL("SELECT new " . __NAMESPACE__ . "\\DummyStruct(u.id, 'foo', (SELECT 1 FROM Doctrine\Tests\Models\CMS\CmsUser su), true) FROM Doctrine\Tests\Models\CMS\CmsUser u");
}

public function testStringPrimaryAcceptsAggregateExpression() : void
{
$this->assertValidDQL(
'SELECT CONCAT(a.topic, MAX(a.version)) last FROM Doctrine\Tests\Models\CMS\CmsArticle a GROUP BY a'
);
}
}

/** @Entity */
Expand Down

0 comments on commit e4189c0

Please sign in to comment.