Modify entity

@param \Cake\Datasource\EntityInterface $entity Entity
@param \Cake\ORM\Table|\Cake\ORM\Association $object Table or association instance.
@return void
424662
protected function _modifyEntity(EntityInterface $entity, $object)
    {
        // belongs to many is tricky
        if ($object instanceof BelongsToMany) {
            unset($entity->_joinData);
        } else {
            // unset primary key
            unset($entity->{$object->getPrimaryKey()});

            // unset foreign key
            if ($object instanceof Association) {
                unset($entity->{$object->getPrimaryKey()});
            }
        }

        // set translations as new
        if (!empty($entity->_translations)) {
            foreach ($entity->_translations as $translation) {
                $translation->isNew(true);
            }
        }

        // set as new
        $entity->isNew(true);
    }
123954
protected function updateEntity(Entity $entity)
    {
        $action = \Schema::hasTable($entity->getTable()) ? 'update' : 'create';

        $this->migrations[$action][$entity->getTable()]['cols'] = $this->buildTable($entity);

        if($action == 'create' && $entity->timestamps) $this->migrations[$action][$entity->getTable()]['cols'][] = '$table->timestamps();';
    }
858144
public function remove($entity)
    {
        $idAccessorRegistry = $this->unitOfWork->getEntityRegistry()->getIdAccessorRegistry();
        $entityId           = $idAccessorRegistry->getEntityId($entity);

        $this->updateMap();

        if (!in_array($entityId, $this->map)) {
            throw new EntityNotFoundException;
        }

        $colJoinName = $this->relation->colJoinName();
        $mapObject   = $this->mapRepository->findOne()->with($colJoinName, Operator::EQUAL, $entityId)->getItem();
        $this->mapRepository->remove($mapObject);
        $this->map = array_diff($this->map, [$entityId]);
    }
1237694
public function update($entity)
    {
        $this->gateway->update($this->tableName, $this->builder->reduce($entity), $this->getIdentifier($entity));
        $this->builder->resolve($entity);
    }
1246023
public function remove(EntityInterface $entity)
    {
        $reflection = $this->getEntityClassReflection();
        
        if ($reflection->isInstance($entity)) {
            
            // trigger event for entity and dispatcher on before remove
            $this->dispatchEvent(ORMEvents::beforeRemove, new EntityLifecycleEvent($this, $entity));
            
            // needed variables
            $metadata = $this->getEntityMetadata();
            $remover = $this->createRemover();
            $identifier = $this->getEntityIdentifier();
            $connection = $this->getConnection();
            $propertyIdentifier = $metadata->getName($identifier, Metadata::CAMILIZED);
            
            // refinement of the request
            $remover->addConditions($identifier, $entity->getByProperty($propertyIdentifier));
            $remover->setLimit(1)->setOffset(0);
            
            // open transaction and try to execute sql
            $connection->transaction(function () use ($connection, $remover) {
                $connection->execute($remover->toSQL());
            });
            
            // reset identifier for removed entity
            $entity->setByProperty($propertyIdentifier, null);
            
            // trigger event for entity and dispatcher on after remove and all actions
            $this->dispatchEvent(ORMEvents::afterRemove, new EntityLifecycleEvent($this, $entity));
            
            return $this;
        }
        
        throw new NotSupportedException(sprintf('Unable to remove entity! Actual entity "%s" expected entity "%s"',
            get_class($entity), $this->getEntityName()));
    }
703503
public function update($entity)
    {
        $eloquentModel = $this->getEloquentModel($entity, true);

        $this->updateRelations($eloquentModel, 'update');

        $eloquentModel->save();

        $eloquentModel->afterSaving($entity, 'update');
    }
1091028
protected function updateInDatabase(Entity $entity)
    {
        $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
        $values = $entity->getModifiedRowData();
        return $this->connection->query(
            'UPDATE %n SET %a WHERE %n = ?',
            $this->getTable(),
            $values,
            $primaryKey,
            $this->getIdValue($entity)
        );
    }
1277001
protected function _save(EntityInterface $entity)
    {
        $entity = $this->_table()->patchEntity(
            $entity,
            ['verified' => true],
            $this->saveOptions()
        );
        $subject = $this->_subject(['entity' => $entity]);

        $this->_trigger('beforeSave', $subject);

        $success = call_user_func(
            [$this->_table(), $this->saveMethod()],
            $entity,
            $this->saveOptions()
        );

        $this->_trigger('afterSave', $subject);

        return $success;
    }
