mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed issue when many-to-many relation were not removed from the object
This commit is contained in:
parent
2e99b1a4b8
commit
2157575af9
@ -27,4 +27,5 @@ usages.
|
||||
* check self referencing
|
||||
* class lifecycle callbacks?
|
||||
* array / json / date column types
|
||||
* exceptions everywhere!
|
||||
* exceptions everywhere!
|
||||
* added ability to load only ids of the relation (similar to loading only single id)
|
||||
@ -29,7 +29,6 @@ export class PersistOperationExecutor {
|
||||
*/
|
||||
executePersistOperation(persistOperation: PersistOperation) {
|
||||
const broadcaster = new OrmBroadcaster(this.connection);
|
||||
// persistOperation.log();
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => this.broadcastBeforeEvents(broadcaster, persistOperation))
|
||||
|
||||
@ -23,7 +23,7 @@ export class PlainObjectToDatabaseEntityTransformer<Entity> {
|
||||
return null;
|
||||
|
||||
const alias = queryBuilder.aliasMap.mainAlias.name;
|
||||
const needToLoad = this.buildLoadMap(object, metadata);
|
||||
const needToLoad = this.buildLoadMap(object, metadata, true);
|
||||
|
||||
this.join(queryBuilder, needToLoad, alias);
|
||||
return queryBuilder
|
||||
@ -45,10 +45,16 @@ export class PlainObjectToDatabaseEntityTransformer<Entity> {
|
||||
});
|
||||
}
|
||||
|
||||
private buildLoadMap(object: any, metadata: EntityMetadata): LoadMap[] {
|
||||
private buildLoadMap(object: any, metadata: EntityMetadata, isFirstLevelDepth = false): LoadMap[] {
|
||||
// todo: rething the way we are trying to load things using left joins cause there are situations when same
|
||||
// todo: entities are loaded multiple times and become different objects (problem with duplicate entities in dbEntities)
|
||||
return metadata.relations
|
||||
.filter(relation => object.hasOwnProperty(relation.propertyName))
|
||||
.filter(relation => !(object[relation.propertyName] instanceof Array) || object[relation.propertyName].length > 0) // this is very important check that prevents building additional query for empty relations
|
||||
.filter(relation => {
|
||||
// we only need to load empty relations for first-level depth objects, otherwise removal can break
|
||||
// this is not reliable, refactor this part later
|
||||
return isFirstLevelDepth || !(object[relation.propertyName] instanceof Array) || object[relation.propertyName].length > 0;
|
||||
})
|
||||
.map(relation => {
|
||||
let value = object[relation.propertyName];
|
||||
if (value instanceof Array)
|
||||
|
||||
@ -271,16 +271,12 @@ describe("many-to-many", function() {
|
||||
.where("p.id=:id", { id: savedPost.id })
|
||||
.getSingleResult()
|
||||
.then(loadedPost => {
|
||||
// console.log("loaded post: ", loadedPost);
|
||||
loadedPost.categories.splice(0, 1);
|
||||
// loadedPost.categories.push(new PostCategory("blbla"));
|
||||
return postRepository.persist(loadedPost);
|
||||
}).then(updatedPost => {
|
||||
console.log("updated post: ", updatedPost);
|
||||
return postCategoryRepository.find({ name : "technology" });
|
||||
}).then(foundCategory => {
|
||||
console.log("found category: ", foundCategory);
|
||||
// todo: expect(foundCategory).to.be.empty;
|
||||
expect(foundCategory).to.be.empty;
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user