Skip to content

Commit

Permalink
Merge pull request #1619 from ryuring/dev-#1596
Browse files Browse the repository at this point in the history
fix #1596 ブログ記事noが重複して保存される問題を改善
  • Loading branch information
gondoh committed Jan 23, 2021
2 parents e774999 + 71c75ab commit 7ab39f2
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 44 deletions.
31 changes: 16 additions & 15 deletions lib/Baser/Plugin/Blog/Config/Schema/blog_posts.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/* BlogPosts schema generated on: 2011-08-20 02:08:54 : 1313774094 */

class BlogPostsSchema extends CakeSchema
{

Expand Down Expand Up @@ -37,26 +35,29 @@ public function after($event = [])
}

public $blog_posts = [
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'key' => 'primary'],
'blog_content_id' => ['type' => 'integer', 'null' => true, 'length' => 8],
'no' => ['type' => 'integer', 'null' => true],
'name' => ['type' => 'string', 'null' => true, 'default' => null],
'content' => ['type' => 'text', 'null' => true, 'default' => null],
'detail' => ['type' => 'text', 'null' => true, 'default' => null],
'blog_category_id' => ['type' => 'integer', 'null' => true, 'default' => null, 'length' => 8],
'user_id' => ['type' => 'integer', 'null' => true, 'default' => null, 'length' => 8],
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'],
'blog_content_id' => ['type' => 'integer', 'null' => true, 'default' => null, 'length' => 8, 'unsigned' => false, 'key' => 'index'],
'no' => ['type' => 'integer', 'null' => true, 'default' => null, 'unsigned' => false],
'name' => ['type' => 'string', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'content' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'detail' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'blog_category_id' => ['type' => 'integer', 'null' => true, 'default' => null, 'length' => 8, 'unsigned' => false],
'user_id' => ['type' => 'integer', 'null' => true, 'default' => null, 'length' => 8, 'unsigned' => false],
'status' => ['type' => 'boolean', 'null' => true, 'default' => null],
'posts_date' => ['type' => 'datetime', 'null' => true, 'default' => null],
'content_draft' => ['type' => 'text', 'null' => true, 'default' => null],
'detail_draft' => ['type' => 'text', 'null' => true, 'default' => null],
'content_draft' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'detail_draft' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'publish_begin' => ['type' => 'datetime', 'null' => true, 'default' => null],
'publish_end' => ['type' => 'datetime', 'null' => true, 'default' => null],
'exclude_search' => ['type' => 'boolean', 'null' => true, 'default' => null],
'eye_catch' => ['type' => 'text', 'null' => true, 'default' => null],
'eye_catch' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'created' => ['type' => 'datetime', 'null' => true, 'default' => null],
'modified' => ['type' => 'datetime', 'null' => true, 'default' => null],
'indexes' => ['PRIMARY' => ['column' => 'id', 'unique' => 1]],
'tableParameters' => ['charset' => 'utf8', 'collate' => 'utf8_general_ci']
'indexes' => [
'PRIMARY' => ['column' => 'id', 'unique' => 1],
'blog_content_id_no_index' => ['column' => ['blog_content_id', 'no'], 'unique' => 1]
],
'tableParameters' => ['charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB']
];

}
45 changes: 26 additions & 19 deletions lib/Baser/Plugin/Blog/Controller/BlogPostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ public function admin_add($blogContentId)
}

$this->request->data['BlogPost']['blog_content_id'] = $blogContentId;
$this->request->data['BlogPost']['no'] = $this->BlogPost->getMax('no', ['BlogPost.blog_content_id' => $blogContentId]) + 1;
$this->request->data['BlogPost']['posts_date'] = str_replace('/', '-', $this->request->data['BlogPost']['posts_date']);

if (!BcUtil::isAdminUser()) {
Expand All @@ -315,24 +314,32 @@ public function admin_add($blogContentId)
}

