From eb822f2d06ab2b8bab93aa0f1027fffae0120629 Mon Sep 17 00:00:00 2001 From: kteraguchi Date: Thu, 15 Oct 2015 10:20:24 +0900 Subject: [PATCH] Modify name to alias --- Model/Behavior/TagBehavior.php | 14 +++++++------- Test/Case/Model/Behavior/TagBehaviorTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Model/Behavior/TagBehavior.php b/Model/Behavior/TagBehavior.php index b46e7a7..d97d15c 100644 --- a/Model/Behavior/TagBehavior.php +++ b/Model/Behavior/TagBehavior.php @@ -55,11 +55,11 @@ public function setup(Model $Model, $settings = array()) { */ public function afterSave(Model $Model, $created, $options = array()) { if ($created) { - $blockId = $Model->data[$Model->name]['block_id']; + $blockId = $Model->data[$Model->alias]['block_id']; if (isset($Model->data['Tag'])) { $Tag = $this->_getTagModel(); - if (!$Tag->saveTags($blockId, $Model->name, $Model->id, $Model->data['Tag'])) { + if (!$Tag->saveTags($blockId, $Model->alias, $Model->id, $Model->data['Tag'])) { throw new InternalErrorException(__d('net_commons', 'Internal Server Error')); } } @@ -119,7 +119,7 @@ public function beforeFind(Model $Model, $query) { 'table' => 'tags_contents', 'alias' => 'TagsContent', 'conditions' => - '`' . $Model->name . '`.`id`=`TagsContent`.`content_id` AND model = \'' . $Model->name . '\'', + '`' . $Model->alias . '`.`id`=`TagsContent`.`content_id` AND model = \'' . $Model->alias . '\'', ); } @@ -135,7 +135,7 @@ public function beforeFind(Model $Model, $query) { if ($joinLinkTable || $joinsTagTable) { // コンテンツIDでgroup byする - $query['group'][] = $Model->name . '.id'; + $query['group'][] = $Model->alias . '.id'; } return $query; @@ -152,9 +152,9 @@ public function beforeFind(Model $Model, $query) { */ public function afterFind(Model $Model, $results, $primary = false) { foreach ($results as $key => $target) { - if (isset($target[$Model->name]['id'])) { + if (isset($target[$Model->alias]['id'])) { $Tag = $this->_getTagModel(); - $tags = $Tag->getTagsByContentId($Model->name, $target[$Model->name]['id']); + $tags = $Tag->getTagsByContentId($Model->alias, $target[$Model->alias]['id']); foreach ($tags as $tag) { $target['Tag'][] = $tag['Tag']; } @@ -187,7 +187,7 @@ public function beforeDelete(Model $Model, $cascade = true) { * @return void */ public function afterDelete(Model $Model) { - $blockId = $this->_deleteTargetData[$Model->name]['block_id']; + $blockId = $this->_deleteTargetData[$Model->alias]['block_id']; $Tag = $this->_getTagModel(); $Tag->cleanup($Model, $blockId); } diff --git a/Test/Case/Model/Behavior/TagBehaviorTest.php b/Test/Case/Model/Behavior/TagBehaviorTest.php index 81a7377..aab4790 100644 --- a/Test/Case/Model/Behavior/TagBehaviorTest.php +++ b/Test/Case/Model/Behavior/TagBehaviorTest.php @@ -162,4 +162,18 @@ public function testAfterFind() { $fake = $FakeModel->findById(1); $this->assertEquals(count($fake['Tag']), 2); } + +/** + * モデルの別名使用テスト + * + * @return void + */ + public function testGetTagUsedAlias() { + $FakeModel = ClassRegistry::init(array('class' => 'FakeModel', 'alias' => 'FakeModelAlias')); + $this->_unloadTrackable($FakeModel); + + $conditions = array('Tag.id' => 1); + $result = $FakeModel->find('all', array('conditions' => $conditions)); + $this->assertInternalType('array', $result); + } }