src/Shell/ImportShell.php
protected function _findRelatedRecord(Table $table, string $field, string $value) : string
    {
        $csvField = FieldUtility::getCsvField($table, $field);
        if (null !== $csvField && 'related' === $csvField->getType()) {
            $relatedTable = (string)$csvField->getLimit();
            $value = $this->_findRelatedRecord(
                TableRegistry::get($relatedTable),
                TableRegistry::get($relatedTable)->getDisplayField(),
                $value
            );
        }

        foreach ($table->associations() as $association) {
            if ($association->getForeignKey() !== $field) {
                continue;
            }

            $targetTable = $association->getTarget();

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

            // combine lookup fields with primary key and display field
            $lookupFields = array_merge(
                FieldUtility::getLookup($targetTable),
                [$primaryKey, $targetTable->getDisplayField()]
            );

            // remove virtual/non-existing fields
            $lookupFields = array_intersect($lookupFields, $targetTable->getSchema()->columns());

            // alias lookup fields
            foreach ($lookupFields as $k => $v) {
                $lookupFields[$k] = $targetTable->aliasField($v);
            }

            // populate lookup field values
            $lookupValues = array_fill(0, count($lookupFields), $value);

            $query = $targetTable->find('all')
                ->enableHydration(true)
                ->select([$targetTable->aliasField($primaryKey)])
                ->where(['OR' => array_combine($lookupFields, $lookupValues)]);

            try {
                $entity = $query->firstOrFail();
            } catch (RecordNotFoundException $e) {
                continue;
            }

            Assert::isInstanceOf($entity, EntityInterface::class);

            return $entity->get($primaryKey);
        }

        return $value;
    }
protected function _findRelatedRecord(Table $table, string $field, string $value) : string
    {
        $csvField = FieldUtility::getCsvField($table, $field);
        if (null !== $csvField && 'related' === $csvField->getType()) {
            $relatedTable = (string)$csvField->getLimit();
            $value = $this->_findRelatedRecord(
                TableRegistry::get($relatedTable),
                TableRegistry::get($relatedTable)->getDisplayField(),
                $value
            );
        }

        foreach ($table->associations() as $association) {
            if ($association->getForeignKey() !== $field) {
                continue;
            }

            $targetTable = $association->getTarget();

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

            // combine lookup fields with primary key and display field
            $lookupFields = array_merge(
                FieldUtility::getLookup($targetTable),
                [$primaryKey, $targetTable->getDisplayField()]
            );

            // remove virtual/non-existing fields
            $lookupFields = array_intersect($lookupFields, $targetTable->getSchema()->columns());

            // alias lookup fields
            foreach ($lookupFields as $k => $v) {
                $lookupFields[$k] = $targetTable->aliasField($v);
            }

            // populate lookup field values
            $lookupValues = array_fill(0, count($lookupFields), $value);

            $query = $targetTable->find('all')
                ->enableHydration(true)
                ->select([$targetTable->aliasField($primaryKey)])
                ->where(['OR' => array_combine($lookupFields, $lookupValues)]);

            try {
                $entity = $query->firstOrFail();
            } catch (RecordNotFoundException $e) {
                continue;
            }

            Assert::isInstanceOf($entity, EntityInterface::class);

            return $entity->get($primaryKey);
        }

        return $value;
    }
protected function _findRelatedRecord ( Table $ table , string $ field , string $ value ) : string { $ csvField = FieldUtility :: getCsvField ( $ table , $ field ) ; if ( null !== $ csvField && 'related' === $ csvField -> getType ( ) ) { $ relatedTable = ( string ) $ csvField -> getLimit ( ) ; $ value = $ this -> _findRelatedRecord ( TableRegistry :: get ( $ relatedTable ) , TableRegistry :: get ( $ relatedTable ) -> getDisplayField ( ) , $ value ) ; } foreach ( $ table -> associations ( ) as $ association ) { if ( $ association -> getForeignKey ( ) !== $ field ) { continue ; } $ targetTable = $ association -> getTarget ( ) ; $ primaryKey = $ targetTable -> getPrimaryKey ( ) ; if ( ! is_string ( $ primaryKey ) ) { throw new UnsupportedPrimaryKeyException ( ) ; } // combine lookup fields with primary key and display field $ lookupFields = array_merge ( FieldUtility :: getLookup ( $ targetTable ) , [ $ primaryKey , $ targetTable -> getDisplayField ( ) ] ) ; // remove virtual/non-existing fields $ lookupFields = array_intersect ( $ lookupFields , $ targetTable -> getSchema ( ) -> columns ( ) ) ; // alias lookup fields foreach ( $ lookupFields as $ k => $ v ) { $ lookupFields [ $ k ] = $ targetTable -> aliasField ( $ v ) ; } // populate lookup field values $ lookupValues = array_fill ( 0 , count ( $ lookupFields ) , $ value ) ; $ query = $ targetTable -> find ( 'all' ) -> enableHydration ( true ) -> select ( [ $ targetTable -> aliasField ( $ primaryKey ) ] ) -> where ( [ 'OR' => array_combine ( $ lookupFields , $ lookupValues ) ] ) ; try { $ entity = $ query -> firstOrFail ( ) ; } catch ( RecordNotFoundException $ e ) { continue ; } Assert :: isInstanceOf ( $ entity , EntityInterface :: class ) ; return $ entity -> get ( $ primaryKey ) ; } return $ value ; }
Fetch related record id if found, otherwise return initial value.

@param \Cake\ORM\Table $table Table instance
@param string $field Field name
@param string $value Field value
@return string
Fetch related record id if found otherwise return initial value .