// データを保存
if ($this->BlogPost->saveAll($this->request->data)) {
clearViewCache();
$id = $this->BlogPost->getLastInsertId();
$this->BcMessage->setSuccess(sprintf(__d('baser', '記事「%s」を追加しました。'), $this->request->data['BlogPost']['name']));

// 下のBlogPost::read()で、BlogTagデータ無しのキャッシュを作ってしまわないように
// recursiveを設定
$this->BlogPost->recursive = 1;

// EVENT BlogPosts.afterAdd
$this->dispatchEvent('afterAdd', [
'data' => $this->BlogPost->read(null, $id)
]);

// 編集画面にリダイレクト
$this->redirect(['action' => 'edit', $blogContentId, $id]);
} else {
$this->BcMessage->setError(__d('baser', 'エラーが発生しました。内容を確認してください。'));
try {
if ($this->BlogPost->saveAll($this->request->data)) {
clearViewCache();
$id = $this->BlogPost->getLastInsertId();
$this->BcMessage->setSuccess(sprintf(__d('baser', '記事「%s」を追加しました。'), $this->request->data['BlogPost']['name']));

// 下のBlogPost::read()で、BlogTagデータ無しのキャッシュを作ってしまわないように
// recursiveを設定
$this->BlogPost->recursive = 1;

// EVENT BlogPosts.afterAdd
$this->dispatchEvent('afterAdd', [
'data' => $this->BlogPost->read(null, $id)
]);

// 編集画面にリダイレクト
$this->redirect(['action' => 'edit', $blogContentId, $id]);
} else {
$this->BcMessage->setError(__d('baser', 'エラーが発生しました。内容を確認してください。'));
}
} catch (Exception $e) {
if($e->getCode() === "23000") {
$this->BcMessage->setError(__d('baser', '同時更新エラーです。しばらく経ってから保存してください。'));
} else {
$this->BcMessage->setError(__d('baser', 'データベース処理中にエラーが発生しました。'));
}
}
} else {
$this->request->data = $this->BlogPost->getDefaultValue($this->BcAuth->user());
Expand Down
16 changes: 15 additions & 1 deletion lib/Baser/Plugin/Blog/Model/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,20 @@ public function getPublishes($options)
return $datas;
}

/**
* After Validate
*/
public function afterValidate()
{
parent::afterValidate();
if(empty($this->data['BlogPost']['blog_content_id'])) {
throw new BcException('blog_content_id が指定されていません。');
}
if (empty($this->validationErrors) && empty($this->data['BlogPost']['id'])) {
$this->data['BlogPost']['no'] = $this->getMax('no', ['BlogPost.blog_content_id' => $this->data['BlogPost']['blog_content_id']]) + 1;
}
}

