Adds an entity to the collection

@param EntityInterface $entity
@throws InvalidArgumentException If the entity is not from
the provided entity type

@return self
514334
public function addEntity(EntityInterface $entity)
    {
        if (!is_a($entity, $this->type)) {
            throw new InvalidArgumentException(
                "Trying to add an entity that is not a {$this->type}."
            );
        }
        $this->data[] = $entity;
        $event = new EntityAdded($entity, ['collection' => $this]);
        $this->getEmitter()->emit($event);
        return $this;
    }
289344
public function save(Entity $entity) {
    $this->_em->persist($entity);
    $this->_em->flush();
    return $this;
  }
925063
public function update($entity)
    {
        if (!$entity instanceof $this->entityClass) {
            throw new \InvalidArgumentException(
                "The entity to be updated must be an instance of {$this->entityClass}."
            );
        }

        $entityId = $entity->id;
        $data = $entity->toArray();
        unset($data['id']);

        return $this->adapter->update($this->entityTable, $data, "id = $entityId");
    }
33024
public function persist(EntityInterface $entity)
    {
        $reflection = $this->getEntityClassReflection();
        
        if ($reflection->isInstance($entity)) {
            
            $this->dispatchEvent(ORMEvents::beforePersist, new EntityLifecycleEvent($this, $entity));
            
            $connection = $this->getConnection();
            $metadata = $this->getEntityMetadata();
            $isEntityNew = $this->isNewEntity($entity);
            $entityData = $this->getEntityDataArray($entity);
            
            $persister = $this->getPersisterForEntity($entity);
            $persister->setDataBatch($entityData);
            
            $propertyIdentifier = $metadata->getName($metadata->getIdentifier(), Metadata::CAMILIZED);
            
            if (!$isEntityNew && $reflection->hasProperty($propertyIdentifier)) {
                $persister->addConditions($metadata->getIdentifier(), $entity->getByProperty($propertyIdentifier));
            }
            
            $connection->start();
            $connection->execute($persister->toSQL());
            
            if ($reflection->hasProperty($propertyIdentifier) && $isEntityNew) {
                $reflection->getProperty($propertyIdentifier)->setValue($entity, $connection->lastInsertId());
            }
            
            $this->dispatchEvent(ORMEvents::afterPersist, new EntityLifecycleEvent($this, $entity));
            
            // commit changes after event dispatching
            $connection->commit();
            
            return $this;
        }
        
        throw new NotSupportedException(sprintf('Unable to persist entity! Actual entity "%s" expected entity "%s"',
            get_class($entity), $this->getEntityName()));
    }
199058
public function update(EntityInterface $entity)
	{
		$sql = <<<EOT
UPDATE `$this->tableName` 
SET 
	`Name` = :name , 
	`Description` = :description , 
	`Created` = :created  
WHERE `ProjectId` = :id 
EOT;

		$stmt = $this->pdo->prepare($sql);
		$stmt->bindParam(':id', $entity->getProjectId(), \PDO::PARAM_INT);
		$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;
		}

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

        $this->validateName($entity->getName());
        $errors = $this->getErrors();

        return empty($errors);
    }
740967
final public function remove(BaseEntityInterface $entity, $flush = false): self
    {
        $this->_em->remove($entity);

        if (true === $flush) {
            $this->_em->flush();
        }

        return $this;
    }
1084351
public function add($property)
    {
        if (!$property instanceof PropertyInterface) {
            throw new \InvalidArgumentException(
                'This Property must be a instance of \ClassGeneration\PropertyInterface'
            );
        }
        if ($property->getName() === null) {
            $property->setName('property' . ($this->count() + 1));
        }

        parent::set($property->getName(), $property);

        return true;
    }
878442
public function encryptEntity(EntityInterface $entity): EntityInterface
    {
        if (!$this->isEncryptable($entity)) {
            return $entity;
        }

        $fields = $this->getFields();
        $encryptionKey = $this->getConfig('encryptionKey');
        $base64 = $this->getConfig('base64');
        $table = $this->getTable();

        $patch = [];
        foreach ($fields as $name => $field) {
            if (!$table->hasField($name)) {
                continue;
            }
            $value = $entity->get($name);
            if ($value === null || is_resource($value)) {
                continue;
            }
            $encrypted = Security::encrypt($value, $encryptionKey);
            if ($base64 === true) {
                $encrypted = base64_encode($encrypted);
            }
            $patch[$name] = $encrypted;
        }

        if (!empty($patch)) {
            $accessible = array_fill_keys(array_keys($patch), true);
            $entity = $table->patchEntity($entity, $patch, [
                'accessibleFields' => $accessible,
            ]);
        }

        return $entity;
    }
891662
protected function beforeSave(EntityInterface $entity): EntityInterface
    {
        $argv = compact('entity');
        $argv = $this->getEventManager()->prepareArgs($argv);

        $this->getEventManager()->trigger("{$this->event_prefix}_save_before", $this, $argv);
        if ($this->entity_event_prefix) {
            $this->getEventManager()->trigger("{$this->entity_event_prefix}_save_before", $this, $argv);
        }

        return $argv['entity'];
    }
580412
public static function newInstance(EntityInterface $realEntity): self
    {
        $basename = $realEntity->basename();
        $path = $realEntity->path();

        $instance = new static($basename, $path);
        $instance->realEntity = $realEntity;

        return $instance;
    }
1143821
public function isNewEntity(EntityInterface $entity)
    {
        $reflection = $this->getEntityClassReflection();
        $metadata = $this->getEntityMetadata();
        $identifier = $metadata->getName($metadata->getIdentifier(), Metadata::CAMILIZED);
        
        return null !== $identifier
            ? ($reflection->getProperty($identifier)->getValue($entity) === null) : true;
    }
112952
public function setEntityTable($entityTable)
    {
        if (!is_string($entityTable) || empty($entityTable)) {
            throw new \InvalidArgumentException('The entity table is invalid.');
        }
        $this->entityTable = $entityTable;

        return $this;
    }
767426
public function insert($entity)
    {
        if (!$entity instanceof $this->entityClass) {
            throw new \InvalidArgumentException(
                "The entity to be inserted must be an instance of {$this->entityClass}."
            );
        }

        return $this->adapter->insert($this->entityTable, $entity->toArray());
    }
1086531
public function registerEntity(CollectionableInterface $entity)
    {
        if (!is_a($entity, $this->entityCollection->getEntityClass())) {
            throw new \InvalidArgumentException(sprintf('Only "%s" object allowed into "%s" store, "%s" given.',
                $this->entityCollection->getEntityClass(),
                get_class($this),
                get_class($entity)
            ));
        }

        $this->entityCollection->set(
            $entity->getId(),
            $this->loadDelegates($entity)
        );
    }
875259
public function removeEntity(Entity $entity)
    {
        foreach ($this->entities as $key => $entityElement) {
            if ($entityElement === $entity) {
                unset($this->entities[$key]);
                break;
            }
        }

        return $this;
    }
215869
public function getSaverForEntity(
        EntityInterface $entity
    ): EntitySaverInterface {
        $fqn = $this->getEntityNamespace($entity);

        return $this->getSaverForEntityFqn($fqn);
    }