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

Commit

Permalink
Merge branch 'hotfix/update-unalias-table' into develop
Browse files Browse the repository at this point in the history
Forward port #31
  • Loading branch information
mwillbanks committed Oct 14, 2015
2 parents acfb14b + a386117 commit 907353a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#31](https://github.com/zendframework/zend-db/pull/31) fixes table gateway
update when there is a table alias utilized.

## 2.6.1 - TBD

Expand Down
12 changes: 12 additions & 0 deletions src/TableGateway/AbstractTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,24 @@ protected function executeUpdate(Update $update)
// apply preUpdate features
$this->featureSet->apply(EventFeatureEventsInterface::EVENT_PRE_UPDATE, [$update]);

$unaliasedTable = false;
if (is_array($updateState['table'])) {
$tableData = array_values($updateState['table']);
$unaliasedTable = array_shift($tableData);
$update->table($unaliasedTable);
}

$statement = $this->sql->prepareStatementForSqlObject($update);
$result = $statement->execute();

// apply postUpdate features
$this->featureSet->apply(EventFeatureEventsInterface::EVENT_POST_UPDATE, [$statement, $result]);

// Reset original table information in Update instance, if necessary
if ($unaliasedTable) {
$update->table($updateState['table']);
}

return $result->getAffectedRows();
}

Expand Down
66 changes: 66 additions & 0 deletions test/TableGateway/TableGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Zend\Db\TableGateway\Feature;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Update;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\TableIdentifier;

Expand Down Expand Up @@ -200,4 +201,69 @@ public function testInsertShouldResetTableToUnaliasedTable($tableValue, $expecte
$state['table']
);
}

/**
* @dataProvider aliasedTables
*/
public function testUpdateShouldResetTableToUnaliasedTable($tableValue, $expected)
{
$phpunit = $this;

$update = new Update();
$update->table($tableValue);

$result = $this->getMockBuilder('Zend\Db\Adapter\Driver\ResultInterface')
->getMock();
$result->expects($this->once())
->method('getAffectedRows')
->will($this->returnValue(1));

$statement = $this->getMockBuilder('Zend\Db\Adapter\Driver\StatementInterface')
->getMock();
$statement->expects($this->once())
->method('execute')
->will($this->returnValue($result));

$statementExpectation = function ($update) use ($phpunit, $expected, $statement) {
$state = $update->getRawState();
$phpunit->assertSame($expected, $state['table']);
return $statement;
};

$sql = $this->getMockBuilder('Zend\Db\Sql\Sql')
->disableOriginalConstructor()
->getMock();
$sql->expects($this->atLeastOnce())
->method('getTable')
->will($this->returnValue($tableValue));
$sql->expects($this->once())
->method('update')
->will($this->returnValue($update));
$sql->expects($this->once())
->method('prepareStatementForSqlObject')
->with($this->equalTo($update))
->will($this->returnCallback($statementExpectation));

$table = new TableGateway(
$tableValue,
$this->mockAdapter,
null,
null,
$sql
);

$result = $table->update([
'foo' => 'FOO',
], [
'bar' => 'BAR'
]);

$state = $update->getRawState();
var_dump($state);
$this->assertInternalType('array', $state['table']);
$this->assertEquals(
$tableValue,
$state['table']
);
}
}

0 comments on commit 907353a

Please sign in to comment.