Skip to content

Commit

Permalink
Fixes #4009
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 19, 2014
1 parent 48ca3de commit ce495e5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -420,11 +420,11 @@ public function saveAssociated(Entity $entity, array $options = []) {
* created if no errors happened, false otherwise * created if no errors happened, false otherwise
*/ */
protected function _saveTarget(Entity $parentEntity, $entities, $options) { protected function _saveTarget(Entity $parentEntity, $entities, $options) {
$associations = false; $joinAssociations = false;
if (!empty($options['associated'][$this->_junctionProperty]['associated'])) { if (!empty($options['associated'][$this->_junctionProperty]['associated'])) {
$associations = $options['associated'][$this->_junctionProperty]['associated']; $joinAssociations = $options['associated'][$this->_junctionProperty]['associated'];
} }
$options['associated'] = $associations; unset($options['associated'][$this->_junctionProperty]);


if (!(is_array($entities) || $entities instanceof \Traversable)) { if (!(is_array($entities) || $entities instanceof \Traversable)) {
$name = $this->property(); $name = $this->property();
Expand Down Expand Up @@ -457,6 +457,7 @@ protected function _saveTarget(Entity $parentEntity, $entities, $options) {
} }
} }


$options['associated'] = $joinAssociations;
$success = $this->_saveLinks($parentEntity, $persisted, $options); $success = $this->_saveLinks($parentEntity, $persisted, $options);
if (!$success && !empty($options['atomic'])) { if (!$success && !empty($options['atomic'])) {
$parentEntity->set($this->property(), $original); $parentEntity->set($this->property(), $original);
Expand Down
57 changes: 57 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -264,4 +264,61 @@ public function testSaveWithCallbacks() {
$this->assertSame($article, $articles->save($article)); $this->assertSame($article, $articles->save($article));
} }


/**
* Tests that whe saving deep associations for a belongsToMany property,
* data is not removed becuase of excesive associations filtering.
*
* @see https://github.com/cakephp/cakephp/issues/4009
* @return void
*/
public function testBelongsToManyDeepSave2() {
$articles = TableRegistry::get('Articles');
$articles->belongsToMany('Highlights', [
'className' => 'TestApp\Model\Table\TagsTable',
'foreignKey' => 'article_id',
'targetForeignKey' => 'tag_id',
'through' => 'SpecialTags',
]);
$articles->Highlights->hasMany('TopArticles', [
'className' => 'TestApp\Model\Table\ArticlesTable',
'foreignKey' => 'author_id',
]);
$entity = $articles->get(2, ['contain' => ['Highlights']]);

$data = [
'highlights' => [
[
'name' => 'New Special Tag',
'_joinData' => [
'highlighted' => true,
'highlighted_time' => '2014-06-01 10:10:00',
],
'top_articles' => [
['title' => 'First top article'],
['title' => 'Second top article'],
]
]
]
];
$options = [
'associated' => [
'Highlights._joinData', 'Highlights.TopArticles'
]
];
$entity = $articles->patchEntity($entity, $data, $options);
$articles->save($entity, $options);
$entity = $articles->get(2, [
'contain' => [
'Highlights.TopArticles'
]
]);
$highlights = $entity->highlights[0];
$this->assertEquals('First top article', $highlights->top_articles[0]->title);
$this->assertEquals('Second top article', $highlights->top_articles[1]->title);
$this->assertEquals(
new Time('2014-06-01 10:10:00'),
$highlights->_joinData->highlighted_time
);
}

} }

0 comments on commit ce495e5

Please sign in to comment.