Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Sql-query breaks on 2.4.0 compare to 2.3.7 #7407

Closed
freax opened this issue Apr 2, 2015 · 5 comments
Closed

Sql-query breaks on 2.4.0 compare to 2.3.7 #7407

freax opened this issue Apr 2, 2015 · 5 comments
Labels
Milestone

Comments

@freax
Copy link
Contributor

freax commented Apr 2, 2015

    ...
    $select->columns(array(
        'count'        => new \Zend\Db\Sql\Expression('COUNT(*)'),
        'yearMonth'    => new \Zend\Db\Sql\Expression('FROM_UNIXTIME(date, "%Y-%m")'),
    ));
    $select->where(array('user' => $user));
    $select->group('yearMonth');

    $adapter = $this->getDbAdapter();
    $selectString = $sql->getSqlStringForSqlObject($select);

    echo $selectString;

In 2.3.7 this code echoed:

    SELECT COUNT(*) AS `count`, FROM_UNIXTIME(date, "%Y-%m") AS `yearMonth` FROM `table` WHERE `user` = '1' GROUP BY `yearMonth`

and everything was fine, but in 2.4.0:

    SELECT COUNT(*) AS `count`, FROM_UNIXTIME(date, "%%Y-%%m") AS `yearMonth` FROM `table` WHERE `user` = '1' GROUP BY `yearMonth`

And I have to rollback to 2.3.7 because of this. May be it related to #7400.

@weierophinney
Copy link
Member

Unrelated to #7400. I'll see if I can hunt this down.

@samsonasik
Copy link
Contributor

I think it from https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Sql/Expression.php#L136 on code :

$expression = str_replace('%', '%%', $this->expression);

@freax
Copy link
Contributor Author

freax commented Apr 3, 2015

@samsonasik this line exists in 2.3.7 too
As I can see, at least the whole \Zend\Db\Sql\Expression::getExpressionData() was been rewritten

@Martin-P
Copy link
Contributor

Martin-P commented Apr 4, 2015

I created a unittest for this and checked Zend\Db\Sql\Expression back to the version where it passes. The line @samsonasik is referring to is indeed the line causing the problem: commit 455f3e1#diff-7a16e1c9e1d21e2bbc77ffe77ae82484 (PR #1443). It seems to be a part of the ZF 2.0.0 release which means the bug should also be present in ZF 2.3.7 (it is when I run the unittest below on ZF 2.3.7).

Before the introduction of that line the test passes, after the introduction of that line the test fails.

/**
 * @group 7407
 */
public function testGetExpressionDatePreservesPercentageSign()
{
    $expressionString = 'FROM_UNIXTIME(date, "%Y-%m")';
    $expression       = new Expression($expressionString);

    $this->assertSame($expressionString, $expression->getExpression());
}

Edit:
On second thought... all works fine. Also in ZF 2.4.0.

Edit 2:
When running the code not in a unittest it fails...

@Martin-P
Copy link
Contributor

Martin-P commented Apr 4, 2015

Found the source of the problem.

The actual problem was caused by a refactoring of Zend\Db\Sql\AbstractSql. It assumed the data from getExpressionData() contained the correct data, but instead getExpression() should be used. This is corrected and fixed in #7422.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants