Skip to content
Permalink
Browse files

Reduce code duplication.

  • Loading branch information...
ADmad committed Feb 27, 2016
1 parent fde081f commit be4eeef6de2426bc0e385b8228bafb24b6040828
@@ -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.';
@@ -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,
@@ -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));
}
/**
@@ -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));
}
/**
@@ -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'));
@@ -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'],
@@ -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'],

0 comments on commit be4eeef

Please sign in to comment.
You can’t perform that action at this time.