public static function findRelations($modelOne, $modelTwo, $keys): array
    {
        $keys = (array)$keys;
        if (!$relations = self::getRelations($modelOne, $modelTwo)) {
            throw new RelationNotFoundException("Relations between model `$modelOne` and `$modelTwo` is not defined");
        }

        /* @var Table $tableOneClass name */
        $tableOneClass = self::getModelClass($modelOne);

        /* @var string $tableOneName */
        $tableOneName = $tableOneClass::getInstance()->getName();

        /* @var Table $tableTwoClass name */
        $tableTwoClass = self::getModelClass($modelTwo);

        /* @var string $tableTwoName */
        $tableTwoName = $tableTwoClass::getInstance()->getName();

        /* @var Query\Select $tableTwoSelect */
        $tableTwoSelect = $tableTwoClass::getInstance()::select();

        // check many to many relation
        if (\is_int(\array_keys($relations)[0])) {
            // many to many relation over third table
            $modelThree = $relations[0];

            // relations between target table and third table
            $relations = self::getRelations($modelTwo, $modelThree);

            /* @var Table $tableThreeClass name */
            $tableThreeClass = self::getModelClass($modelThree);

            /* @var string $tableTwoName */
            $tableThreeName = $tableThreeClass::getInstance()->getName();

            // join it to query
            $tableTwoSelect->join(
                $tableTwoName,
                $tableThreeName,
                $tableThreeName,
                $tableTwoName . '.' . $relations[$modelTwo] . '=' . $tableThreeName . '.' . $relations[$modelThree]
            );

            // relations between source table and third table
            $relations = self::getRelations($modelOne, $modelThree);

            // join it to query
            $tableTwoSelect->join(
                $tableThreeName,
                $tableOneName,
                $tableOneName,
                $tableThreeName . '.' . $relations[$modelThree] . '=' . $tableOneName . '.' . $relations[$modelOne]
            );

            // set source keys
            $tableTwoSelect->where($tableOneName . '.' . $relations[$modelOne] . ' IN (?)', $keys);
        } else {
            // set source keys
            $tableTwoSelect->where($relations[$modelTwo] . ' IN (?)', $keys);
        }
        return $tableTwoSelect->execute();
    }
public static function findRelations($modelOne, $modelTwo, $keys): array
    {
        $keys = (array)$keys;
        if (!$relations = self::getRelations($modelOne, $modelTwo)) {
            throw new RelationNotFoundException("Relations between model `$modelOne` and `$modelTwo` is not defined");
        }

        /* @var Table $tableOneClass name */
        $tableOneClass = self::getModelClass($modelOne);

        /* @var string $tableOneName */
        $tableOneName = $tableOneClass::getInstance()->getName();

        /* @var Table $tableTwoClass name */
        $tableTwoClass = self::getModelClass($modelTwo);

        /* @var string $tableTwoName */
        $tableTwoName = $tableTwoClass::getInstance()->getName();

        /* @var Query\Select $tableTwoSelect */
        $tableTwoSelect = $tableTwoClass::getInstance()::select();

        // check many to many relation
        if (\is_int(\array_keys($relations)[0])) {
            // many to many relation over third table
            $modelThree = $relations[0];

            // relations between target table and third table
            $relations = self::getRelations($modelTwo, $modelThree);

            /* @var Table $tableThreeClass name */
            $tableThreeClass = self::getModelClass($modelThree);

            /* @var string $tableTwoName */
            $tableThreeName = $tableThreeClass::getInstance()->getName();

            // join it to query
            $tableTwoSelect->join(
                $tableTwoName,
                $tableThreeName,
                $tableThreeName,
                $tableTwoName . '.' . $relations[$modelTwo] . '=' . $tableThreeName . '.' . $relations[$modelThree]
            );

            // relations between source table and third table
            $relations = self::getRelations($modelOne, $modelThree);

            // join it to query
            $tableTwoSelect->join(
                $tableThreeName,
                $tableOneName,
                $tableOneName,
                $tableThreeName . '.' . $relations[$modelThree] . '=' . $tableOneName . '.' . $relations[$modelOne]
            );

            // set source keys
            $tableTwoSelect->where($tableOneName . '.' . $relations[$modelOne] . ' IN (?)', $keys);
        } else {
            // set source keys
            $tableTwoSelect->where($relations[$modelTwo] . ' IN (?)', $keys);
        }
        return $tableTwoSelect->execute();
    }
public static function findRelations ( $ modelOne , $ modelTwo , $ keys ) : array { $ keys = ( array ) $ keys ; if ( ! $ relations = self :: getRelations ( $ modelOne , $ modelTwo ) ) { throw new RelationNotFoundException ( "Relations between model `$modelOne` and `$modelTwo` is not defined" ) ; } /* @var Table $tableOneClass name */ $ tableOneClass = self :: getModelClass ( $ modelOne ) ; /* @var string $tableOneName */ $ tableOneName = $ tableOneClass :: getInstance ( ) -> getName ( ) ; /* @var Table $tableTwoClass name */ $ tableTwoClass = self :: getModelClass ( $ modelTwo ) ; /* @var string $tableTwoName */ $ tableTwoName = $ tableTwoClass :: getInstance ( ) -> getName ( ) ; /* @var Query\Select $tableTwoSelect */ $ tableTwoSelect = $ tableTwoClass :: getInstance ( ) :: select ( ) ; // check many to many relation if ( \ is_int ( \ array_keys ( $ relations ) [ 0 ] ) ) { // many to many relation over third table $ modelThree = $ relations [ 0 ] ; // relations between target table and third table $ relations = self :: getRelations ( $ modelTwo , $ modelThree ) ; /* @var Table $tableThreeClass name */ $ tableThreeClass = self :: getModelClass ( $ modelThree ) ; /* @var string $tableTwoName */ $ tableThreeName = $ tableThreeClass :: getInstance ( ) -> getName ( ) ; // join it to query $ tableTwoSelect -> join ( $ tableTwoName , $ tableThreeName , $ tableThreeName , $ tableTwoName . '.' . $ relations [ $ modelTwo ] . '=' . $ tableThreeName . '.' . $ relations [ $ modelThree ] ) ; // relations between source table and third table $ relations = self :: getRelations ( $ modelOne , $ modelThree ) ; // join it to query $ tableTwoSelect -> join ( $ tableThreeName , $ tableOneName , $ tableOneName , $ tableThreeName . '.' . $ relations [ $ modelThree ] . '=' . $ tableOneName . '.' . $ relations [ $ modelOne ] ) ; // set source keys $ tableTwoSelect -> where ( $ tableOneName . '.' . $ relations [ $ modelOne ] . ' IN (?)' , $ keys ) ; } else { // set source keys $ tableTwoSelect -> where ( $ relations [ $ modelTwo ] . ' IN (?)' , $ keys ) ; } return $ tableTwoSelect -> execute ( ) ; }
Find Relations between two tables

@param  string $modelOne Table
@param  string $modelTwo Target table
@param  array  $keys     Keys from first table

@return array
@throws Exception\RelationNotFoundException
Find Relations between two tables