Saves relation between optimizer and search container

@param \Magento\Framework\Model\AbstractModel $object Optimizer to save

@return void
546
private function saveSearchContainerRelation(\Magento\Framework\Model\AbstractModel $object)
    {
        $searchContainers = $object->getSearchContainer();

        if (is_array($searchContainers) && (count($searchContainers) > 0)) {
            $searchContainerLinks = [];
            $deleteCondition = OptimizerInterface::OPTIMIZER_ID . " = " . $object->getId();

            foreach ($searchContainers as $searchContainer) {
                $searchContainerName = (string) $searchContainer;
                // Treat autocomplete apply_to like the quick search.
                if ($searchContainerName === 'catalog_product_autocomplete') {
                    $searchContainerName = 'quick_search_container';
                }
                $searchContainerData = $object->getData($searchContainerName);
                $applyTo = is_array($searchContainerData) ? ((bool) $searchContainerData['apply_to'] ?? false) : false;
                $searchContainerLinks[(string) $searchContainer] = [
                    OptimizerInterface::OPTIMIZER_ID     => (int) $object->getId(),
                    OptimizerInterface::SEARCH_CONTAINER => (string) $searchContainer,
                    'apply_to'                           => (int) $applyTo,
                ];
            }

            $this->getConnection()->delete($this->getTable(OptimizerInterface::TABLE_NAME_SEARCH_CONTAINER), $deleteCondition);
            $this->getConnection()->insertOnDuplicate(
                $this->getTable(OptimizerInterface::TABLE_NAME_SEARCH_CONTAINER),
                $searchContainerLinks
            );
        }
    }
871081
public function save($object)
    {
        if (method_exists($object, 'getContenttype')) {
            $entityName = $object->getContenttype();
        } else {
            $entityName = get_class($object);
        }
        $repo = $this->getRepository($entityName);

        return $repo->save($object);
    }
480904
public function setInstance($object, $abstract = null)
    {
        if (!is_object($object)) {
            throw new \InvalidArgumentException('$object must be an object');
        }
        if ($abstract === null) {
            //If $abstract is not specified, use $objects class
            $abstract = get_class($object);
        } elseif ($abstract[0] === '\\') {
            //Strip all leading backslashes
            $abstract = substr($abstract, 1);
        }
        $concrete = $this->findMostConcreteDefinition($abstract);
        if (isset($this->objects[$concrete])) {
            //If this class already has an instance, we'll return the old object
            $old = $this->objects[$concrete];
        } else {
            $old = false;
        }
        //Store the object
        $this->objects[$concrete] = $object;

        return $old;
    }
985978
protected function createModelAndSetDependencies(ModelFactoryInterface $modelFactory, $object)
    {
        $models = $this->createModelsAndSetDependencies($modelFactory, [$object]);

        return reset($models);
    }
1065917
public static function persist(IdentifiableObject $object) : void
    {
        self::repositoryFor(\get_class($object))->persist($object);
    }
1050242
public function persist(AbstractModel $model)
    {
        $collection = $this->getCollectionBy(
            $this->getModelConfiguration(get_class($model))
        );

        $this->setLastResult(
            $collection->insert($this->getModelDataAsArray($model))
        );

        return (bool) ($this->lastResult) ? $this->lastResult[self::RESULT_OK_INDEX] : false;
    }
5159
public function store(string $key, $object)
    {
        if (isset($this->aliases[$key])) {
            $key = $this->aliases[$key];
        }

        $this->store[$key] = $object;
    }
168130
public function save($object)
    {

        if (is_array($object)) {
            $objectData = $object;
            if ($object['id'] > 0) {
                $object = $this->find($object['id']);
            } else {
                $object = new Group();
            }
            $this->assignArrayToObject($object, $objectData);
        } else if (!($object instanceof GroupInterface)) {
            $this->addErrorMessage(self::ERROR_WRONG_OBJECT);
        }

        if (!$this->hasError()) {
            $check = $this->groupManager->update($object);
            if ($check === true) {
                $this->addSuccessMessage(self::SUCCESS_SAVED);
            }
        }

        return $object;
    }
198240
final public function deleteObject(Model $object)
    {
        if ($object->isNewRecord()) {
            return;
        }

        $this->onObjectDeleted($object);

        $this->deleteObjectFromCache($object);
    }
22705
public function persist($object, $flush = true)
    {
        if ($object->getId()) {
            $this->update($object, $flush);
        } else {
            $this->create($object, $flush);
        }
    }
819536
public function attach($object)
    {
        $this->checkType($object);

        if (gettype($object) !== 'object') {
            $msg = "You can only attach 'objects' into an AbstractCollection, '" . gettype($object) . "' given";
            throw new InvalidArgumentException($msg);
        } elseif (!$this->contains($object)) {
            $this->offsetSet(null, $object);
        }
    }
520637
public function fetchCacheData(AbstractModel $model)
    {
        $this->position = get_class($model);
        if (!isset($this->modelDataCache[$this->position])) {
            $this->modelDataCache[$this->position] = $this->prepareCacheData($model);
        }

        return $this->modelDataCache[$this->position];
    }
