Skip to content
Permalink
Browse files

Fixes #4009

  • Loading branch information...
lorenzo committed Jul 19, 2014
1 parent 48ca3de commit ce495e52638541924cb37b8e9f73b13c3c7b1963
Showing with 61 additions and 3 deletions.
  1. +4 −3 src/ORM/Association/BelongsToMany.php
  2. +57 −0 tests/TestCase/ORM/QueryRegressionTest.php
@@ -420,11 +420,11 @@ public function saveAssociated(Entity $entity, array $options = []) {
* created if no errors happened, false otherwise
*/
protected function _saveTarget(Entity $parentEntity, $entities, $options) {
$associations = false;
$joinAssociations = false;
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)) {
$name = $this->property();
@@ -457,6 +457,7 @@ protected function _saveTarget(Entity $parentEntity, $entities, $options) {
}
}
$options['associated'] = $joinAssociations;
$success = $this->_saveLinks($parentEntity, $persisted, $options);
if (!$success && !empty($options['atomic'])) {
$parentEntity->set($this->property(), $original);
@@ -264,4 +264,61 @@ public function testSaveWithCallbacks() {
$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.
You can’t perform that action at this time.