Get Entity with Primary Key value

@param string $entity
@param int|string $primaryValue
@return false|Entity
@throws \Exception
351779
public static function get($entity, $primaryValue)
    {
        $query = new Query($entity);
        $column = self::getInstance()->getEntityStructure($entity)->primaryColumn;
        $query->where($column->name, $primaryValue);
        return $query->one();
    }
376935
public static function save($entity): ResultInterface
    {
        $className = \get_class($entity);
        list($table, , , $fields) = self::getFields($entity, 1);
        $instance = self::getInstance($className);

        $fields = $fields ?? [];
        $query  = Query::table($table)->selectInstance($instance);
        // Set Primary Id to Entity
        $query->addDecorator(function ($primaryId) use ($entity, $className) {
            list(, , $idColumn) = self::getTable($className);
            // When Primary Id is auto increment
            $getter = 'get' . StringHelper::camel($idColumn, false);
            $setter = 'set' . StringHelper::camel($idColumn, false);
            if (method_exists($entity, $getter) && method_exists($entity, $setter)) {
                if ($entity->$getter() === null) {
                    $entity->$setter($primaryId);
                } else {
                    $primaryId = $entity->$getter();
                }
            }
            return $primaryId;
        });
        return $query->insert($fields);
    }
839800
protected function getPrimaryDomain($domain)
    {
        /** @var \Rcm\Entity\Domain $domain */
        $domain = $this->_em->getRepository(\Rcm\Entity\Domain::class)
            ->findOneBy(['domain' => $domain]);

        if (empty($domain)) {
            return null;
        }

        $primary = $domain->getPrimary();

        if (!$primary->getPrimary()) {
            return $primary;
        }

        return $this->getPrimaryDomain($primary->getDomainName());
    }
266500
public function delete($entity) {
		if (is_null($entity))
			return;
		
		//connect to database
		$this->mapper->connect();
		
		//begin transaction
		$this->mapper->beginTransaction();
		
		//get primary key
		$pk = $this->getPropertyValue($this->entityProfile, $entity, $this->entityProfile->getPrimaryKey());

		foreach ($this->entityProfile->getReferences() as $assoc)
			$this->entityProfile->getAssociation($assoc)->delete($this->mapper, $pk);
		
		//build fluent query
		$query = $this->mapper->newQuery()
		->deleteFrom($this->entityProfile->getEntityTable())
		->where(Column::__callstatic($this->entityProfile->getPrimaryKey(true))->eq($pk));
		
		//run query
		$result = $query->exec();
		
		//commit
		$this->mapper->commit();
		return $result;
	}
