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>)[]
)
| 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, |