src/Controller/AppController.php
protected function fetchEntity(string $id) : EntityInterface
    {
        $table = $this->loadModel();
        Assert::isInstanceOf($table, Table::class);

        $primaryKey = $table->getPrimaryKey();
        if (! is_string($primaryKey)) {
            throw new UnsupportedPrimaryKeyException();
        }

        try {
            $entity = $table->find()
                ->where([$table->aliasField($primaryKey) => $id])
                ->enableHydration(true)
                ->firstOrFail();
            Assert::isInstanceOf($entity, EntityInterface::class);

            return $entity;
        } catch (Exception $e) {
            // $id is a UUID, re-throwing the exception as we cannot fetch the record by lookup field(s)
            if (Validation::uuid($id)) {
                throw $e;
            }
        }

        /**
         * Try to fetch record by lookup field(s)
         *
         * @var \Cake\Datasource\EntityInterface
         */
        $entity = $table->find()
            ->applyOptions(['lookup' => true, 'value' => $id])
            ->enableHydration(true)
            ->firstOrFail();

        return $entity;
    }
protected function fetchEntity(string $id) : EntityInterface
    {
        $table = $this->loadModel();
        Assert::isInstanceOf($table, Table::class);

        $primaryKey = $table->getPrimaryKey();
        if (! is_string($primaryKey)) {
            throw new UnsupportedPrimaryKeyException();
        }

        try {
            $entity = $table->find()
                ->where([$table->aliasField($primaryKey) => $id])
                ->enableHydration(true)
                ->firstOrFail();
            Assert::isInstanceOf($entity, EntityInterface::class);

            return $entity;
        } catch (Exception $e) {
            // $id is a UUID, re-throwing the exception as we cannot fetch the record by lookup field(s)
            if (Validation::uuid($id)) {
                throw $e;
            }
        }

        /**
         * Try to fetch record by lookup field(s)
         *
         * @var \Cake\Datasource\EntityInterface
         */
        $entity = $table->find()
            ->applyOptions(['lookup' => true, 'value' => $id])
            ->enableHydration(true)
            ->firstOrFail();

        return $entity;
    }
protected function fetchEntity ( string $ id ) : EntityInterface { $ table = $ this -> loadModel ( ) ; Assert :: isInstanceOf ( $ table , Table :: class ) ; $ primaryKey = $ table -> getPrimaryKey ( ) ; if ( ! is_string ( $ primaryKey ) ) { throw new UnsupportedPrimaryKeyException ( ) ; } try { $ entity = $ table -> find ( ) -> where ( [ $ table -> aliasField ( $ primaryKey ) => $ id ] ) -> enableHydration ( true ) -> firstOrFail ( ) ; Assert :: isInstanceOf ( $ entity , EntityInterface :: class ) ; return $ entity ; } catch ( Exception $ e ) { // $id is a UUID, re-throwing the exception as we cannot fetch the record by lookup field(s) if ( Validation :: uuid ( $ id ) ) { throw $ e ; } } /**
         * Try to fetch record by lookup field(s)
         *
         * @var \Cake\Datasource\EntityInterface
         */ $ entity = $ table -> find ( ) -> applyOptions ( [ 'lookup' => true , 'value' => $ id ] ) -> enableHydration ( true ) -> firstOrFail ( ) ; return $ entity ; }
Fetches entity from database.

Tries to fetch the record using the primary key, if no record found and the ID
value is not a UUID it will try to fetch the record using the lookup fields.
If that fails as well then a record not found exception will be thrown.

@param string $id Entity id
@return \Cake\Datasource\EntityInterface
@throws \Cake\Datasource\Exception\RecordNotFoundException When the record is not found
Fetches entity from database .