Process paste the collection of models into the a model.

@param CollectionInterface $models The collection of models.
@param ModelIdInterface    $into   The paste into model.

@return void
430718
private function processPasteInto(CollectionInterface $models, ModelIdInterface $into = null)
    {
        if ($into && $models->count() && $into->getId()) {
            $manualSorting = ViewHelpers::getManualSortingProperty($this->getEnvironment());

            $this->pasteInto($this->modelCollector->getModel($into), $models, $manualSorting);

            $this->triggerPostPasteModel($models);
            $this->clearModelCollection($models);
        }
    }
1018478
protected function prepare($models)
    {
        return !is_null($models) ?
        ((Collection::count($models) > 0)
            ? ((Collection::count($models) == 1)
                ? $models[0]
                : $models
            )
            : null
        )
        : null;
    }
898759
protected function buildDictionary(Collection $models)
    {
        foreach ($models as $model) {
            $morphType = $this->morphType;

            if (isset($model->{$morphType})) {
                $type = $model->{$morphType};

                $key = $model->getAttribute($this->foreignKey);

                $this->dictionary[$type][$key][] = $model;
            }
        }
    }
873396
private function addModelToArraySimpleMode($models, $model)
    {
        $apply_to_models = array();
        
        if (count($models) > 0) {
            foreach ($models as $item) {
                $object = $model->find($item);
                if ($object != null) {
                    $apply_to_models[] = $object;
                }
            }
        }
        
        return $apply_to_models;
    }
399510
public function copy(EnvironmentInterface $environment, ModelIdInterface $modelId)
    {
        $this->guardNotEditOnly($environment->getDataDefinition(), $modelId);
        $this->guardIsCreatable($environment, $modelId);

        $dataProvider = $environment->getDataProvider();
        $model        = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));

        // We need to keep the original data here.
        $copyModel = $environment->getController()->createClonedModel($model);

        $eventDispatcher = $environment->getEventDispatcher();
        // Dispatch pre duplicate event.
        $preCopyEvent = new PreDuplicateModelEvent($environment, $copyModel, $model);
        $eventDispatcher->dispatch($preCopyEvent::NAME, $preCopyEvent);

        // Save the copy.
        $environment->getDataProvider($copyModel->getProviderName())->save($copyModel);

        // Dispatch post duplicate event.
        $postCopyEvent = new PostDuplicateModelEvent($environment, $copyModel, $model);
        $eventDispatcher->dispatch($postCopyEvent::NAME, $postCopyEvent);

        return $copyModel;
    }
622645
public function insertModels(Models $models)
    {
        foreach ($models as $model) {
            $this->dispatchBeforeEvent($model, Event::INSERT);
            $this->dispatchBeforeEvent($model, Event::SAVE);
        }

        $this->insertAll()->executeModels($models);

        foreach ($models as $model) {
            $model
                ->resetOriginals()
                ->setState(State::SAVED);

            $this->dispatchAfterEvent($model, Event::INSERT);
            $this->dispatchAfterEvent($model, Event::SAVE);
        }

        return $this;
    }
872906
protected function run($models)
    {
        $models = collect($models);
        if ($models->count() > 0) {
            $model = $models->first();

            if (!is_object($model)) {
                return;
            }

            if (!$this->isCascadable($model)) {
                return;
            }

            if (!in_array($model->getConnectionName(), $this->dbsToTransact)) {
                $this->dbsToTransact[] = $model->getConnectionName();
                DB::connection($model->getConnectionName())->beginTransaction();
            }

            $this->relations($model, $models->pluck($model->getKeyName()));
        }
    }
1005422
public function update($models)
    {
        if (! $this->shouldMaintainIndex($models->first())) {
            return;
        }

        foreach ($models as $model) {
            $this->performUpdate($model);
        }
    }
931625
public function merge(ModelInformationInterface $with)
    {
        if ( ! empty($with->model)) {
            $this->model = $with->model;
        }

        if ( ! empty($with->original_model)) {
            $this->original_model = $with->original_model;
        }

        $mergeAttributes = [
            'single',
            'meta',
            'verbose_name',
            'translated_name',
            'verbose_name_plural',
            'translated_name_plural',
            'allow_delete',
            'delete_condition',
            'delete_strategy',
            'confirm_delete',
            'list',
            'form',
            'show',
            'export',
            'reference',
            'includes',
        ];

        foreach ($mergeAttributes as $attribute) {
            $this->mergeAttribute($attribute, $with->{$attribute});
        }
    }
