Load entity by primary key

@throws \Rio\Exception
@return self
963908
protected function load()
    {
        if ($this->uid === null) {
            throw new Exception('Entity "'. CamelCase::to($this->table) . '" not initialized');
        }

        $statement = $this->adapter->getDriver()->query(
            sprintf(
                "SELECT t0.* FROM `{$this->table}` AS t0 WHERE t0.`%s` = %s",
                $this->primary,
                $this->adapter->getDriver()->quote($this->uid, $this->properties[$this->primary]['type'])
            )
        );

        $row = $statement->fetchAssoc();

        if (false === is_array($row)) {
            throw new Exception('Entity not found "' . CamelCase::to($this->table) . ' #' . $this->uid . '"');
        }

        return $this->fromArray($row);
    }
442782
protected function getPrimaryKey()
    {
        if (!isset($this->values[static::$primary_key])) {
            throw new \LogicException('Primary key not set');
        }

        return $this->current_index ?: $this->values[static::$primary_key];
    }
1225893
protected function initEntity($entityClass, $primaryKey)
    {
        $primaryKey = (array) $primaryKey;

        if (!isset($primaryKey[0])) {
            throw new \InvalidArgumentException('Hash key is required');
        }

        $entity = $this->entityFactory($entityClass)->setHashKey($primaryKey[0]);
        if (isset($primaryKey[1])) {
            $entity->setRangeKey($primaryKey[1]);
        }

        return $entity;
    }
531303
public function load($data)
    {
        foreach ($data as $row) {
            $this->put($row['id'], RequestEntity::createExisting($row));
        }

        return $this;
    }
1271207
public function load($id, Persistence $from_persistence = null)
    {
        if (!$from_persistence) {
            $from_persistence = $this->persistence;
        }

        if (!$from_persistence) {
            throw new Exception(['Model is not associated with any database']);
        }

        if ($this->loaded()) {
            $this->unload();
        }

        if ($this->hook('beforeLoad', [$id]) === false) {
            return $this;
        }

        $this->data = $from_persistence->load($this, $id);
        if (null === $this->id) {
            $this->id = $id;
        }

        $ret = $this->hook('afterLoad');
        if ($ret === false) {
            return $this->unload();
        } elseif (is_object($ret)) {
            return $ret;
        }

        return $this;
    }
401694
public function load(Container $object)
    {
        $pks = [];
        $values = $this->parameters;
        $identifier = $this->identifier;
        foreach ($this->primaryKey as $key) {
            if (isset($object->$key)) {
                $pks[$key] = sprintf('%s.%s = ?', $identifier, $key);
                $values[] = $object->$key;
            } else {
                throw new Exception\PrimaryKey($identifier, $key);
            }
        }
        $fields = $this->fields;
        foreach ($fields as &$field) {
            $field = "$identifier.$field";
        }
        $identifier .= $this->generateJoin($fields);
        $sql = "SELECT %s FROM %s WHERE %s";
        $stmt = $this->getStatement(sprintf(
            $sql,
            implode(', ', $fields),
            $identifier,
            implode(' AND ', $pks)
        ));
        $stmt->setFetchMode(Base::FETCH_INTO, $object);
        $stmt->execute($values);
        $stmt->fetch();
        $object->markClean();
    }
1130589
protected function doInsert()
    {
        /**
         * Run pre-INSERT logic
         */
        $this->beforeInsert();

        $data = $this->toArray();
        /**
         * Execute validator logic
         * Can throw ValidatorException
         */
        $this->assert($data);

        $table = $this->getTable();

        /**
         * Execute the INSERT (this may throw an exception)
         */
        $primaryKey = $table::insert($data);

        /**
         * Normalize the result to an array indexed by primary key column(s)
         */
        $tempPrimaryKey = $table->getPrimaryKey();
        $newPrimaryKey = [current($tempPrimaryKey) => $primaryKey];

        /**
         * Save the new primary key value in object. The primary key may have
         * been generated by a sequence or auto-increment mechanism, and this
         * merge should be done before the afterInsert() method is run, so the
         * new values are available for logging, etc.
         */
        $this->setFromArray($newPrimaryKey);

        /**
         * Run post-INSERT logic
         */
        $this->afterInsert();

        /**
         * Update the "clean" to reflect that the data has been inserted.
         */
        $this->clean = $this->toArray();

        return $newPrimaryKey;
    }
37425
protected function load($entityId)
    {
        $table = $this->getEntityDescriptor()->getTableName();
        $primaryKey = $this->getEntityDescriptor()
            ->getPrimaryKey()
            ->getField();

        return $this->find()
            ->where([
                "{$table}.{$primaryKey} = :id" => [':id' => $entityId]
            ])
            ->first();
    }
