Skip to content

Commit 730a80f

Browse files
committed
Adding fix and test for #3626
The code was assuming that if oyu passed _joinData to the save method that belonged to something already in the database, which was not all of the time. Removing the assumption, but also making saving a long list of associaitions a bit slower as it has to check the existance of the row first.
1 parent 6e363d3 commit 730a80f

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

src/ORM/Association/BelongsToMany.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ protected function _collectJointEntities($sourceEntity, $targetEntities) {
822822
continue;
823823
}
824824

825-
$joint->isNew(false);
826825
$result[] = $joint;
827826
}
828827

tests/Fixture/SpecialTagFixture.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
12+
* @since 3.0.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace Cake\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* A fixture for a join table containing additional data
21+
*
22+
*/
23+
class SpecialTagFixture extends TestFixture {
24+
25+
/**
26+
* fields property
27+
*
28+
* @var array
29+
*/
30+
public $fields = array(
31+
'id' => ['type' => 'integer'],
32+
'article_id' => ['type' => 'integer', 'null' => false],
33+
'tag_id' => ['type' => 'integer', 'null' => false],
34+
'highlighted' => ['type' => 'boolean', 'null' => true],
35+
'highlighted_time' => ['type' => 'timestamp', 'null' => true],
36+
'_constraints' => [
37+
'primary' => ['type' => 'primary', 'columns' => ['id']],
38+
'UNIQUE_TAG2' => ['type' => 'unique', 'columns' => ['article_id', 'tag_id']]
39+
]
40+
);
41+
42+
/**
43+
* records property
44+
*
45+
* @var array
46+
*/
47+
public $records = array(
48+
array('article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null),
49+
array('article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00')
50+
);
51+
}
52+

tests/TestCase/ORM/QueryRegressionTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class QueryRegressionTest extends TestCase {
3131
*
3232
* @var array
3333
*/
34-
public $fixtures = ['core.user', 'core.article', 'core.tag', 'core.articles_tag', 'core.author'];
34+
public $fixtures = [
35+
'core.user',
36+
'core.article',
37+
'core.tag',
38+
'core.articles_tag',
39+
'core.author',
40+
'core.special_tag'
41+
];
3542

3643
/**
3744
* Tear down
@@ -119,4 +126,36 @@ public function testNullableTimeColumn() {
119126
$this->assertNull($entity->created);
120127
}
121128

129+
130+
/**
131+
* Test for https://github.com/cakephp/cakephp/issues/3626
132+
*
133+
* Checks that join data is actually created and not tried to be updated every time
134+
* @return void
135+
*/
136+
public function testCreateJointData() {
137+
$articles = TableRegistry::get('Articles');
138+
$articles->belongsToMany('Highligths', [
139+
'className' => 'TestApp\Model\Table\TagsTable',
140+
'foreignKey' => 'article_id',
141+
'targetForeignKey' => 'tag_id',
142+
'through' => 'SpecialTags'
143+
]);
144+
$entity = $articles->get(2);
145+
$data = [
146+
'id' => 2,
147+
'highligths' => [
148+
[
149+
'name' => 'New Special Tag',
150+
'_joinData' => ['highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00']
151+
]
152+
]
153+
];
154+
$entity = $articles->patchEntity($entity, $data, ['Highligths' => ['associated' => ['_joinData']]]);
155+
$articles->save($entity);
156+
$entity = $articles->get(2, ['contain' => ['Highligths']]);
157+
$this->assertEquals(4, $entity->highligths[0]->_joinData->tag_id);
158+
$this->assertEquals('2014-06-01', $entity->highligths[0]->_joinData->highlighted_time->format('Y-m-d'));
159+
}
160+
122161
}

0 commit comments

Comments
 (0)