425003
public function save($object)
    {

        if (is_array($object)) {
            $objectData = $object;
            if ($object['id'] > 0) {
                $object = $this->find($object['id']);
            } else {
                $object = $this->createNewObject();
            }
            $this->assignArrayToObject($object, $objectData);
        } else if (!$this->isCorrectInstance($object)) {
            $this->addErrorMessage(self::ERROR_WRONG_OBJECT);
        }

        if (!$this->hasError()) {
            $check = $this->manager->save($object);
            if ($check) {
                $this->addSuccessMessage(self::SUCCESS_SAVED);
            }
        }

        return $object;
    }
1117275
public function afterSave($event)
    {
        foreach ($this->relationKey as $from => $to) {
            $this->_relation[$to] = $this->owner[$from];
        }
        $this->_relation->save();
    }
375880
protected function saveRelationsAfterHasOne($key)
    {
        /** @var \T4\Orm\Model $class */
        $class = get_class($this);
        $relation = $class::getRelations()[$key];
        $column = $class::getRelationLinkName($relation);

        /** @var \T4\Orm\Model $oldSubModel */
        $oldSubModel = $this->getRelationLazy($key);

        /** @var \T4\Orm\Model $newSubModel */
        $newSubModel = $this->{$key};

        if ( !empty($oldSubModel) && (empty($newSubModel) || $newSubModel->getPk() != $oldSubModel->getPk()) ) {
            $oldSubModel->{$column} = null;
            $oldSubModel->save();
        }

        if ( !empty($newSubModel) ) {
            $newSubModel->{$column} = $this->getPk();
            $newSubModel->save();
        }
    }
991224
public function save(entity $object = null)
    {

        $data = null;
        $primaryValue = array();
        if (null !== $object) {
            $primaryValue = array_filter(array_values($object->getPrimaryValue()), 'strlen');
        }

        if (null === $object || empty($primaryValue)) {
            $entity = $object;
            if (null === $object) {
                if ( null === $this->oosql_entity_obj) {
                    $msg = 'Nothing to save! ' . $this->sql();
                    throw new \Exception($msg, 9806, null);
                }
                // This is a brand new record let's insert;
                $entity = $this->getEntityObject();
            }

            $fields = (array)$entity;

            if ($identity = $entity->identity()) {
                unset($fields[$identity]);
            }
            $populated = array_filter($fields, 'strlen');

            $fieldnames = array_keys($populated);
            $fvalues = array_values($populated);

            call_user_func_array(array($this, 'insert'), $fieldnames);

            $lastID = call_user_func_array(array($this, 'values'), $fvalues)->exe();
            $entity->{$identity} = $lastID;

            return $entity;
        }

        if (isset(self::$oosql_result)) {
            // Updating after a select

            $primaryField = $object->getPrimary();

            $old = self::$oosql_result->objectWhere($primaryField[0], $object->{$primaryField[0]});

            // Is it really a modification of a selected row?
            if ($old) {

                $identity = $this->getEntityObject()->identity();
                foreach (array_diff_assoc((array)$object, (array)$old) as $key => $value) {
                    if ($key == $identity) {
                        continue;
                    }
                    $data[$key] = $value;
                }


                if (null === $data) {
                    $msg = 'Nothing to save! ' . $this->sql();
                    throw new \Exception($msg, 9807, null);
                }
                $this->update()->set($data)->createWhere($object->getPrimaryValue())->exe();

                return $object;

            }

        }
        // update a row just after inserting it
        // Or
        // update a related table (no select on it)
        $identity = $object->identity();

        foreach ((array)$object as $k => $v) {
            if ($v === null || $k == $identity) {
                continue;
            }
            $data[$k] = $v;
        }

        if (count($data) !== 0) {

            $this->update()->set($data)->createWhere($object->getPrimaryValue())->exe();

            return $object;
        }
        $msg = 'Nothing to save! ' . $this->sql();
        throw new \Exception($msg, 9808, null);
    }
1235362
private function insertObject(Model $object)
    {
        $schema = $this->reposSchema;
        $changes = $object->takeChangeSnapshot();

        $params = [];
        $columns = [];

        $sql = "INSERT INTO `{$schema->schemaName}`";

        $schemaColumns = $schema->getColumns();

        foreach ($changes as $columnName => $value) {
            $changeData = $changes;

            if (isset($schemaColumns[$columnName])) {
                $storageColumns = $schemaColumns[$columnName]->getStorageColumns();

                $transforms = $this->columnTransforms[$columnName];

                if ($transforms[1] !== null) {
                    $closure = $transforms[1];

                    $transformedData = $closure($changes);

                    if (is_array($transformedData)) {
                        $changeData = $transformedData;
                    } else {
                        $changeData[$columnName] = $transformedData;
                    }
                }

                foreach ($storageColumns as $storageColumnName => $storageColumn) {
                    $value = (isset($changeData[$storageColumnName])) ? $changeData[$storageColumnName] : null;

                    $columns[] = "`" . $storageColumnName . "` = :" . $storageColumnName;

                    if ($value === null) {
                        $value = $storageColumn->defaultValue;
                    }

                    $params[$storageColumnName] = $value;
                }
            }
        }

        if (sizeof($columns) > 0) {
            $sql .= " SET " . implode(", ", $columns);
        } else {
            $sql .= " VALUES ()";
        }

        $insertId = self::executeInsertStatement($sql, $params);

        if ($insertId > 0) {
            $object[$object->getUniqueIdentifierColumnName()] = $insertId;
        }
    }