Skip to content

Commit

Permalink
Adding the source() method to Association
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 10, 2013
1 parent 12759c4 commit 9bb1f90
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
25 changes: 23 additions & 2 deletions lib/Cake/ORM/Association.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ abstract class Association {
*/
protected $_dependent = false;

/**
* Source table instance
*
* @var Cake\ORM\Table
*/
protected $_sourceTable;

/**
* Target table instance
*
Expand All @@ -86,7 +93,7 @@ abstract class Association {
* @return void
*/
public function __construct($name, array $options = []) {
$defaults = ['className', 'foreignKey', 'conditions', 'dependent'];
$defaults = ['className', 'foreignKey', 'conditions', 'dependent', 'sourceTable'];
foreach ($defaults as $property) {
if (isset($options[$property])) {
$this->{'_' . $property} = $options[$property];
Expand Down Expand Up @@ -115,14 +122,28 @@ public function name($name = null) {
return $this->_name;
}

/**
* Sets the table instance for the source side of the association. If no arguments
* are passed, the current configured table instance is returned
*
* @param Cake\ORM\Table $table the instance to be assigned as source side
* @return Cake\ORM\Table
*/
public function source(Table $table = null) {
if ($table === null) {
return $this->_sourceTable;
}
return $this->_sourceTable = $table;
}

/**
* Sets the table instance for the target side of the association. If no arguments
* are passed, the current configured table instance is returned
*
* @param Cake\ORM\Table $table the instance to be assigned as target side
* @return Cake\ORM\Table
*/
public function repository(Table $table = null) {
public function target(Table $table = null) {
if ($table === null && $this->_targetTable) {
return $this->_targetTable;
}
Expand Down
28 changes: 22 additions & 6 deletions lib/Cake/Test/TestCase/ORM/AssociationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class TestTable extends Table {
class AssociationTest extends \Cake\TestSuite\TestCase {

public function setUp() {
$this->source = new TestTable;
$config = [
'className' => '\Cake\Test\TestCase\ORM\TestTable',
'foreignKey' => 'a_key',
'conditions' => ['field' => 'value'],
'dependent' => true
'dependent' => true,
'sourceTable' => $this->source
];
$this->association = $this->getMock(
'\Cake\ORM\Association',
Expand Down Expand Up @@ -103,17 +105,31 @@ public function testCanBeJoined() {
}

/**
* Tests that repository() returns the correct Table object
* Tests that target() returns the correct Table object
*
* @return void
*/
public function testRepository() {
$table = $this->association->repository();
public function testTarget() {
$table = $this->association->target();
$this->assertInstanceOf(__NAMESPACE__ . '\TestTable', $table);

$other = new Table;
$this->association->repository($other);
$this->assertSame($other, $this->association->repository());
$this->association->target($other);
$this->assertSame($other, $this->association->target());
}

/**
* Tests that source() returns the correct Table object
*
* @return void
*/
public function testSource() {
$table = $this->association->source();
$this->assertSame($this->source, $table);

$other = new Table;
$this->association->source($other);
$this->assertSame($other, $this->association->source());
}

}

0 comments on commit 9bb1f90

Please sign in to comment.