667120
protected function setDataFromModel(ModelInterface $model): void
    {
        $this->container->set('model', $model);
        
        foreach (get_class_methods($model) as $method) {
            if (0 !== strpos($method, 'get')) {
                continue;
            }
            
            $this->data[strtolower(substr($method, 3))] = $model->$method();
        }
    }
884259
public function insertMany(array $models, array $options = [])
    {
        $data = [];
        foreach ($models as $key => $model) {
            $data[$key] = get_values($model, false);
        }

        $result = $this->collection->insertMany($data, $options);
        if (false == $result->isAcknowledged()) {
            throw new \LogicException('Operation is not acknowledged');
        }

        foreach ($result->getInsertedIds() as $key => $modelId) {
            $this->hydrator->hydrate($data[$key], $models[$key]);
            set_object_id($models[$key], $modelId);

            $this->changesCollector->register($models[$key], $data[$key]);
        }

        return $result;
    }
1103546
protected function assignIndexes($indexes)
    {
        foreach ($indexes as $index) {
            if (!$index instanceof IndexInterface) {
                throw new ModelException(sprintf('Index must be an instance of IndexInterface, got "%s"', $this->getType($index)));
            }

            foreach ($index->fields() as $key => $field) {
                $field = $index->type() == 'foreign' ? $key : $field;

                $this->assertField($field);
            }

            if ($index->type() !== 'foreign') {
                $index->table($this->table);
            }

            $this->indexes[$index->name()] = $index;
        }
    }
631598
public function batchLoadFor($models, $subWiths) {
        $ids = [];
        foreach ($models as $model) {
            $id = $model->properties[$this->prop];
            if (is_string($id)) {
                $ids[] = "'$id'";
            } elseif (is_int($id)) {
                $ids[] = $id;
            } elseif ($id !== null) {
                X::logger()->warn('Unsupported reference type ' . gettype($id)
                    . ' of ' . get_class($model) . '->' . $this->prop
                    . ' (PK: ' . $model->pk . ')'
                );
            }
        }
        if (count($ids) === 0) {
            return;
        }
        $idStr = implode(',', $ids);
        /** @var Model[] $foreignModels */
        $foreignModels = $this->model
            ->with(...$subWiths)
            ->many("`{$this->model->pkName}` IN ($idStr)");
        $map = [];
        foreach ($foreignModels as $foreignModel) {
            $map[$foreignModel->pk] = $foreignModel;
        }
        foreach ($models as $model) {
            $model->{$this->name} = $map[$model->properties[$this->prop]] ?? null;
        }
    }
21519
public function handleModels(array $models)
    {
        $builder = $this->builder;

        $builder->start();

        foreach ($models as $model) {
            $languages = (array)(isset($model->sitemapLanguages) ? $model->sitemapLanguages : Yii::$app->language);

            if (count($languages) > 1) {
                // Handle separately for each language
                foreach ($languages as $language) {
                    // Swith App language to $language
                    if (isset($model->sitemapSwithLanguages) && $model->sitemapSwithLanguages) {
                        static::setLanguage($language);
                    }

                    $this->handleModel($model, $language);

                    // Restore App language
                    static::restoreLanguage();
                }
            } else {
                // Handle only one language
                $this->handleModel($model, null);
            }
        }

        $builder->finish();
    }
242596
protected function _includeCollection(ModelEntityInterface $entities)
    {
        $result = array('data' => array());

        foreach ($entities as $entity)
        {
            $relation = $this->_includeResource($entity);
            $result['data'][] = $relation['data'];
        }

        return $result;
    }
18476
public function apply(Builder $builder, Model $model)
    {
        if ($model->getTypeKey())
        {
            $builder->where(
                $model->getTypeKeyName(),
                $model->getTypeKey()
            );
        }
    }
1369146
protected function createModelsAndSetDependencies(ModelFactoryInterface $modelFactory, $objects)
    {
        $models = $modelFactory->createModels($objects);
        foreach ($models as $model) {
            if ($model instanceof ModelFactoryCollectionAwareModelInterface) {
                $model->setModelFactoryCollection($this);
            }
        }

        return $models;
    }