/**
* afterSave
*
Expand All @@ -537,7 +551,7 @@ public function getPublishes($options)
public function afterSave($created, $options = [])
{
// 検索用テーブルへの登録・削除
if ($this->searchIndexSaving && !$this->data['BlogPost']['exclude_search']) {
if ($this->searchIndexSaving && empty($this->data['BlogPost']['exclude_search'])) {
$this->saveSearchIndex($this->createSearchIndex($this->data));
} else {
if (!empty($this->data['BlogPost']['id'])) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Baser/Plugin/Blog/Test/Case/Model/BlogContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class BlogContentTest extends BaserTestCase
'baser.Default.Content',
'baser.Default.Site',
'baser.Default.User',
'baser.Default.UserGroup',
'baser.Default.Favorite'
];

public function setUp()
Expand Down
16 changes: 11 additions & 5 deletions lib/Baser/Plugin/Blog/Test/Case/Model/BlogPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function test必須チェック()
{

$this->BlogPost->create([
'BlogPost' => []
'BlogPost' => ['blog_content_id' => 1]
]);

$this->assertFalse($this->BlogPost->validates());
Expand All @@ -73,7 +73,8 @@ public function test空チェック()
{
$this->BlogPost->create([
'BlogPost' => [
'user_id' => ''
'user_id' => '',
'blog_content_id' => 1
]
]);

Expand All @@ -88,6 +89,7 @@ public function test桁数チェック異常系()
$this->BlogPost->create([
'BlogPost' => [
'name' => '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456',
'blog_content_id' => 1
]
]);

Expand All @@ -102,7 +104,8 @@ public function test桁数チェック正常系()
$this->BlogPost->create([
'BlogPost' => [
'name' => '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345',
'posts_date' => '2020-01-27 12:57:59'
'posts_date' => '2020-01-27 12:57:59',
'blog_content_id' => 1
]
]);
$this->assertTrue($this->BlogPost->validates());
Expand All @@ -116,6 +119,7 @@ public function testその他異常系()
'publish_begin' => 'test',
'publish_end' => 'test',
'posts_date' => 'test',
'blog_content_id' => 1
]
]);

Expand All @@ -142,6 +146,7 @@ public function testその他異常系()
'BlogPost' => [
'detail' => $bigData,
'detail_draft' => $bigData,
'blog_content_id' => 1
]
]);

Expand All @@ -162,6 +167,7 @@ public function testその他正常系()
'publish_begin' => '2020-01-27 12:57:59',
'publish_end' => '2020-01-29 12:57:59',
'posts_date' => '2020-01-27 12:57:59',
'blog_content_id' => 1
]
]);

Expand All @@ -175,6 +181,7 @@ public function testその他正常系()
'BlogPost' => [
'detail' => 'test',
'detail_draft' => 'test',
'blog_content_id' => 1
]
]);

Expand Down Expand Up @@ -397,14 +404,14 @@ public function testAfterSave()
{
$data = ['BlogPost' => [
'id' => 99,
'no' => 99,
'exclude_search' => 0,
'name' => 'test-name',
'blog_content_id' => 1,
'blog_category_id' => null,
'posts_date' => '2020-01-27 12:57:59',
'content' => 'test-content',
'detail' => 'test-detail',
'no' => 4,
'status' => 0,
'publish_begin' => '2020-01-27 12:57:59',
'publish_end' => '2020-01-28 12:57:59',
Expand Down Expand Up @@ -550,7 +557,6 @@ public function createSearchIndexPublishDataProvider()
];
}


/**
* beforeDelete
*/
Expand Down
10 changes: 8 additions & 2 deletions lib/Baser/Plugin/Blog/Test/Case/View/Helper/BlogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ public function testSetContent($blogContentId, $viewVars, $expected)
'name' => 'test',
]
]];
$View->request = $this->_getRequest('/');
$View->request->params['Content']['type'] = 'BlogContent';
$this->Blog = new BlogHelper($View);
}
$this->Blog->blogContent = null;
$this->Blog->setContent($blogContentId);
$this->assertEquals($this->Blog->blogContent['id'], $expected, 'ブログコンテンツデータを正しくセットできません');
$result = null;
if(!empty($this->Blog->blogContent['id'])) {
$result = $this->Blog->blogContent['id'];
}
$this->assertEquals($result, $expected, 'ブログコンテンツデータを正しくセットできません');
}

