#12 MetaForeignKeys() function in v 4.6.8 for Mysql and Mysqli

open
nobody
None
5
2006-01-03
2006-01-03
Anonymous
No

This function is bogged in adodb-mysql.inc.php an
adodb-mysqli.inc.php.

You should replace the code by this one :

-variable "asociation" for "association" in
mysqli.inc.php

- you should use $foreign_keys['$ref_table'] instead
of $foreign_keys[$ref_table] to get different
foreign_keys from the same external table.

------------------------------------------

function MetaForeignKeys( $table, $owner =
FALSE, $upper = FALSE, $associative = FALSE )
{
if ( !empty($owner) ) {
$table = "$owner.$table";
}
$a_create_table = $this->getRow(sprintf
('SHOW CREATE TABLE %s', $table));
if ($associative) $create_sql =
$a_create_table["Create Table"];
else $create_sql = $a_create_table[1];
$matches = array();
if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\)
REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql,
$matches)) return false;

$foreign_keys = array();
$num_keys = count($matches[0]);

for ( $i = 0; $i < $num_keys; $i ++ ) {
$my_field = explode('`, `', $matches[1]
[$i]);
$ref_table = $matches[2][$i];
$ref_field = explode('`, `', $matches[3]
[$i]);
if ( $upper ) {
$ref_table = strtoupper($ref_table);
}

$foreign_keys[$ref_table] = array();
$num_fields = count($my_field);
for ( $j = 0; $j < $num_fields; $j
++ ) {
if ( $associative ) {
$foreign_keys['$ref_table']
[$ref_field[$j]] = $my_field[$j];
} else {
$foreign_keys['$ref_table'][]
= "{$my_field[$j]}={$ref_field[$j]}";
}
}
}
return $foreign_keys;
}
}

Discussion

  • Logged In: NO

    Sorry! I have posted it a little quickly (beginer in
    php ... and in english too).

    The good solution for viewing multiple foreign keys from
    the same external table is simply to comment the line :
    // $foreign_keys[$ref_table] = array();
    If $ref_table has the same values than an other iteration
    passed before, this code was re-initializing the array.
    So, only the last iteration on the same external table is
    memorized.

    function MetaForeignKeys( $table, $owner = FALSE,
    $upper = FALSE, $associative = FALSE )
    {
    if ( !empty($owner) ) {
    $table = "$owner.$table";
    }
    $a_create_table = $this->getRow(sprintf('SHOW
    CREATE TABLE %s', $table));
    if ($associative) $create_sql =
    $a_create_table["Create Table"];
    else $create_sql = $a_create_table[1];
    $matches = array();
    if (!preg_match_all("/FOREIGN KEY \(`(.*?)`\)
    REFERENCES `(.*?)` \(`(.*?)`\)/", $create_sql, $matches))
    return false;

    $foreign_keys = array();
    $num_keys = count($matches[0]);

    for ( $i = 0; $i < $num_keys; $i ++ ) {
    $my_field = explode('`, `', $matches[1][$i]);
    $ref_table = $matches[2][$i];
    $ref_field = explode('`, `', $matches[3][$i]);
    if ( $upper ) {
    $ref_table = strtoupper($ref_table);
    }
    // $foreign_keys[$ref_table] = array();
    $num_fields = count($my_field);
    for ( $j = 0; $j < $num_fields; $j ++ ) {
    if ( $associative ) {
    $foreign_keys[$ref_table][$ref_field
    [$j]] = $my_field[$j];
    } else {
    $foreign_keys[$ref_table][]
    = "{$my_field[$j]}={$ref_field[$j]}";
    }
    }
    }
    return $foreign_keys;
    }
    }