@param Entity $entity

@throws EntityDefinitionException
770271
public function readEntityMetadata(Entity $entity)
    {
        $className = $entity->getClassName();
        if (!isset($this->entities[ $className ])) {
            $this->entities[ $className ] = $entity;

            $this->readTableName($entity);

            $filter = \ReflectionProperty::IS_PRIVATE
                      | \ReflectionProperty::IS_PROTECTED
                      | \ReflectionProperty::IS_PUBLIC;

            $properties = $this->annotationReader->readProperties($className, $filter);

            try {
                foreach ($properties as $property => $comment) {
                    if ($comment->hasAnnotationType(FieldAnnotation::class)) {
                        $this->processField($comment, $property, $entity);
                    } else if ($comment->hasAnnotationType(RelationAnnotation::class)) {
                        $this->processRelation($comment, $property, $entity);
                    }
                }
                if ($entity->getPrimaryKey() === null) {
                    throw new EntityDefinitionException("Class {$className} must have a primary key.");
                }
            } catch (EntityDefinitionException $e) {
                throw new EntityDefinitionException("Could not instantiate metadata for {$className}", 0, $e);
            }
        }
    }
1047850
public function removeEntity($entity)
    {
        $registries = $this->findRegistriesEntityById(get_class($entity), $entity->getObjectIdentifier());

        foreach ($registries as $registry) {
            $this->remove($registry);
        }
    }
479209
public function persist(Entity $entity)
    {
        $this->checkEntityType($entity);

        $this->events->invokeCallbacks(Events::EVENT_BEFORE_PERSIST, $entity);
        if ($entity->isDetached()) {
            $entity->makeAlive($this->entityFactory, $this->connection, $this->mapper);
            $this->events->invokeCallbacks(Events::EVENT_BEFORE_CREATE, $entity);
            $result = $id = $this->insertIntoDatabase($entity);
            $entity->attach($id);
            $this->events->invokeCallbacks(Events::EVENT_AFTER_CREATE, $entity);
        } else {
            if ($entity->isModified()) {
                $this->events->invokeCallbacks(Events::EVENT_BEFORE_UPDATE, $entity);
                $result = $this->updateInDatabase($entity);
                $this->events->invokeCallbacks(Events::EVENT_AFTER_UPDATE, $entity);
            }
            $this->persistHasManyChanges($entity);
            $entity->markAsUpdated();
        }
        $this->events->invokeCallbacks(Events::EVENT_AFTER_PERSIST, $entity);

        return isset($result) ? $result : null;
    }
995680
public function add($entity)
    {
        foreach ($this->restrictions as $preset) {
            if ($preset['op'] == Operator::EQUAL) {
                $property          = Normalise::toVariable($preset['field']);
                $entity->$property = $preset['value'];
            }
        }

        $this->unitOfWork->scheduleForInsertion($entity);
    }
1371171
private function updateActiveRow(Entity $entity) {
		$reflection = $entity->getColumns();
		$record = array();
		foreach ($reflection as $property) {
			$name = $property['name'];
			if ($entity->toRow()->$name != $entity->$name) {
				$record[$name] = $entity->$name;
			}
		}
		if (count($record) > 0) {
			$entity->toRow()->update($record);
		}
		$this->addLoop($entity);
	}
1168383
public function delete(Entity $entity)
    {
        $endPointUrl = $this->getEndPointUrl(get_class($entity));

        if (method_exists($entity, 'endPointUrlArg')) {
            $endPointUrl.= '/'.$entity->endPointUrlArg();
        }

        return $this->curlDelete($endPointUrl);
    }
969229
public function solveFetch(Entity &$entity)
    {
        // We need to find all target entities with the ID defined in the current entity source column
        $column = $this->relation->join->column;

        $value = $entity->{$column};
        $where = array($this->relation->join->targetColumn => $value);

        $query = EntityManager::find($this->relation->targetEntity);
        return $query->where($where)->all();
    }
1333997
public function addReferencesToEntity($entity)
    {
        foreach ($this->strategies as $strategy) {
            $strategy->addReferencesToEntity($entity);
        }

        $this->objectManager->flush();
    }
