Ensures that the provided entity contains non-empty values for the left and
right fields

@param \Cake\Datasource\EntityInterface $entity The entity to ensure fields for
@return void
974706
protected function _ensureFields($entity)
    {
        $config = $this->getConfig();
        $fields = [$config['left'], $config['right']];
        $values = array_filter($entity->extract($fields));
        if (count($values) === count($fields)) {
            return;
        }

        $fresh = $this->_table->get($entity->get($this->_getPrimaryKey()), $fields);
        $entity->set($fresh->extract($fields), ['guard' => false]);

        foreach ($fields as $field) {
            $entity->setDirty($field, false);
        }
    }
279742
public function associate($entity)
    {
        $this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey));
    }
310461
public function saveEntity(EntityInterface $entity)
    {
        $check = $this->storage->fetchEntityById($entity->getIdentifier());

        if ($check) {
            return $this->storage->updateEntity($entity);
        } else {
            return $this->storage->insertEntity($entity);
        }
    }
306712
protected function removeEntity(EntityInterface $entity)
    {
        Orm::getRepository($this->getEntityClassName())
            ->getIdentityMap()
            ->remove($entity);
    }
1058556
public function beforeDelete(Event $event, EntityInterface $entity)
    {
        $config = $this->getConfig();
        $this->_ensureFields($entity);
        $left = $entity->get($config['left']);
        $right = $entity->get($config['right']);
        $diff = $right - $left + 1;

        if ($diff > 2) {
            $query = $this->_scope($this->_table->query())
                ->delete()
                ->where(function ($exp) use ($config, $left, $right) {
                    /* @var \Cake\Database\Expression\QueryExpression $exp */
                    return $exp
                        ->gte($config['leftField'], $left + 1)
                        ->lte($config['leftField'], $right - 1);
                });
            $statement = $query->execute();
            $statement->closeCursor();
        }

        $this->_sync($diff, '-', "> {$right}");
    }
125538
public function attachEntityFields(EntityInterface $entity)
    {
        $_fields = [];
        foreach ($this->_attributesForEntity($entity) as $attr) {
            $field = $this->_prepareMockField($entity, $attr);
            if ($entity->has($field->get('name'))) {
                $this->_fetchPost($field);
            }

            $field->fieldAttached();
            $_fields[] = $field;
        }

        $entity->set('_fields', new FieldCollection($_fields));

        return $entity;
    }
117486
private function buildEntityInjectMethodsForEntity(EntityInterface $entity): void
    {
        $reflection = $entity::getDoctrineStaticMeta()->getReflectionClass();
        $entityFqn  = $this->leadingSlash($reflection->getName());
        if (array_key_exists($entityFqn, $this->entityInjectMethods)) {
            return;
        }
        $this->entityInjectMethods[$entityFqn] = [
            self::TYPE_KEY_INSTANCE => [],
            self::TYPE_KEY_STATIC   => [],
        ];

        $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
        foreach ($methods as $method) {
            if (!\ts\stringStartsWith($method->getName(), self::INJECT_DEPENDENCY_METHOD_PREFIX)) {
                continue;
            }
            $typeKey = $method->isStatic() ? self::TYPE_KEY_STATIC : self::TYPE_KEY_INSTANCE;

            $this->entityInjectMethods[$entityFqn][$typeKey][$method->getName()] =
                $this->getDependencyForInjectMethod($method);
        }
    }
397983
public function insert(EntityInterface $entity)
	{
		$sql = <<<EOT
INSERT INTO `$this->tableName` (`Name`, `Description`, `Created`) 
VALUES ( :name , :description , :created )
EOT;

		$stmt = $this->pdo->prepare($sql);
		$stmt->bindParam(':name', $entity->getName(), \PDO::PARAM_STR);
		$stmt->bindParam(':description', $entity->getDescription(), \PDO::PARAM_STR);
		$stmt->bindParam(':created', $entity->getCreatedRfc3339(), \PDO::PARAM_STR);
		$res = $stmt->execute();

		if ($res === false || $stmt->rowCount() !== 1) {
			return false;
		}

		$entity->setProjectId($this->pdo->lastInsertId());

		return $entity;
	}
286809
public function validate(EntityInterface $entity)
    {
        if (!$entity instanceof Attribution) {
            throw new Exception(
                sprintf('The Entity to validate must be an instance of %s', Attribution::class)
            );
        }

        $this->validateSource($entity->getSource());
        $this->validateTarget($entity->getTarget());
        $this->validateRole($entity->getRole());
        $errors = $this->getErrors();

        return empty($errors);
    }
142782
public function afterSave(Event $event, EntityInterface $entity) {
		if ($this->_checkEvent($event) && $entity->isNew()) {
			$fileField = $this->getConfig('fileField');

			$entity['hash'] = $this->getFileHash($entity, $fileField);
			$entity['path'] = $this->pathBuilder()->path($entity);

			if (!$this->_storeFile($event)) {
				return;
			}

			$event->stopPropagation();
		}
	}
