Skip to content

Commit

Permalink
Reduce code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Feb 27, 2016
1 parent fde081f commit be4eeef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
14 changes: 12 additions & 2 deletions src/ORM/Association.php
Expand Up @@ -436,8 +436,7 @@ public function property($name = null)
$this->_propertyName = $name;
}
if ($name === null && !$this->_propertyName) {
list(, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore($name);
$this->_propertyName = $this->_propertyName();
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.';
Expand All @@ -450,6 +449,17 @@ public function property($name = null)
return $this->_propertyName;
}

/**
* Returns default property name based on association name.
*
* @return string
*/
protected function _propertyName()
{
list(, $name) = pluginSplit($this->_name);
return Inflector::underscore($name);
}

/**
* Sets the strategy name to be used to fetch associated records. Keep in mind
* that some association types might not implement but a default strategy,
Expand Down
17 changes: 4 additions & 13 deletions src/ORM/Association/BelongsTo.php
Expand Up @@ -72,23 +72,14 @@ public function cascadeDelete(EntityInterface $entity, array $options = [])
}

/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
* Returns default property name based on association name.
*
* @param string|null $name The property name, use null to read the current property.
* @return string
*/
public function property($name = null)
protected function _propertyName()
{
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_propertyName) {
list(, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
}
return $this->_propertyName;
list(, $name) = pluginSplit($this->_name);
return Inflector::underscore(Inflector::singularize($name));
}

/**
Expand Down
17 changes: 4 additions & 13 deletions src/ORM/Association/HasOne.php
Expand Up @@ -57,23 +57,14 @@ public function foreignKey($key = null)
}

/**
* Sets the property name that should be filled with data from the target table
* in the source table record.
* If no arguments are passed, currently configured type is returned.
* Returns default property name based on association name.
*
* @param string|null $name The name of the property. Pass null to read the current value.
* @return string
*/
public function property($name = null)
protected function _propertyName()
{
if ($name !== null) {
return parent::property($name);
}
if ($name === null && !$this->_propertyName) {
list(, $name) = pluginSplit($this->_name);
$this->_propertyName = Inflector::underscore(Inflector::singularize($name));
}
return $this->_propertyName;
list(, $name) = pluginSplit($this->_name);
return Inflector::underscore(Inflector::singularize($name));
}

/**
Expand Down
12 changes: 9 additions & 3 deletions tests/TestCase/ORM/AssociationCollectionTest.php
Expand Up @@ -91,7 +91,11 @@ public function testRemoveAll()
*/
public function testGetByProperty()
{
$belongsTo = new BelongsTo('Users', []);
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$belongsTo = new BelongsTo('Users', [
'sourceTable' => $table
]);
$this->assertEquals('user', $belongsTo->property());
$this->associations->add('Users', $belongsTo);
$this->assertNull($this->associations->get('user'));
Expand Down Expand Up @@ -186,7 +190,8 @@ public function testCascadeDelete()
*/
public function testSaveParents()
{
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$mockOne = $this->getMock(
'Cake\ORM\Association\BelongsTo',
['saveAssociated'],
Expand Down Expand Up @@ -235,7 +240,8 @@ public function testSaveParents()
*/
public function testSaveParentsFiltered()
{
$table = $this->getMock('Cake\ORM\Table', [], [[]]);
$table = $this->getMock('Cake\ORM\Table', ['table'], [[]]);
$table->schema([]);
$mockOne = $this->getMock(
'Cake\ORM\Association\BelongsTo',
['saveAssociated'],
Expand Down

0 comments on commit be4eeef

Please sign in to comment.