insertOne

Insert a new entity in the database. The entity is passed by reference.
It is updated with values returned by the database (ie, default values).

@access public
@param  FlexibleEntityInterface  $entity
@return Model                    $this
495486
public function insertOne(FlexibleEntityInterface &$entity)
    {
        $values = $entity->fields(
            array_intersect(
                array_keys($this->getStructure()->getDefinition()),
                array_keys($entity->fields())
            )
        );
        $sql = strtr(
            "insert into :relation (:fields) values (:values) returning :projection",
            [
                ':relation'   => $this->getStructure()->getRelation(),
                ':fields'     => $this->getEscapedFieldList(array_keys($values)),
                ':projection' => $this->createProjection()->formatFieldsWithFieldAlias(),
                ':values'     => join(',', $this->getParametersList($values))
            ]);

        $entity = $this
            ->query($sql, array_values($values))
            ->current()
            ->status(FlexibleEntityInterface::STATUS_EXIST);

        return $this;
    }
1176027
@Transactional()
    public E add(E entity)
    {
        getSqlSession().insert(getMapId(ADD_MAP_ID), entity);
        return entity;
    }
981066
final public function save(BaseEntityInterface $entity, $flush = false): self
    {
        $this->_em->persist($entity);

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

        return $this;
    }
599878
public function setEntity($entity)
    {
        if ($this->isRegistryEntity(get_class($entity))) {
            $this->classname = get_class($entity);
        }
        $this->entity = $entity;

        return $this;
    }
449706
public function buildInsertQuery($entity) {

        $mapper = MapperBuilder::buildFromMapping($this->mapping, get_class($entity));
        $fields = "";
        $values = "";
        foreach ($mapper->getPropertys() as $property) {
            if ($property->getColumn() != "id") {
                $method = "get" . $property->getName();
                $entity->$method();
                $fields .= "`" . $property->getColumn() . "`, ";
                $values .= "'" . $entity->$method() . "', ";
            }
        }

        $fields = substr_replace($fields, "", -2);
        $values = substr_replace($values, "", -2);

        $query = "INSERT INTO `" . $mapper->getTable() . "` (" . $fields . ") VALUES (" . $values . ");";

        return $query;
    }
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;
    }
502926
public function insert($table)
    {
        $this->type  = 'INSERT';
        $this->table = $this->quoteName($table);
        return $this;
    }
610418
public function insert($entity)
    {
        $data     = $this->builder->reduce($entity);
        $entityId = $this->entityRegistry->getEntityId($entity);

        #try
        #{
        $this->connection->insert($this->tableName, $data);

        if (empty($entityId)) {
            $this->entityRegistry->setEntityId($entity, $this->connection->lastInsertId());
        }

        $this->builder->resolve($entity);
        #}
        #catch (\Exception $e)
        #{
        #	throw new OrmException("Entity with id {$entityId} already exists.\n" . $e->getMessage(), 0, $e);
        #}
    }
554614
public function load(EntityInterface $entity)
    {
        $repository = $this->getParentRepository();

        $query = $repository->find()
            ->where($this->getConditions($entity))
            ->limit($this->limit);
        $this->addJoinTable($query);
        $this->checkConditions($query)
            ->checkOrder($query);

        /** @var EntityCollectionInterface $collection */
        $collection = $query->all();

        $collection
            ->setParentEntity($entity)
            ->getEmitter()
            ->addListener(EntityAdded::ACTION_ADD, [$this, 'add'])
            ->addListener(EntityRemoved::ACTION_REMOVE, [$this, 'remove']);

        return $collection;
    }
1195576
public function scheduleForInsertion($entity)
    {
        $objectHashId                               = $this->entityRegistry->getObjectHashId($entity);
        $this->scheduledForInsertion[$objectHashId] = $entity;
        $this->entityRegistry->setState($entity, EntityStates::QUEUED);
    }
88216
public function update(BaseEntity $entity) {
		$this->beforeAction(self::EVENT_UPDATE);
		$data = ArrayHelper::toArray($entity);
		$this->validateForeign($data);
		$this->validateForbiddenChangeFields($data);
		$entity->validate();
		$this->repository->update($entity);
		return $this->afterAction(self::EVENT_UPDATE);
	}
470149
private function insert($object)
    {
        //TODO: this should only be done if there is a Delete query pending for the same entity table
        $this->manager->commit();

        $query = $this->manager
            ->getDriver()
            ->getQueryBuilder()
            ->insert($this->getTable());

        $query->values(
            $query->createPositionalParameter(
                array_filter(
                    $this->toArray($object),
                    [Utils::class, 'notNull']
                )
            )
        );

        return $this->setFieldValue(
            $object,
            $this->getPrimaryKey(),
            $query->query()
        );
    }
270101
public function createBatch(EntityInterface $entity)
    {
        if (is_null($this->putBatch)) {
            $this->putBatch = WriteRequestBatch::factory($this->dynamoDb);
        }
        $this->putBatch->add(new PutRequest(Item::fromArray($entity->getArrayCopy()), $this->getEntityTable($entity)));
    }
1167000
public function addInsert($entity)
    {
        $this->inserts[] = $entity;
        if ( ! is_null($this->mockIdGeneratorType) && $this->mockIdGeneratorType == ClassMetadata::GENERATOR_TYPE_IDENTITY
                || $this->class->isIdGeneratorIdentity()) {
            $id = $this->identityColumnValueCounter++;
            $this->postInsertIds[] = [
                'generatedId' => $id,
                'entity' => $entity,
            ];
            return $id;
        }
        return null;
    }
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;
	}
180855
private function populate($entity, $model)
    {
        if ($entity && (is_array($entity) || is_object($entity))) {
            foreach ($entity as $name => $value) {
                $model->$name = $value;
            }
        }

        return $model;
    }
254451
public function flush($entity)
    {
        /**
         * @var ObjectManager $objectManager
         */
        $objectManager = $this
            ->get('elcodi.provider.manager')
            ->getManagerByEntityNamespace(get_class($entity));

        $objectManager->persist($entity);
        $objectManager->flush($entity);

        return $this;
    }