1349436
public function saveEntity($entity)
    {
        if (!$this->entityManager->contains($entity)) {
            $this->entityManager->persist($entity);
        }

        $this->entityManager->flush();
        return true;
    }
379026
public function post(EntityInterface $entity)
    {
        $pk = (string)TableRegistry::get($entity->source())->primaryKey();
        if (empty($this->_controller->request->data['comment']) ||
            $this->config('settings.visibility') !== 1 ||
            !$entity->has($pk)
        ) {
            return false;
        }

        $this->_controller->loadModel('Comment.Comments');
        $data = $this->_getRequestData($entity);
        $this->_controller->Comments->validator('commentValidation', $this->_createValidator());
        $comment = $this->_controller->Comments->newEntity($data, ['validate' => 'commentValidation']);
        $errors = $comment->errors();
        $errors = !empty($errors);

        if (!$errors) {
            $persist = true;
            $saved = true;
            $this->_controller->Comments->addBehavior('Tree', [
                'scope' => [
                    'entity_id' => $data['entity_id'],
                    'table_alias' => $data['table_alias'],
                ]
            ]);

            if ($this->config('settings.use_akismet')) {
                $newStatus = $this->_akismetStatus($data);
                $comment->set('status', $newStatus);

                if ($newStatus == 'spam' &&
                    $this->config('settings.akismet_action') != 'mark'
                ) {
                    $persist = false;
                }
            }

            if ($persist) {
                $saved = $this->_controller->Comments->save($comment);
            }

            if ($saved) {
                $this->_afterSave($comment);

                return true; // all OK
            } else {
                $errors = true;
            }
        }

        if ($errors) {
            $this->_setErrors($comment);
            $errorMessage = $this->config('errorMessage');
            if (is_callable($errorMessage)) {
                $errorMessage = $errorMessage($comment, $this->_controller);
            }
            $this->_controller->Flash->danger($errorMessage, ['key' => 'commentsForm']);
        }

        return false;
    }
1212077
public function isEqualTo(EntityInterface $entity)
    {
        if ($entity->getType() !== $this->getType()) {
            return false;
        }

        if ($this->getType()->getAttributes()->getSize() !== $this->value_holder_map->getSize()) {
            return false;
        }

        foreach ($this->getType()->getAttributes()->getKeys() as $attribute_name) {
            $value_holder = $this->value_holder_map->getItem($attribute_name);
            if (!$value_holder->sameValueAs($entity->getValue($attribute_name))) {
                return false;
            }
        }

        return true;
    }
289710
protected function setDefaultValues($entity)
    {
        $entityClass = ClassUtils::getClass($entity);

        $meta = $this->extendedMetadataFactory->getExtendedMetadata($entityClass);

        if (!isset($meta['defaultValues']) || empty($meta['defaultValues'])) {
            return;
        }

        $defaultValuesMap = $meta['defaultValues'];
        $this->filterDefaultValuesMap($defaultValuesMap, $entity, $entityClass);

        if (empty($defaultValuesMap)) {
            return;
        }

        $sourcePropertyValues = $this->getSourcePropertyValues(
            array_unique(array_values($defaultValuesMap)),
            $entity,
            $entityClass
        );

        $recomputeChangeSet = false;

        foreach ($defaultValuesMap as $targetProperty => $sourcePropertyPath) {
            if (null === $sourcePropertyValues[$sourcePropertyPath]) {
                continue;
            }
            if (!$this->propertyAccessor->isWritable($entity, $targetProperty)) {
                throw new DefaultValueException(sprintf('Property "%s::$%s" is not writable.', $entityClass, $targetProperty));
            }

            $this->propertyAccessor->setValue($entity, $targetProperty, $sourcePropertyValues[$sourcePropertyPath]);

            $recomputeChangeSet = true;
        }
        if ($recomputeChangeSet) {
            $this->recomputeChangeSet($entity);
        }
    }
1312742
protected function updateField(Entity $entity, $field) {
		if ($entity->dirty($field)) {
			return;
		}

		$entity->set($field, $this->getUserId());
	}
397904
public function update(EntityInterface $entity): void
    {
        $uri = $this->getEntityEndpointUri($entity->id());
        $response = $this->getClient()->put(
            $uri,
            $this->getEntitySerializer()->serialize($entity, 'json')
        );
        $this->getEntitySerializer()->setPropertiesFromResponse($response, $entity);
    }
753782
protected function _unsetEmptyFields(EntityInterface $entity)
    {
        $translations = (array)$entity->get('_translations');
        foreach ($translations as $locale => $translation) {
            $fields = $translation->extract($this->_config['fields'], false);
            foreach ($fields as $field => $value) {
                if (strlen($value) === 0) {
                    $translation->unsetProperty($field);
                }
            }

            $translation = $translation->extract($this->_config['fields']);

            // If now, the current locale property is empty,
            // unset it completely.
            if (empty(array_filter($translation))) {
                unset($entity->get('_translations')[$locale]);
            }
        }

        // If now, the whole _translations property is empty,
        // unset it completely and return
        if (empty($entity->get('_translations'))) {
            $entity->unsetProperty('_translations');
        }
    }