public function setContentDataProvider()
Expand Down Expand Up @@ -832,7 +838,7 @@ public function getTagLinkUrlDataProvider()
['/', 1, 'タグ1', '', false, '/news/archives/tag/タグ1'],
['/', 1, 'タグ1', '/sub', false, '/news/archives/tag/タグ1'],
['/', 1, 'タグ1', '/sub', true, '/sub/news/archives/tag/タグ1'],
['/s/', 3, 'タグ2', '', false, 'http://main.com/en/news/archives/tag/タグ2'],
['/en/', 3, 'タグ2', '', false, '/en/news/archives/tag/タグ2'],
['/', 4, 'タグ2', '', false, 'http://sub.main.com/news/archives/tag/タグ2'],
['/', null, 'タグ1', '', false, '/tags/タグ1'],
['/s/', null, 'タグ2', '', false, '/s/tags/タグ2']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BlogCategoryModelFixture extends BaserTestFixture
[
'id' => '3',
'blog_content_id' => '1',
'no' => '2',
'no' => '3',
'name' => 'child-no-parent',
'title' => '親子関係なしカテゴリ',
'status' => '1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class BlogPostModelFixture extends BaserTestFixture
*/
public $name = 'BlogPost';

/**
* BlogPostFixture constructor.
*/
public function __construct()
{
parent::__construct();
// PostgreSQLの場合、プライマリーキー以外のインデックスが設定されている場合
// テスト用のテーブルを作成する際に本体用のインデックスとの重複エラーとなってしまうため
// 別名のインデックス名として作成しなおす
include_once BASER_PLUGINS . 'Blog' . DS . 'Config' . DS . 'Schema' . DS . 'blog_posts.php';
$schema = new BlogPostsSchema();
$schema->tables['blog_posts']['indexes']['test_blog_content_id_no_index'] = $schema->tables['blog_posts']['indexes']['blog_content_id_no_index'];
unset($schema->tables['blog_posts']['indexes']['blog_content_id_no_index']);
$this->fields = $schema->tables['blog_posts'];
}

/**
* Records
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class BlogPostBlogTagFindCustomPramsFixture extends CakeTestFixture
*/
public $import = ['model' => 'Blog.BlogPost'];

/**
* BlogPostFixture constructor.
*/
public function __construct()
{
parent::__construct();
// PostgreSQLの場合、プライマリーキー以外のインデックスが設定されている場合
// テスト用のテーブルを作成する際に本体用のインデックスとの重複エラーとなってしまうため
// 別名のインデックス名として作成しなおす
include_once BASER_PLUGINS . 'Blog' . DS . 'Config' . DS . 'Schema' . DS . 'blog_posts.php';
$schema = new BlogPostsSchema();
$schema->tables['blog_posts']['indexes']['test_blog_content_id_no_index'] = $schema->tables['blog_posts']['indexes']['blog_content_id_no_index'];
unset($schema->tables['blog_posts']['indexes']['blog_content_id_no_index']);
$this->fields = $schema->tables['blog_posts'];
}

/**
* Records
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class BlogPostBlogBaserHelperFixture extends BaserTestFixture
*/
public $name = 'BlogPost';

/**
* BlogPostFixture constructor.
*/
public function __construct()
{
parent::__construct();
// PostgreSQLの場合、プライマリーキー以外のインデックスが設定されている場合
// テスト用のテーブルを作成する際に本体用のインデックスとの重複エラーとなってしまうため
// 別名のインデックス名として作成しなおす
include_once BASER_PLUGINS . 'Blog' . DS . 'Config' . DS . 'Schema' . DS . 'blog_posts.php';
$schema = new BlogPostsSchema();
$schema->tables['blog_posts']['indexes']['test_blog_content_id_no_index'] = $schema->tables['blog_posts']['indexes']['blog_content_id_no_index'];
unset($schema->tables['blog_posts']['indexes']['blog_content_id_no_index']);
$this->fields = $schema->tables['blog_posts'];
}

/**
* Records
*
Expand Down Expand Up @@ -60,7 +76,7 @@ class BlogPostBlogBaserHelperFixture extends BaserTestFixture
[
'id' => '3',
'blog_content_id' => '1',
'no' => '1',
'no' => '3',
'name' => 'name3',
'content' => 'content3',
'blog_category_id' => '3',
Expand Down
16 changes: 16 additions & 0 deletions lib/Baser/Test/Fixture/Default/BlogPostFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class BlogPostFixture extends BaserTestFixture
*/
public $name = 'BlogPost';

/**
* BlogPostFixture constructor.
*/
public function __construct()
{
parent::__construct();
// PostgreSQLの場合、プライマリーキー以外のインデックスが設定されている場合
// テスト用のテーブルを作成する際に本体用のインデックスとの重複エラーとなってしまうため
// 別名のインデックス名として作成しなおす
include_once BASER_PLUGINS . 'Blog' . DS . 'Config' . DS . 'Schema' . DS . 'blog_posts.php';
$schema = new BlogPostsSchema();
$schema->tables['blog_posts']['indexes']['test_blog_content_id_no_index'] = $schema->tables['blog_posts']['indexes']['blog_content_id_no_index'];
unset($schema->tables['blog_posts']['indexes']['blog_content_id_no_index']);
$this->fields = $schema->tables['blog_posts'];
}

/**
* Records
*
Expand Down

0 comments on commit 7ab39f2

Please sign in to comment.