740144
protected function persistHasManyChanges(Entity $entity)
    {
        $primaryKey = $this->mapper->getPrimaryKey($this->getTable());
        $idField = $this->mapper->getEntityField($this->getTable(), $primaryKey);
        $driver = $this->connection->getDriver();

        foreach ($entity->getHasManyRowDifferences() as $key => $difference) {
            list($columnReferencingSourceTable, $relationshipTable, $columnReferencingTargetTable) = explode(':', $key);
            $multiInsert = [];
            foreach ($difference as $value => $count) {
                if ($count > 0) {
                    for ($i = 0; $i < $count; $i++) {
                        $multiInsert[] = [
                            $columnReferencingSourceTable => $entity->$idField,
                            $columnReferencingTargetTable => $value,
                        ];
                    }
                } else {
                    if ($driver instanceof \Dibi\Drivers\PostgreDriver) {
                        $this->connection->query(
                            'DELETE FROM %n WHERE [ctid] IN (SELECT [ctid] FROM %n WHERE %n = ? AND %n = ? LIMIT %i)',
                            $relationshipTable,
                            $relationshipTable,
                            $columnReferencingSourceTable,
                            $entity->$idField,
                            $columnReferencingTargetTable,
                            $value,
                            -$count
                        );
                    } elseif ($driver instanceof \Dibi\Drivers\Sqlite3Driver) {
                        $this->connection->query(
                            'DELETE FROM %n WHERE [rowid] IN (SELECT [rowid] FROM %n WHERE %n = ? AND %n = ? LIMIT %i)',
                            $relationshipTable,
                            $relationshipTable,
                            $columnReferencingSourceTable,
                            $entity->$idField,
                            $columnReferencingTargetTable,
                            $value,
                            -$count
                        );
                    } else {
                        $this->connection->query(
                            'DELETE FROM %n WHERE %n = ? AND %n = ? %lmt',
                            $relationshipTable,
                            $columnReferencingSourceTable,
                            $entity->$idField,
                            $columnReferencingTargetTable,
                            $value,
                            -$count
                        );
                    }
                }
            }
            if (!empty($multiInsert)) {
                $this->connection->query(
                    'INSERT INTO %n %ex',
                    $relationshipTable,
                    $multiInsert
                );
            }
        }
    }
289344
public function save(Entity $entity) {
    $this->_em->persist($entity);
    $this->_em->flush();
    return $this;
  }
627366
public function call_moveDown(\Asgard\Entity\Entity $entity) {
		$next = $this->call_next($entity);
		if(!$next)
			return;
		$next2 = $this->call_next($next);
		if($next2)
			$pos = ($next->position + $next2->position)/2;
		else
			$pos = $next->position + 1;
		$entity->save(['position'=>$pos]);
	}
369056
protected function generateMethods(Entity $entity)
    {
        $result     = [];
        $descriptor = $entity->getDescriptor();
        $methods    = $descriptor->getMethodList() ?: [];

        foreach ($methods as $method) {
            $result[] = $this->generateMethod($entity, $method);
        }

        return $result;
    }
611796
public function save(AbstractEntity $entity)
    {
        if (empty($entity->getId())) {
            $this->getEntityManager()->persist($entity);
        }
        $this->getEntityManager()->flush($entity);
    }
1315428
public function push(Entity $entity) {
		$entity->setEntityManager($this->entityManager);
		if ($entity->toRow()) {
			$this->rows[$entity->getPrimary()] = $entity;
		} else {
			$this->rows[] = $entity;
		}
	}
612804
public function injectEntityDependencies(EntityInterface $entity): void
    {
        $this->buildEntityInjectMethodsForEntity($entity);
        $entityFqn = $this->leadingSlash($entity::getDoctrineStaticMeta()->getReflectionClass()->getName());
        $this->injectStatic($entity, $this->entityInjectMethods[$entityFqn][self::TYPE_KEY_STATIC]);
        $this->inject($entity, $this->entityInjectMethods[$entityFqn][self::TYPE_KEY_INSTANCE]);
    }
1312742
protected function updateField(Entity $entity, $field) {
		if ($entity->dirty($field)) {
			return;
		}

		$entity->set($field, $this->getUserId());
	}
4553
public function save( EntityDocument $entity ) {
		if( !$this->cache->save(
			$this->getCacheIdFromEntityId( $entity->getId() ),
			$entity,
			$this->lifeTime
		) ) {
			throw new RuntimeException( 'The cache failed to save for entity ' . $entity->getId()->getSerialization() );
		}
	}