Store an entity collection inside a single DB Transaction.

@param \Traversable|array $entities

@throws \InvalidArgumentException
@throws MappingException

@return \Traversable|array
481813
protected function storeCollection($entities)
    {
        $this->adapter->beginTransaction();

        foreach ($entities as $entity) {
            $this->storeEntity($entity);
        }

        $this->adapter->commit();

        return $entities;
    }
535189
private function sortEntities(array $entities, array $keys)
    {
        $map = [];
        foreach ($entities as $entity) {
            $map[(string) $entity->key()] = $entity;
        }

        $ret = [];
        foreach ($keys as $key) {
            if (isset($map[(string) $key])) {
                $ret[] = $map[(string) $key];
            }
        }

        return $ret;
    }
900303
protected function getModelKeysFromCollection(EntityCollection $entities)
    {
        $keyName = $this->relatedMap->getKeyName();

        return array_map(function ($m) use ($keyName) {
            return $m->$keyName;
        }, $entities);
    }
1275462
public function references($references)
    {
        if (null !== $references && !is_array($references)) {
            throw new \InvalidArgumentException(sprintf('The references "%s" are not valid.', $references));
        }

        $this->references = $references;

        return $this;
    }
260080
protected function setMappings(array $mappings = array())
    {
        foreach ($mappings as $mapping) {
            if (!$mapping instanceof ImsManifestMapping) {
                $msg = "The mappings argument must be an array composed of ImsManifestMapping objects";
                throw new InvalidArgumentException($msg);
            }
        }

        $this->mappings = $mappings;
    }
61879
public function entitiesFrom(IEntitySet $entities) : ObjectArrayFieldBuilder
    {
        return (new ObjectArrayFieldBuilder($this
            ->type(new ArrayOfObjectIdsType(
                $entities,
                Field::element()->entityFrom($entities)->required()->build(),
                $loadAsObjects = true
            ))
        ))->containsNoDuplicates();
    }
36629
public function setEntitiesAttribute($entities)
    {
        static::saved(function ($model) use ($entities) {
            $values = [];
            foreach ($entities as $entity) {
                $values[] = ['attribute_id' => $model->id, 'entity_type' => $entity];
            }

            DB::table(config('rinvex.attributable.tables.attribute_entity'))->where('attribute_id', $model->id)->delete();
            DB::table(config('rinvex.attributable.tables.attribute_entity'))->insert($values);
        });
    }
925063
public function update($entity)
    {
        if (!$entity instanceof $this->entityClass) {
            throw new \InvalidArgumentException(
                "The entity to be updated must be an instance of {$this->entityClass}."
            );
        }

        $entityId = $entity->id;
        $data = $entity->toArray();
        unset($data['id']);

        return $this->adapter->update($this->entityTable, $data, "id = $entityId");
    }
1175105
private function keyEntityArray(array $entities) : array
    {
        $keyedArray = [];

        foreach ($entities as $entity) {
            $keyedArray[$this->idAccessorRegistry->getEntityId($entity)] = $entity;
        }

        return $keyedArray;
    }
699764
protected function save($entities)
    {
        if (!is_array($entities)) {
            $entities = [$entities];
        }

        foreach ($entities as $entity) {
            $entityClass = get_class($entity);
            $entityManager = $this
                ->get('base.object_manager_provider')
                ->getObjectManagerByEntityNamespace($entityClass);
            $entityManager->persist($entity);
            $entityManager->flush($entity);
        }
    }
909161
protected function saveMany(Entity $entity)
    {
        $manyFields = $this->getManyFields();
        $id         = $entity->get('id');
        foreach ($manyFields as $manyField) {
            $thisField = '`'.$this->definition->getSubTypeField($manyField, 'many', 'thisField').'`';
            $thatField = '`'.$this->definition->getSubTypeField($manyField, 'many', 'thatField').'`';
            $this->database->delete($manyField, [$thisField => $id]);
            $manyValues = $entity->get($manyField) ?: [];
            foreach ($manyValues as $thatId) {
                $this->database->insert($manyField, [
                    $thisField => $id,
                    $thatField => $thatId['id']
                ]);
            }
        }
    }
1265438
public function entityIdsFrom(IEntitySet $entities) : ObjectArrayFieldBuilder
    {
        return (new ObjectArrayFieldBuilder($this
            ->type(new ArrayOfObjectIdsType(
                $entities,
                Field::element()->entityIdFrom($entities)->required()->build()
            ))
        ))->containsNoDuplicates();
    }
514334
public function addEntity(EntityInterface $entity)
    {
        if (!is_a($entity, $this->type)) {
            throw new InvalidArgumentException(
                "Trying to add an entity that is not a {$this->type}."
            );
        }
        $this->data[] = $entity;
        $event = new EntityAdded($entity, ['collection' => $this]);
        $this->getEmitter()->emit($event);
        return $this;
    }
574482
public function delete($entities)
    {
        if($entities instanceof Entity) {
            $entities = [$entities];
        }
        return $this->obj_gateway
            ->withSchema($this->obj_schema)
            ->withTransaction($this->consumeTransaction())
            ->deleteMulti($entities);
    }
337551
private function encapsulate($entities)
	{
		$items = [];

		foreach ($entities as $entity)
		{
			$items[] = Entity::fromMetadata($entity, $this);
		}

		return new Collection($items);
	}
27136
protected function cacheForgetByKeys($entity = null)
    {
        $keys = $this->keysToForgetCache;

        if (is_array($keys) and array_diff_key($keys, array_keys(array_keys($keys)))) {
            foreach ($keys as $key => $method) {
                cache()->forget($key);
                if (is_subclass_of($entity, ParentModel::class)) {
                    if (method_exists($model = $entity->getModel(), $method)) {
                        $model->$method();
                    }
                }
            }
        }
    }
855389
public static function collectIds($entities)
	{
		$ids = array();

		foreach ($entities as $entity) {
			$ids[] = $entity->getId();
		}

		return $ids;
	}