Skip to content

Commit

Permalink
Migrating HasOne to use the new table name convention
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 8, 2013
1 parent 3c1de52 commit d836e0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Cake/ORM/Association/HasOne.php
Expand Up @@ -56,7 +56,8 @@ class HasOne extends Association {
public function foreignKey($key = null) {
if ($key === null) {
if ($this->_foreignKey === null) {
$this->_foreignKey = Inflector::tableize($this->source()->alias()) . '_id';
$key = Inflector::singularize($this->source()->alias());
$this->_foreignKey = Inflector::underscore($key) . '_id';
}
return $this->_foreignKey;
}
Expand Down
46 changes: 23 additions & 23 deletions Cake/Test/TestCase/ORM/Association/HasOneTest.php
Expand Up @@ -35,13 +35,13 @@ class HasOneTest extends \Cake\TestSuite\TestCase {
*/
public function setUp() {
parent::setUp();
$this->user = TableRegistry::get('User', [
$this->user = TableRegistry::get('Users', [
'schema' => [
'id' => ['type' => 'integer'],
'username' => ['type' => 'string'],
]
]);
$this->profile = TableRegistry::get('Profile', [
$this->profile = TableRegistry::get('Profiles', [
'schema' => [
'id' => ['type' => 'integer'],
'first_name' => ['type' => 'string'],
Expand Down Expand Up @@ -82,24 +82,24 @@ public function testAttachTo() {
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
'targetTable' => $this->profile,
'conditions' => ['Profile.is_active' => true]
'conditions' => ['Profiles.is_active' => true]
];
$association = new HasOne('Profile', $config);
$field = new IdentifierExpression('Profile.user_id');
$association = new HasOne('Profiles', $config);
$field = new IdentifierExpression('Profiles.user_id');
$query->expects($this->once())->method('join')->with([
'Profile' => [
'Profiles' => [
'conditions' => [
'Profile.is_active' => true,
['User.id' => $field],
'Profiles.is_active' => true,
['Users.id' => $field],
],
'type' => 'INNER',
'table' => 'profiles'
]
]);
$query->expects($this->once())->method('select')->with([
'Profile__id' => 'Profile.id',
'Profile__first_name' => 'Profile.first_name',
'Profile__user_id' => 'Profile.user_id'
'Profiles__id' => 'Profiles.id',
'Profiles__first_name' => 'Profiles.first_name',
'Profiles__user_id' => 'Profiles.user_id'
]);
$association->attachTo($query);
}
Expand All @@ -115,24 +115,24 @@ public function testAttachToConfigOverride() {
'foreignKey' => 'user_id',
'sourceTable' => $this->user,
'targetTable' => $this->profile,
'conditions' => ['Profile.is_active' => true]
'conditions' => ['Profiles.is_active' => true]
];
$association = new HasOne('Profile', $config);
$association = new HasOne('Profiles', $config);
$query->expects($this->once())->method('join')->with([
'Profile' => [
'Profiles' => [
'conditions' => [
'Profile.is_active' => false
'Profiles.is_active' => false
],
'type' => 'INNER',
'table' => 'profiles'
]
]);
$query->expects($this->once())->method('select')->with([
'Profile__first_name' => 'Profile.first_name'
'Profiles__first_name' => 'Profiles.first_name'
]);

$override = [
'conditions' => ['Profile.is_active' => false],
'conditions' => ['Profiles.is_active' => false],
'foreignKey' => false,
'fields' => ['first_name']
];
Expand All @@ -149,15 +149,15 @@ public function testAttachToNoFields() {
$config = [
'sourceTable' => $this->user,
'targetTable' => $this->profile,
'conditions' => ['Profile.is_active' => true]
'conditions' => ['Profiles.is_active' => true]
];
$association = new HasOne('Profile', $config);
$field = new IdentifierExpression('Profile.user_id');
$association = new HasOne('Profiles', $config);
$field = new IdentifierExpression('Profiles.user_id');
$query->expects($this->once())->method('join')->with([
'Profile' => [
'Profiles' => [
'conditions' => [
'Profile.is_active' => true,
['User.id' => $field],
'Profiles.is_active' => true,
['Users.id' => $field],
],
'type' => 'INNER',
'table' => 'profiles'
Expand Down

0 comments on commit d836e0c

Please sign in to comment.