Get the foreign key reference count.

@param \MwbExporter\Model\ForeignKey $fkey   The foreign key
@param int        $max    The maximum count
@return int
427581
protected function getForeignKeyReferenceCount($fkey, $max = null)
    {
        $count = 0;
        $tablename = $fkey->getReferencedTable()->getRawTableName();
        $columns = array();
        foreach ($this->getColumns() as $column) {
            foreach ($column->getForeignKeys() as $foreignKey) {
                // process only unique foreign key
                if (in_array($foreignKey->getId(), $columns)) {
                    continue;
                }
                $columns[] = $foreignKey->getId();
                if ($this->checkForeignKeyOwnerTableName($foreignKey, $tablename)) {
                    $count++;
                }
                if ($max && $count == $max) {
                    break;
                }
            }
        }

        return $count;
    }
1036119
static protected function getMMTableName($ftable, $fkey, $ptable, $pkey, $fConf=null) {
		if ($fConf) {
			list($fclass, $pfkey) = $fConf;
			$self = get_called_class();
			// check for a matching config
			if ($pfkey != $pkey)
				trigger_error(sprintf(self::E_MM_REL_FIELD,
					$fclass.'.'.$pfkey, $self.'.'.$pkey),E_USER_ERROR);
		}
		$mmTable = array($ftable.'__'.$fkey, $ptable.'__'.$pkey);
		natcasesort($mmTable);
		// shortcut for self-referencing mm tables
		if ($mmTable[0] == $mmTable[1] ||
			($fConf && isset($fConf['isSelf']) && $fConf['isSelf']==true))
			return array_shift($mmTable);
		$return = strtolower(str_replace('\\', '_', implode('_mm_', $mmTable)));
		return $return;
	}
1026437
protected function affectedRows($relation, $foreignKey, $foreignKeyIds)
    {
        $relationModel = $relation->getQuery()->getModel();
        $relationModel = new $relationModel();

        return $relationModel->withTrashed()->whereIn($foreignKey, $foreignKeyIds)->count();
    }
631789
protected function getForeignKeyCreate($table, $fkName)
    {
        $foreignKeys = $this->dba->getForeignKeys($table);
        $fkData = $foreignKeys[$fkName];
        $columns = "'" . $fkData['COLUMN_NAME'] . "'";
        $referencedTable = "'" . $fkData['REFERENCED_TABLE_NAME'] . "'";
        $referencedColumns = "'" . $fkData['REFERENCED_COLUMN_NAME'] . "'";
        $options = $this->getForeignKeyOptions($fkData, $fkName);

        $output = [];
        $output[] = sprintf("%s\$this->table(\"%s\")->addForeignKey(%s, %s, %s, %s)->save();", $this->ind2, $table,
            $columns, $referencedTable, $referencedColumns, $options);

        $result = implode($this->nl, $output);

        return $result;
    }
359895
public function getAddForeignKeyDDL(ForeignKey $fk)
    {
        if ($fk->isSkipSql() || $fk->isPolymorphic()) {
            return;
        }
        $pattern = "
ALTER TABLE %s ADD %s;
";

        return sprintf($pattern,
            $this->quoteIdentifier($fk->getTable()->getName()),
            $this->getForeignKeyDDL($fk)
        );
    }
526218
public function hasReferrer(ForeignKey $fk)
    {
        return $this->hasReferrers() && in_array($fk, $this->referrers, true);
    }
320235
protected function buildForeignKeyName($foreignKey, $baseTable)
    {
        return (is_null($foreignKey)) ?
            Inflector::singularize($baseTable).self::DEFAULT_FOREIGN_KEY_SUFFIX :
            $foreignKey;
    }
989961
public function addReferrer(ForeignKey $fk)
    {
        if ($this->referrers === null) {
            $this->referrers = array();
        }
        $this->referrers[] = $fk;
    }
619339
protected function isBelongsToMany($foreignKey)
    {
        $remote = $this->describes[$foreignKey->TABLE_NAME];
        $count = 0;
        foreach ($remote as $field) {
            if ($field->Key == 'PRI') {
                $count++;
            }
        }
        if ($count == 2) {
            return true;
        }

        return false;
    }
218341
public function createForeignKey($foreignKey, $table)
    {
        $changeSetElt = $this->createChangeSet('create-foreign-keys-' . $table->getName());
        $addForeignKeyConstraintElt = $this->document->createElement('addForeignKeyConstraint');

        $this->fillForeignKeyAttributes($addForeignKeyConstraintElt, $foreignKey, $table);

        $changeSetElt->appendChild($addForeignKeyConstraintElt);
        $this->root->appendChild($changeSetElt);
    }
414530
public function count($max = '') {
		$countKey = '0rpHeus_Count';
		$query = $this->getClone(false);
		
		$result = $query->set('what', 'COUNT(*) ' . $countKey)
			->from('(' . $this->getQuery() . ') oq')
			->asArray()->run();
		
		return isset($result[$countKey]) ? $result[$countKey] : 0;
	}
694888
private function getName($foreignKey, $ignoreForeignKeyNames) {
		if ($ignoreForeignKeyNames or $this->isDefaultName($foreignKey)) {
			return null;
		}
		return $foreignKey->getName();
	}
871894
private function isManyToOneRelationConfigured($key)
    {
        $relationConfiguration = $this->getRelationConfigurationFromTca($key);
        $cardinality = isset($relationConfiguration['maxitems']) ? (int)$relationConfiguration['maxitems'] : 1;

        return $cardinality === 1;
    }
886779
public function getForeignKeyDDL(ForeignKey $fk)
    {
        if ($this->supportsForeignKeys($fk->getTable())) {
            return parent::getForeignKeyDDL($fk);
        }

        return '';
    }
439294
public static function getKeyIndex(): int
    {
        if (self::$keyIndex == PHP_INT_MAX) {
            self::$keyIndex = 0;
        } else {
            self::$keyIndex++;
        }

        return self::$keyIndex;
    }
1233335
public function foreignKey($foreignKey, $references = 'id', $unique = false)
    {
        return $this->joinColumn($foreignKey, $references, $unique);
    }
1144410
public function getMostVisitedFromReferences(array $referencesIds, $maxNumber = 10)
    {
        return $this->createQueryBuilder('be')
            ->select('be, COUNT(be.viewReferenceId) AS HIDDEN visits')
            ->groupBy('be.viewReferenceId')
            ->andWhere('be.viewReferenceId in (:ids)')
            ->setParameter('ids', $referencesIds)
            ->setMaxResults($maxNumber)
            ->orderBy('visits', 'DESC');
    }