Skip to content
Permalink
Browse files

Implementing belongsTo association

  • Loading branch information...
lorenzo committed May 12, 2013
1 parent 9bb1f90 commit af1d7739751d1d622ef2443622a4f692dc5f9c87
@@ -93,7 +93,14 @@ abstract class Association {
* @return void
*/
public function __construct($name, array $options = []) {
$defaults = ['className', 'foreignKey', 'conditions', 'dependent', 'sourceTable'];
$defaults = [
'className',
'foreignKey',
'conditions',
'dependent',
'sourceTable',
'targetTable'
];
foreach ($defaults as $property) {
if (isset($options[$property])) {
$this->{'_' . $property} = $options[$property];
@@ -0,0 +1,66 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\ORM\Association;
use Cake\ORM\Association;
use Cake\ORM\Query;
use Cake\Utility\Inflector;
class BelongsTo extends Association {
protected $_canBeJoined = true;
public function foreignKey($key = null) {
if ($key === null) {
if ($this->_foreignKey === null) {
$this->_foreignKey = Inflector::underscore($this->target()->alias()) . '_id';
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
public function attachTo(Query $query, array $options = []) {
$target = $this->target();
$source = $this->source();
$options += ['includeFields' => true, 'foreignKey' => $this->foreignKey()];
if (!empty($options['foreignKey'])) {
$options['conditions'][] = sprintf('%s.%s = %s.%s',
$target->alias(),
$source->primaryKey(),
$source->alias(),
$options['foreignKey']
);
}
$joinOptions = ['table' => 1, 'conditions' => 1, 'type' => 1];
$query->join([$target->alias() => array_intersect_key($options, $joinOptions)]);
if (empty($options['fields'])) {
$f = isset($options['fields']) ? $options['fields'] : null;
if ($options['includeFields'] && ($f === null || $f !== false)) {
$options['fields'] = array_keys($target->schema());
}
}
if (!empty($options['fields'])) {
$query->select($query->aliasFields($options['fields'], $target->alias()));
}
}
}
@@ -0,0 +1,93 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\ORM\Association;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Table;
use Cake\ORM\Query;
/**
* Tests BelongsTo class
*
*/
class BelongsToTest extends \Cake\TestSuite\TestCase {
/**
* Set up
*
* @return void
*/
public function setUp() {
$this->company = Table::build('Company', [
'schema' => [
'id' => ['type' => 'integer'],
'company_name' => ['type' => 'string'],
]
]);
$this->client = Table::build('Client', [
'schema' => [
'id' => ['type' => 'integer'],
'client_name' => ['type' => 'string'],
'company_id' => ['type' => 'integer'],
]
]);
}
/**
* Tear down
*
* @return void
*/
public function tearDown() {
Table::clearRegistry();
}
/**
* Tests that the association reports it can be joined
*
* @return void
*/
public function testCanBeJoined() {
$assoc = new BelongsTo('Test');
$this->assertTrue($assoc->canBeJoined());
}
/**
* Tests that the correct join and fields are attached to a query depending on
* the association config
*
* @return void
*/
public function testAttachTo() {
$query = $this->getMock('\Cake\ORM\Query', ['join', 'select'], [null]);
$config = [
'foreignKey' => 'company_id',
'sourceTable' => $this->client,
'targetTable' => $this->company
];
$association = new BelongsTo('Company', $config);
$query->expects($this->once())->method('join')->with([
'Company' => ['conditions' => ['Company.id = Client.company_id']]
]);
$query->expects($this->once())->method('select')->with([
'Company__id' => 'Company.id',
'Company__company_name' => 'Company.company_name'
]);
$association->attachTo($query);
}
}

0 comments on commit af1d773

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