Cake\ORM\Association\BelongsToMany::_saveLinks PHP Method

    protected function _saveLinks(EntityInterface $sourceEntity, $targetEntities, $options)
    {
        $target = $this->target();
        $junction = $this->junction();
        $entityClass = $junction->entityClass();
        $belongsTo = $junction->association($target->alias());
        $foreignKey = (array) $this->foreignKey();
        $assocForeignKey = (array) $belongsTo->foreignKey();
        $targetPrimaryKey = (array) $target->primaryKey();
        $bindingKey = (array) $this->bindingKey();
        $jointProperty = $this->_junctionProperty;
        $junctionAlias = $junction->alias();
        foreach ($targetEntities as $e) {
            $joint = $e->get($jointProperty);
            if (!$joint || !$joint instanceof EntityInterface) {
                $joint = new $entityClass([], ['markNew' => true, 'source' => $junctionAlias]);
            }
            $sourceKeys = array_combine($foreignKey, $sourceEntity->extract($bindingKey));
            $targetKeys = array_combine($assocForeignKey, $e->extract($targetPrimaryKey));
            if ($sourceKeys !== $joint->extract($foreignKey)) {
                $joint->set($sourceKeys, ['guard' => false]);
            }
            if ($targetKeys !== $joint->extract($assocForeignKey)) {
                $joint->set($targetKeys, ['guard' => false]);
            }
            $saved = $junction->save($joint, $options);
            if (!$saved && !empty($options['atomic'])) {
                return false;
            }
            $e->set($jointProperty, $joint);
            $e->dirty($jointProperty, false);
        }
        return true;
    }