509091
public function create(Entity $entity)
    {
        if (!$entity->getValidator()->validate()) {
            throw new Exception\ValidatorException($entity->getValidator());
        }

        $values = $entity->getData();

        $reflection = $entity::getReflection();

        // Prevent to force empty primary property
        if ($reflection->hasPrimary()) {

            $primaryName = $reflection->getPrimaryProperty()->getName();

            if (empty($values[$primaryName])) {
                unset($values[$primaryName]);
            }
        }

        try {
            $primaryValue = $this->query()->insert($values)->run($this->connection);
        } catch (Exception\QueryException $e) {
            throw new Exception\RepositoryException($e->getMessage());
        }

        $this->_saveAssociated($primaryValue, $entity);

        return $primaryValue;
    }
463851
public function duplicate() {
        $this->isNewRecord = true;

        foreach ($this->primaryKey() as $key) {
                    $this->$key = null;
        }

        if ($this->save()) {
            return $this;
        }
        return null;
    }
901689
public function reload()
	{
		$this->_relationships = array();
		$pk = array_values($this->getValuesFor($this->getPrimaryKey()));

		$this->setAttributesViaMassAssignment($this->find($pk)->attributes, false);
		$this->resetDirty();

		return $this;
	}
144485
public static function load() : LogStore
    {
        if (is_null(self::$instance)) {
            self::$instance = new LogStore(Connection::get());
        }

        return self::$instance;
    }
1094989
public function generatePK()
    {
        $format = $this->pkFormat;
        $pk     = $this->_properties['primary_key'];
        if (isset($pk[1]))
            throw new Exception(sprintf(self::E_Composit, get_called_class(), __FUNCTION__), 1);

        $pk     = array_shift($pk);
        if (!$pk)
            throw new Exception(sprintf(self::E_PrimaryKey, get_called_class()), 1);

        $last = (int) $this
            ->select(($format['serial']?
                'right('.$pk.', '.$format['serial'].')':
                $pk).' as last')
            ->order($pk.' desc')
            ->read(1)
            ->last;

        // compile prefix format
        if (preg_match('/\{(?<date>.+)\}/', $format['prefix'], $match))
            $newPK = preg_replace('/\{.+\}/', date($match['date']), $format['prefix']);
        else
            $newPK = $format['prefix'];

        $newPK .= trim(str_pad($last+1, $format['serial'], $format['prefix']?'0':' ', STR_PAD_LEFT));

        return $newPK;
    }
586314
private function index($entity)
    {
        //Get meta info
        $meta = $this->getMeta($entity);

        //Get class name
        $class = $meta->getName();

        //Get the index from the repository
        $index = $this->getRepository($class)->getIndex();

        //Get the loaded node if it's a node
        if($meta instanceof NodeMeta)
        {
            $en = $this->getEverymanNode($entity);
        }

        //Otherwise, get the loaded relation
        else
        {
            $en = $this->getEverymanRelation($entity);
        }

        //Add all indexed properties
        foreach ($meta->getIndexedProperties() as $property)
        {
            $index->add($en, $property->getName(), $property->getValue($entity));
        }

        //Get the primary key
        $pk = $meta->getPrimaryKey();
        $name = $pk->getName();
        $id = $pk->getValue($entity);

        //Add the primary key to the index
        $index->add($en, $name, $id);
    }
1079491
private function loadUnitDataIteratorFromDatabase()
    {
        $result = $this->connection->query(
            sprintf("SELECT * FROM %s ORDER BY unique_id ASC", $this->tableName)
        );

        return new \ArrayIterator($result);
    }
362185
public function load(Entity $entity)
    {
        $sql = Entity\Manager::getInstance()
            ->get($this->getRelatedEntity())
            ->getEntity()->find();
        $relEnt = Entity\Manager::getInstance()
            ->get($this->getRelatedEntity())
            ->getEntity();
        $relationTable = $this->getRelationTable();
        $relPrk = $relEnt->getPrimaryKey();
        $relFnk = $this->getRelatedForeignKey();
        $relTblName = $relEnt->getTableName();
        $entFnk = $this->getForeignKey();
        $entPrk = $entity->getPrimaryKey();
        $clause = "{$relationTable}.{$relFnk} = {$relTblName}.{$relPrk}";
        $sql->join($relationTable, $clause, null, null, Join::JOIN_RIGHT)
            ->where(
                [
                    "{$relationTable}.{$entFnk} = :id" => [
                        ':id' => $entity->$entPrk
                    ]
                ]
            )
            ->limit($this->getLimit());

        return $sql->all();
    }
477673
public function load()
    {
        if (!$this->isRegistered()) {
            if (!$this->exist()) {
                require_once($_SERVER["DOCUMENT_ROOT"] . '/bitrix/modules/main/classes/general/update_client_partner.php');

                if (!\CUpdateClientPartner::LoadModuleNoDemand(
                    $this->getName(),
                    $strError,
                    $this->isBeta() ? 'N' : 'Y',
                    LANGUAGE_ID)
                ) {
                    throw new Exception\ModuleLoadException($strError, $this->getName());
                }
            }
        }

        return $this;
    }