1196640
@Override
	public CPRule fetchByPrimaryKey(Serializable primaryKey) {
		Serializable serializable = entityCache.getResult(CPRuleModelImpl.ENTITY_CACHE_ENABLED,
				CPRuleImpl.class, primaryKey);

		if (serializable == nullModel) {
			return null;
		}

		CPRule cpRule = (CPRule)serializable;

		if (cpRule == null) {
			Session session = null;

			try {
				session = openSession();

				cpRule = (CPRule)session.get(CPRuleImpl.class, primaryKey);

				if (cpRule != null) {
					cacheResult(cpRule);
				}
				else {
					entityCache.putResult(CPRuleModelImpl.ENTITY_CACHE_ENABLED,
						CPRuleImpl.class, primaryKey, nullModel);
				}
			}
			catch (Exception e) {
				entityCache.removeResult(CPRuleModelImpl.ENTITY_CACHE_ENABLED,
					CPRuleImpl.class, primaryKey);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return cpRule;
	}
466297
public function get($entity)
    {
        $key = $this->mapping['fieldname'];
        $valueMethod = 'get' . ucfirst($key);
        $value = $entity->$valueMethod();

        return $value;
    }
891495
public function save($entity)
    {
        $query = new Query($entity, false);
        $structure = $this->getEntityStructure($entity);

        if ($entity->_saved) {
            // Update
            $result = $query->update()->set($this->getEntityDataArray($entity))->where(array($structure->primaryColumn->name => $entity->_id))->apply();

            // Update relations
            RelationManager::with($entity)->saveRelations();

            return $result;
        } else {
            // Insert
            $id = $query->insert()->into($structure->tableName)->values($this->getEntityDataArray($entity))->apply();

            if ($id === false) {
                return false; // @codeCoverageIgnore
            }

            // Update relations
            RelationManager::with($entity)->saveRelations();

            // Save ID and state
            $entity->{$structure->primaryColumn->propertyName} = $id;
            $entity->_id = $id;
            $entity->_saved = true;

            return true;
        }
    }
1049834
public function findByEntityInstance($entity)
    {
        $businessEntity = null;
        $class = new \ReflectionClass($entity);
        while (!$businessEntity && $class && $class->name !== null) {
            $businessEntity = $this->findByEntityClassname($class->name);
            $class = $class->getParentClass();
        }

        return $businessEntity;
    }
749125
public function findPrimaries(array $primaryValues, array $associate = [])
    {
        $reflection = Entity\Reflection::load($this->getEntityName());

        if (!$reflection->hasPrimary()) {
            throw new Exception\RepositoryException(
                "Method can not be used because entity " . $this->getEntityName()
                . " has no primary property defined!"
            );
        }

        if (empty($primaryValues)) {
            throw new Exception\RepositoryException(
                "Values can not be empty!"
            );
        }

        try {

            return $this->query()
                ->select()
                ->setFilter(
                    [
                        $reflection->getPrimaryProperty()->getName() => [
                            Filter::EQUAL => $primaryValues
                        ]
                    ]
                )
                ->associate($associate)
                ->run($this->connection);
        } catch (Exception\QueryException $e) {
            throw new Exception\RepositoryException($e->getMessage());
        }
    }
789407
public function get($key, $defaultValue = null)
    {
        if (!isset($this->keyToIndexMap[$key]) && 2 === func_num_args()) {
            return $defaultValue;
        }

        return $this->value($key);
    }
1255451
public function pushData(Entity $entity)
    {
        if ($this->isMany()) {
            $this->primaryData[] = $entity;
            return $this;
        }
        $this->primaryData = $entity;
        return $this;
    }
865934
public function getEntity()
    {
        if (!isset($this->data['_embedded']['entity']) || !isset($this->resourceClass)) {
            throw new \Exception("No entity found in result");
        }

        $data = $this->data['_embedded']['entity'];
        $resourceClass = $this->resourceClass;
        return new $resourceClass($data, $this->baseUrl, $this->client);
    }
171783
protected function getEntity($modelName, $primaryModelClass) {
		if ($this->getConfig(static::CONFIG_PLUGIN) && $modelName !== $primaryModelClass && !strpos($modelName, '.')) {
			$modelName = $this->getConfig(static::CONFIG_PLUGIN) . '.' . $modelName;
		}

		try {
			$table = TableRegistry::get($modelName);
			$entityClassName = $table->getEntityClass();
		} catch (Exception $exception) {
			return '\Cake\ORM\Entity';
		}

		return $entityClassName;
	}
154896
public function updateByPrimary($table, $primaryKeyValue, $column, $value)
    {
        $where = $this->buildWhereForArray($primaryKeyValue);
        $quotedValue = $value === null ? 'null' : $this->pdo->quote($value);

        $sql = "UPDATE
                    {$table}
                SET
                    {$column} = {$quotedValue}
                WHERE
                    {$where}";

        $this->pdo->query($sql);
    }
630778
public function get($key, $defaultValue = null)
    {
        $this->ensureValidEntryIdentifier($key);
        try {
            $rawResult = $this->backend->get($key);
        } catch (\Throwable $throwable) {
            throw new Exception('An exception was thrown in retrieving the key from the cache backend.', 1515193339722, $throwable);
        }
        if ($rawResult === false) {
            return $defaultValue;
        }

        return unserialize($rawResult);
    }
125467
protected function _getOldValues(Entity $entity)
    {
        $config = $this->getConfig();
        $fields = array_merge($config['scope'], [$config['order']]);

        $values = [];
        foreach ($fields as $field) {
            if ($entity->isDirty($field)) {
                $values[$field] = $entity->getOriginal($field);
            } elseif ($entity->has($field)) {
                $values[$field] = $entity->get($field);
            }
        }

        if (count($fields) != count($values)) {
            $primaryKey = $entity->get($this->_table->getPrimaryKey());
            $entity = $this->_table->get($primaryKey, ['fields' => $fields]);
            $values = $entity->extract($fields);
        }

        $order = $values[$config['order']];
        unset($values[$config['order']]);

        return [$order, $values];
    }
77529
public function &getPrimaryAttribute()
    {
        if (count($this->primaryAttributes) != 1) {
            throw new \RuntimeException( 'There is not one primary attribute on the \'' . $this->getName() . '\' entity. (There are ' . count($this->getPrimaryAttributes()) . ')' );
        }

        return $this->primaryAttributes[array_keys($this->primaryAttributes)[0]];
    }