protected function _collectJointEntities($sourceEntity, $targetEntities)
    {
        $target = $this->getTarget();
        $source = $this->getSource();
        $junction = $this->junction();
        $jointProperty = $this->_junctionProperty;
        $primary = (array)$target->getPrimaryKey();

        $result = [];
        $missing = [];

        foreach ($targetEntities as $entity) {
            if (!($entity instanceof EntityInterface)) {
                continue;
            }
            $joint = $entity->get($jointProperty);

            if (!$joint || !($joint instanceof EntityInterface)) {
                $missing[] = $entity->extract($primary);
                continue;
            }

            $result[] = $joint;
        }

        if (empty($missing)) {
            return $result;
        }

        $belongsTo = $junction->getAssociation($target->getAlias());
        $hasMany = $source->getAssociation($junction->getAlias());
        $foreignKey = (array)$this->getForeignKey();
        $assocForeignKey = (array)$belongsTo->getForeignKey();
        $sourceKey = $sourceEntity->extract((array)$source->getPrimaryKey());

        $unions = [];
        foreach ($missing as $key) {
            $unions[] = $hasMany->find('all')
                ->where(array_combine($foreignKey, $sourceKey))
                ->andWhere(array_combine($assocForeignKey, $key));
        }

        $query = array_shift($unions);
        foreach ($unions as $q) {
            $query->union($q);
        }

        return array_merge($result, $query->toArray());
    }
protected function _collectJointEntities($sourceEntity, $targetEntities)
    {
        $target = $this->getTarget();
        $source = $this->getSource();
        $junction = $this->junction();
        $jointProperty = $this->_junctionProperty;
        $primary = (array)$target->getPrimaryKey();

        $result = [];
        $missing = [];

        foreach ($targetEntities as $entity) {
            if (!($entity instanceof EntityInterface)) {
                continue;
            }
            $joint = $entity->get($jointProperty);

            if (!$joint || !($joint instanceof EntityInterface)) {
                $missing[] = $entity->extract($primary);
                continue;
            }

            $result[] = $joint;
        }

        if (empty($missing)) {
            return $result;
        }

        $belongsTo = $junction->getAssociation($target->getAlias());
        $hasMany = $source->getAssociation($junction->getAlias());
        $foreignKey = (array)$this->getForeignKey();
        $assocForeignKey = (array)$belongsTo->getForeignKey();
        $sourceKey = $sourceEntity->extract((array)$source->getPrimaryKey());

        $unions = [];
        foreach ($missing as $key) {
            $unions[] = $hasMany->find('all')
                ->where(array_combine($foreignKey, $sourceKey))
                ->andWhere(array_combine($assocForeignKey, $key));
        }

        $query = array_shift($unions);
        foreach ($unions as $q) {
            $query->union($q);
        }

        return array_merge($result, $query->toArray());
    }
protected function _collectJointEntities ( $ sourceEntity , $ targetEntities ) { $ target = $ this -> getTarget ( ) ; $ source = $ this -> getSource ( ) ; $ junction = $ this -> junction ( ) ; $ jointProperty = $ this -> _junctionProperty ; $ primary = ( array ) $ target -> getPrimaryKey ( ) ; $ result = [ ] ; $ missing = [ ] ; foreach ( $ targetEntities as $ entity ) { if ( ! ( $ entity instanceof EntityInterface ) ) { continue ; } $ joint = $ entity -> get ( $ jointProperty ) ; if ( ! $ joint || ! ( $ joint instanceof EntityInterface ) ) { $ missing [ ] = $ entity -> extract ( $ primary ) ; continue ; } $ result [ ] = $ joint ; } if ( empty ( $ missing ) ) { return $ result ; } $ belongsTo = $ junction -> getAssociation ( $ target -> getAlias ( ) ) ; $ hasMany = $ source -> getAssociation ( $ junction -> getAlias ( ) ) ; $ foreignKey = ( array ) $ this -> getForeignKey ( ) ; $ assocForeignKey = ( array ) $ belongsTo -> getForeignKey ( ) ; $ sourceKey = $ sourceEntity -> extract ( ( array ) $ source -> getPrimaryKey ( ) ) ; $ unions = [ ] ; foreach ( $ missing as $ key ) { $ unions [ ] = $ hasMany -> find ( 'all' ) -> where ( array_combine ( $ foreignKey , $ sourceKey ) ) -> andWhere ( array_combine ( $ assocForeignKey , $ key ) ) ; } $ query = array_shift ( $ unions ) ; foreach ( $ unions as $ q ) { $ query -> union ( $ q ) ; } return array_merge ( $ result , $ query -> toArray ( ) ) ; }
Returns the list of joint entities that exist between the source entity
and each of the passed target entities

@param \Cake\Datasource\EntityInterface $sourceEntity The row belonging to the source side
of this association.
@param array $targetEntities The rows belonging to the target side of this
association.
@throws \InvalidArgumentException if any of the entities is lacking a primary
key value
@return array
Returns the list of joint entities that exist between the source entity and each of the passed target entities