MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / add

Method add

packages/core/src/entity/Collection.ts:236–266  ·  view source on GitHub ↗

Adds one or more items to the collection, propagating the change to the inverse side. Returns the number of items added.

(
    entity: TT | Reference<TT> | Iterable<TT | Reference<TT>>,
    ...entities: (T | Reference<T>)[]
  )

Source from the content-addressed store, hash-verified

234
235 /** Adds one or more items to the collection, propagating the change to the inverse side. Returns the number of items added. */
236 add<TT extends T>(
237 entity: TT | Reference<TT> | Iterable<TT | Reference<TT>>,
238 ...entities: (T | Reference<T>)[]
239 ): number {
240 entities = (Utils.asArray(entity) as (T | Reference<T>)[]).concat(entities);
241 const unwrapped = entities.map(i => Reference.unwrapReference(i)) as T[];
242 this.validateModification(unwrapped);
243 const em = this.getEntityManager(entities as T[], false);
244 let added = 0;
245
246 for (const item of entities) {
247 const entity = Reference.unwrapReference(item) as T;
248
249 if (!this.contains(entity, false)) {
250 this.incrementCount(1);
251 this[this.#items.size] = entity;
252 this.#items.add(entity);
253 added++;
254 this.#dirty = true;
255 this.propagate(entity, 'add');
256 }
257 }
258
259 if (this.property.kind === ReferenceKind.ONE_TO_MANY && em) {
260 em.persist(entities);
261 }
262
263 this.cancelOrphanRemoval(unwrapped);
264
265 return added;
266 }
267
268 /**
269 * Remove specified item(s) from the collection. Note that removing item from collection does not necessarily imply deleting the target entity,

Callers 15

initMethod · 0.95
setMethod · 0.95
hydrateMethod · 0.95
nativeInsertManyFunction · 0.80
nativeUpdateManyFunction · 0.80
getAllChecksMethod · 0.80
acquireConnectionMethod · 0.80
processOnCreateHooksMethod · 0.80
processOnUpdateHooksMethod · 0.80
selectMethod · 0.80
countMethod · 0.80

Calls 10

validateModificationMethod · 0.95
getEntityManagerMethod · 0.95
containsMethod · 0.95
incrementCountMethod · 0.95
propagateMethod · 0.95
cancelOrphanRemovalMethod · 0.95
asArrayMethod · 0.80
unwrapReferenceMethod · 0.80
persistMethod · 0.65
mapMethod · 0.45

Tested by 15

createBooksWithTagsFunction · 0.64
createBooksWithTagsFunction · 0.64
constructorMethod · 0.64
createTestDataFunction · 0.64
constructorMethod · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
createEntitiesFunction · 0.64
populateDatabaseFunction · 0.64
createBooksWithTagsFunction · 0.64