Skip to content

Commit

Permalink
Show warning if association property name is same as table field.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 27, 2016
1 parent 1804d87 commit 2d44ca9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/ORM/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ public function property($name = null)
if ($name === null && !$this->_propertyName) {
list(, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore($name);
if (in_array($this->_propertyName, $this->_sourceTable->schema()->columns())) {
$msg = 'Association property name "%s" clashes with field of same name of table "%s".' .
"\n" . 'You should explicitly specify the "propertyName" option.';
trigger_error(
sprintf($msg, $this->_propertyName, $this->_sourceTable->table()),
E_USER_WARNING
);
}
}
return $this->_propertyName;
}
Expand Down
16 changes: 12 additions & 4 deletions tests/TestCase/ORM/Association/BelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,12 @@ public function testSaveAssociatedNotEmptyNotIterable()
*/
public function testSaveAssociatedEmptySetSuccess($value)
{
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['_saveTarget', 'replaceLinks'],
['tags']
['tags', ['sourceTable' => $table]]
);
$entity = new Entity([
'id' => 1,
Expand All @@ -726,10 +728,12 @@ public function testSaveAssociatedEmptySetSuccess($value)
*/
public function testSaveAssociatedEmptySetUpdateSuccess($value)
{
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['_saveTarget', 'replaceLinks'],
['tags']
['tags', ['sourceTable' => $table]]
);
$entity = new Entity([
'id' => 1,
Expand All @@ -755,10 +759,12 @@ public function testSaveAssociatedEmptySetUpdateSuccess($value)
*/
public function testSaveAssociatedWithReplace()
{
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['replaceLinks'],
['tags']
['tags', ['sourceTable' => $table]]
);
$entity = new Entity([
'id' => 1,
Expand All @@ -782,10 +788,12 @@ public function testSaveAssociatedWithReplace()
*/
public function testSaveAssociatedWithReplaceReturnFalse()
{
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$assoc = $this->getMock(
'\Cake\ORM\Association\BelongsToMany',
['replaceLinks'],
['tags']
['tags', ['sourceTable' => $table]]
);
$entity = new Entity([
'id' => 1,
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/ORM/AssociationCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public function testSaveParentsFiltered()
*/
public function testSaveChildrenFiltered()
{
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$mockOne = $this->getMock(
'Cake\ORM\Association\HasMany',
['saveAssociated'],
Expand Down

0 comments on commit 2d44ca9

Please sign in to comment.