From d836e0c8a93d7055569d9f975f1246e6af0b7502 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 8 Nov 2013 22:43:08 +0100 Subject: [PATCH] Migrating HasOne to use the new table name convention --- Cake/ORM/Association/HasOne.php | 3 +- .../TestCase/ORM/Association/HasOneTest.php | 46 +++++++++---------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Cake/ORM/Association/HasOne.php b/Cake/ORM/Association/HasOne.php index b5bc7fe2617..267b7bb2563 100644 --- a/Cake/ORM/Association/HasOne.php +++ b/Cake/ORM/Association/HasOne.php @@ -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; } diff --git a/Cake/Test/TestCase/ORM/Association/HasOneTest.php b/Cake/Test/TestCase/ORM/Association/HasOneTest.php index f8de8917bf1..e229915f0bb 100644 --- a/Cake/Test/TestCase/ORM/Association/HasOneTest.php +++ b/Cake/Test/TestCase/ORM/Association/HasOneTest.php @@ -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'], @@ -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); } @@ -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'] ]; @@ -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'