src/Koldy/Db/Model.php
public function reload(): Model
    {
        $pk = static::$primaryKey;
        $condition = null;

        if (is_array($pk)) {

            if (count($pk) === 0) {
                $class = get_class($this);
                throw new Exception("Can not reload model of {$class}, primary key definition is incorrect, it can't be empty array");
            }

            $select = [];
            foreach ($pk as $column) {
                if (!$this->has($column)) {
                    $class = get_class($this);
                    throw new Exception("Can not reload model of {$class}, primary key column {$column} is not set in model");
                }

                $pkValue = $this->$column;
                $select[$column] = $pkValue;
            }

            $condition = $select;
            $row = static::select()->where($select)->fetchFirst();
        } else {
            if (!$this->has($pk)) {
                $class = get_class($this);
                throw new Exception("Can not reload model of {$class}, primary key {$pk} is not set in model");
            }

            $pkValue = $this->$pk;
            $condition = [$pk => $pkValue];

            $row = static::select()->where($pk, $pkValue)->fetchFirst();
        }

        if ($row === null) {
            $class = get_class($this);

            $conditions = [];
            foreach ($condition as $key => $value) {
                $conditions[] = "{$key}={$value}";
            }

            $conditions = implode(', ', $conditions);
            throw new Exception("Can not reload model of {$class}, there is no record in database under {$conditions}");
        }

        return $this->setData($row);
    }
public function reload(): Model
    {
        $pk = static::$primaryKey;
        $condition = null;

        if (is_array($pk)) {

            if (count($pk) === 0) {
                $class = get_class($this);
                throw new Exception("Can not reload model of {$class}, primary key definition is incorrect, it can't be empty array");
            }

            $select = [];
            foreach ($pk as $column) {
                if (!$this->has($column)) {
                    $class = get_class($this);
                    throw new Exception("Can not reload model of {$class}, primary key column {$column} is not set in model");
                }

                $pkValue = $this->$column;
                $select[$column] = $pkValue;
            }

            $condition = $select;
            $row = static::select()->where($select)->fetchFirst();
        } else {
            if (!$this->has($pk)) {
                $class = get_class($this);
                throw new Exception("Can not reload model of {$class}, primary key {$pk} is not set in model");
            }

            $pkValue = $this->$pk;
            $condition = [$pk => $pkValue];

            $row = static::select()->where($pk, $pkValue)->fetchFirst();
        }

        if ($row === null) {
            $class = get_class($this);

            $conditions = [];
            foreach ($condition as $key => $value) {
                $conditions[] = "{$key}={$value}";
            }

            $conditions = implode(', ', $conditions);
            throw new Exception("Can not reload model of {$class}, there is no record in database under {$conditions}");
        }

        return $this->setData($row);
    }
public function reload ( ) : Model { $ pk = static :: $ primaryKey ; $ condition = null ; if ( is_array ( $ pk ) ) { if ( count ( $ pk ) === 0 ) { $ class = get_class ( $ this ) ; throw new Exception ( "Can not reload model of {$class}, primary key definition is incorrect, it can't be empty array" ) ; } $ select = [ ] ; foreach ( $ pk as $ column ) { if ( ! $ this -> has ( $ column ) ) { $ class = get_class ( $ this ) ; throw new Exception ( "Can not reload model of {$class}, primary key column {$column} is not set in model" ) ; } $ pkValue = $ this -> $ column ; $ select [ $ column ] = $ pkValue ; } $ condition = $ select ; $ row = static :: select ( ) -> where ( $ select ) -> fetchFirst ( ) ; } else { if ( ! $ this -> has ( $ pk ) ) { $ class = get_class ( $ this ) ; throw new Exception ( "Can not reload model of {$class}, primary key {$pk} is not set in model" ) ; } $ pkValue = $ this -> $ pk ; $ condition = [ $ pk => $ pkValue ] ; $ row = static :: select ( ) -> where ( $ pk , $ pkValue ) -> fetchFirst ( ) ; } if ( $ row === null ) { $ class = get_class ( $ this ) ; $ conditions = [ ] ; foreach ( $ condition as $ key => $ value ) { $ conditions [ ] = "{$key}={$value}" ; } $ conditions = implode ( ', ' , $ conditions ) ; throw new Exception ( "Can not reload model of {$class}, there is no record in database under {$conditions}" ) ; } return $ this -> setData ( $ row ) ; }
Reloads this model with the latest data from database. It's using primary key to fetch the data. If primary key
contains multiple columns, then all columns must be present in the model in order to refresh the data successfully.

@return Model
@throws Exception
@throws Query\Exception
@throws \Koldy\Exception
Reloads this model with the latest data from database . It s using primary key to fetch the data . If primary key contains multiple columns then all columns must be present in the model in order to refresh the data successfully .