Skip to content

Commit

Permalink
Adding methods to Table to create associations
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 12, 2013
1 parent 6ee3140 commit 22c3ced
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/Cake/ORM/Association/BelongsTo.php
Expand Up @@ -59,10 +59,9 @@ public function foreignKey($key = null) {
*
* - includeFields: Whether to include target model fields in the result or not
* - foreignKey: The name of the field to use as foreign key, if false none
* will be sued
* will be used
* - conditions: array with a list of conditions to filter the join with
* - fields: a list of fields in the target table to include in the result
* - type: The type of join to be used (e.g. INNER)
* - joinType: The type of join to be used (e.g. INNER)
*
* @param Query $query the query to be altered to include the target table data
* @param array $options Any extra options or overrides to be taken in account
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/ORM/Association/HasOne.php
Expand Up @@ -66,7 +66,7 @@ public function foreignKey($key = null) {
*
* - includeFields: Whether to include target model fields in the result or not
* - foreignKey: The name of the field to use as foreign key, if false none
* will be sued
* will be used
* - conditions: array with a list of conditions to filter the join with
* - fields: a list of fields in the target table to include in the result
* - type: The type of join to be used (e.g. INNER)
Expand Down
89 changes: 89 additions & 0 deletions lib/Cake/ORM/Table.php
Expand Up @@ -16,6 +16,8 @@
*/
namespace Cake\ORM;

use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Association\HasOne;
use Cake\Utility\Inflector;

class Table {
Expand All @@ -34,6 +36,10 @@ class Table {

protected $_schema;

protected $_primaryKey = 'id';

protected $_associations = [];

public function __construct($config = array()) {
if (!empty($config['table'])) {
$this->_table = $config['table'];
Expand Down Expand Up @@ -143,6 +149,89 @@ public function schema($schema = null) {
return $this->_schema = $schema;
}

public function primaryKey($key = null) {
if ($key !== null) {
$this->_primaryKey = $key;
}
return $this->_primaryKey;
}

/**
* Returns a association objected configured for the specified alias if any
*
* @param string $name the alias used for the association
* @return Cake\ORM\Association
*/
public function association($name) {
if (isset($this->_associations[$name])) {
return $this->_associations[$name];
}

return null;
}

/**
* Creates a new BelongsTo association between this table and a target
* table. A "belongs to" association is a 1-N relationship where this table
* is the N side, and where there is a single associated record in the target
* table for each one in this table.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the class name to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key, if false none
* will be used
* - conditions: array with a list of conditions to filter the join with
* - joinType: The type of join to be used (e.g. INNER)
*
* This method will return the recently built association object
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\BelongsTo
*/
public function belongsTo($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new BelongsTo($associated, $options);
return $this->_associations[$association->name()] = $association;
}

/**
* Creates a new HasOne association between this table and a target
* table. A "has one" association is a 1-1 relationship.
*
* Target table can be inferred by its name, which is provided in the
* first argument, or you can either pass the class name to be instantiated or
* an instance of it directly.
*
* The options array accept the following keys:
*
* - className: The class name of the target table object
* - targetTable: An instance of a table object to be used as the target table
* - foreignKey: The name of the field to use as foreign key, if false none
* will be used
* - conditions: array with a list of conditions to filter the join with
* - joinType: The type of join to be used (e.g. LEFT)
*
* This method will return the recently built association object
*
* @param string $associated the alias for the target table. This is used to
* uniquely identify the association
* @param array $options list of options to configure the association definition
* @return Cake\ORM\Association\HasOne
*/
public function hasOne($associated, array $options = []) {
$options += ['sourceTable' => $this];
$association = new HasOne($associated, $options);
return $this->_associations[$association->name()] = $association;
}

public function find($type, $options = []) {
return $this->{'find' . ucfirst($type)}($this->buildQuery(), $options);
}
Expand Down
34 changes: 34 additions & 0 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -194,4 +194,38 @@ public function testFindAllConditionAutoTypes() {
$this->assertSame($expected, $query->toArray());
}

/**
* Tests that belongsTo() creates and configures correctly the association
*
* @return void
*/
public function testBelongsTo() {
$options = ['foreignKey' => 'fake_id', 'conditions' => ['a' => 'b']];
$table = new Table(['table' => 'dates']);
$belongsTo = $table->belongsTo('user', $options);
$this->assertInstanceOf('\Cake\ORM\Association\BelongsTo', $belongsTo);
$this->assertSame($belongsTo, $table->association('user'));
$this->assertEquals('user', $belongsTo->name());
$this->assertEquals('fake_id', $belongsTo->foreignKey());
$this->assertEquals(['a' => 'b'], $belongsTo->conditions());
$this->assertSame($table, $belongsTo->source());
}

/**
* Tests that hasOne() creates and configures correctly the association
*
* @return void
*/
public function testHasOne() {
$options = ['foreignKey' => 'user_id', 'conditions' => ['b' => 'c']];
$table = new Table(['table' => 'users']);
$hasOne = $table->hasOne('profile', $options);
$this->assertInstanceOf('\Cake\ORM\Association\HasOne', $hasOne);
$this->assertSame($hasOne, $table->association('profile'));
$this->assertEquals('profile', $hasOne->name());
$this->assertEquals('user_id', $hasOne->foreignKey());
$this->assertEquals(['b' => 'c'], $hasOne->conditions());
$this->assertSame($table, $hasOne->source());
}

}

0 comments on commit 22c3ced

Please sign in to comment.