975075
public function afterSave(Event $event, Entity $entity): void
    {
        $fields = array_merge([
            $this->_table->getPrimaryKey(),
            $this->getConfig('sortField'),
        ], $this->getConfig('columnScope'));
        $savedEntity = $this->_table->get($entity->get($this->_table->getPrimaryKey()), [
            'fields' => $fields,
        ]);
        $scope = $this->getConfig('scope');

        foreach ($this->getConfig('columnScope') as $column) {
            $scope[$column] = $savedEntity->get($column);
        }
        $entitySort = $savedEntity->{$this->getConfig('sortField')};
        $entityId = $entity->get($this->_table->getPrimaryKey());

        if (empty($entitySort)) {
            $nextSortValue = $this->getNextSortValue($scope);
            $this->_table->updateAll([
                $this->getConfig('sortField') => $nextSortValue,
            ], [
                $this->_table->getPrimaryKey() => $entityId,
            ]);
        } else {
            if ($entitySort < $this->__originalSortValue) {
                $currentScope = $scope;
                $currentScope[] = "{$this->getConfig('sortField')} <" . $this->__originalSortValue;
                $currentScope[] = "{$this->getConfig('sortField')} >=" . $entitySort;
                $currentScope[] = [
                    $this->_table->getPrimaryKey() . ' !=' => $entity->id,
                ];

                $query = $this->_table->query()->update();
                $query->where($currentScope);
                $query->set([
                    $this->getConfig('sortField') => $query->newExpr($this->getConfig('sortField') . ' + 1'),
                ]);
                $query->execute();
            } elseif ($entitySort > $this->__originalSortValue) {
                $currentScope = $scope;
                $currentScope[] = "{$this->getConfig('sortField')} >" . $this->__originalSortValue;
                $currentScope[] = "{$this->getConfig('sortField')} <=" . $entitySort;
                $currentScope[] = [
                    $this->_table->getPrimaryKey() . ' !=' => $entity->id,
                ];

                $query = $this->_table->query()->update();
                $query->where($currentScope);
                $query->set([
                    $this->getConfig('sortField') => $query->newExpr($this->getConfig('sortField') . ' - 1'),
                ]);
                $query->execute();
            } elseif ($entitySort == $this->__originalSortValue) {
                $currentScope = $scope;
                $currentScope[] = "{$this->getConfig('sortField')} >=" . $entitySort;
                $currentScope[] = [
                    $this->_table->getPrimaryKey() . ' !=' => $entity->id,
                ];

                $query = $this->_table->query()->update();
                $query->where($currentScope);
                $query->set([
                    $this->getConfig('sortField') => $query->newExpr($this->getConfig('sortField') . ' + 1'),
                ]);
                $query->execute();
            }
        }
        $this->__originalSortValue = null;
    }
628779
protected function addToDb(Database\Model &$object)
    {
        $this->FKValue[1] = $object->id;
        $this->doQuery($this->addToDbQuery());
    }
619915
public function update($object)
    {
        if (!($object instanceof $this->objectType)) {
            throw new IllegalObjectTypeException('The modified object given to update() was not of the type (' . $this->objectType . ') this repository manages.', 1249479625);
        }
        $this->persistenceManager->update($object);
    }
1047850
public function removeEntity($entity)
    {
        $registries = $this->findRegistriesEntityById(get_class($entity), $entity->getObjectIdentifier());

        foreach ($registries as $registry) {
            $this->remove($registry);
        }
    }
253508
public function apply(Builder $builder, Entity $entity): void
    {
        $eagerLoads = $builder->getEagerLoads();

        // If there is any eagerload matching the eav key, we will replace it with
        // all the registered properties for the entity. We'll simulate as if the
        // user has manually added all of these withs in purpose when querying.
        if (array_key_exists('eav', $eagerLoads)) {
            $eagerLoads = array_merge($eagerLoads, $entity->getEntityAttributeRelations());

            $builder->setEagerLoads(array_except($eagerLoads, 'eav'));
        }
    }
175903
public function identifyEntity(ModelInterface $model, &$entity, $identifier)
    {
        $primaryKeys = $model->primaryFields();
        if (count($primaryKeys) !== 1) {
            return;
        }

        $field = reset($primaryKeys)->name();

        $this->setPropertyValue($entity, $field, $identifier);
    }
306712
protected function removeEntity(EntityInterface $entity)
    {
        Orm::getRepository($this->getEntityClassName())
            ->getIdentityMap()
            ->remove($entity);
    }
1336543
public function commit($entity)
    {
        switch ($entity->getEntityState()) {
            case AnDomain::STATE_NEW:
                $operation = AnDomain::OPERATION_INSERT;
                $command = 'insert';
                break;
            case AnDomain::STATE_MODIFIED :
                //get all the updated serializable property/value pairs
                $operation = AnDomain::OPERATION_UPDATE;
                $command = 'update';
                break;
            case  AnDomain::STATE_DELETED :
                $operation = AnDomain::OPERATION_DELETE;
                $command = 'delete';
                break;
            default :
                return;
        }

        $context = $this->getCommandContext();
        $context->operation = $operation;
        $context->entity = $entity;
        $context->data = array();
        $store = $this->getStore();

        if ($context->result = $this->getCommandChain()->run('before.'.$command, $context) !== false) {
            $context->data = $entity->getAffectedRowData();

            switch ($operation) {
                case(AnDomain::OPERATION_INSERT) :
                    if (!count($context->data)) {
                        throw new AnDomainRepositoryException('Attempting to store an entity with empty data');
                    }

                    $context->result = $store->insert($this, $context->data);
                    break;

                case(AnDomain::OPERATION_UPDATE) :
                case(AnDomain::OPERATION_DELETE) :

                    $keys = $this->_description->getIdentityProperty()->serialize($entity->getIdentityId());
                    $keys = array($this->_description->getIdentityProperty()->getName() => $entity->getIdentityId());

                    if ($operation & AnDomain::OPERATION_UPDATE) {
                        $context->result = count($context->data) ? $this->update($context->data, $keys) : true;
                    } else {
                        $context->result = $this->destroy($keys);
                    }
            }

            $this->getCommandChain()->run('after.'.$command, $context);
        }

        return    $context->result !== false;
    }
632851
public function delete(EntityInterface $entity)
    {
        $this->entity = $entity;
        $primaryKey = $this->getDescriptor()->getPrimaryKey()->getName();
        $table = $this->getDescriptor()->getTableName();
        $sql = Sql::createSql($this->getAdapter());

        $this->triggerBeforeDelete($entity);

        $this->setUpdateCriteria(
            $sql->delete($table),
            $primaryKey,
            $table
        )->execute();

        $this->triggerAfterDelete($entity);
        $this->removeEntity($entity);
        return $this;
    }