VictorWesterlund / innodb-fk
检索并可选地解决 MySQL/MariaDB InnoDB 数据库中的外键
1.0.3
2023-11-02 13:26 UTC
Requires
This package is auto-updated.
Last update: 2024-10-01 00:20:44 UTC
README
此库通过 InnoDB 存储引擎检索并可选地解决 MySQL/MariaDB 数据库中的外键。
此库仅适用于使用 InnoDB 创建的数据库
使用 composer 安装
composer require victorwesterlund/innodb-fk
use victorwesterlund\ForeignKeys
示例 / 文档
首先使用 mysqli
连接详细信息初始化 ForeignKeys
。 ForeignKeys
将将参数传递给 mysqli::__construct()
。
请记住将存储 InnoDB 外键的数据库名称传递给第 4 个参数。用户还必须具有(第 4 个)$database
参数的 SELECT
权限。这通常是 information_schema
。如果您不确定,也可以传递 ForeignKeys::DATABASE_NAME
常量。
示例数据库关系
初始化 ForeignKeys
use victorwesterlund\ForeignKeys $fk = new ForeignKeys($host, $user, $pass, ForeignKeys::DATABASE_NAME);
获取表的列约束
将数据库和表传递给 for()
,然后链接 get_constraints()
以接收该表的列关系的关联数组。
$fk->for("test", "bar")->get_constraints();
[ // Name of the column that has a foreign key reference "fk" => [ // key is the database and table it references. Value is the column "test.foo" => "id" ] ]
解决实体外键引用
您还可以解决传递的数组数组的外键引用。
从您的数据库检索行并将它们传递给 resolve_all()
作为关联数组的数组以自动解决。
$rows = [ [ "id" => 1, "fk" => 2 ], [ "id" => 2, "fk" => 1 ] ]; $rows = $fk->for("test", "bar")->resolve_all($rows);
// $rows will become [ [ "id" => 1, "fk" => [ "id" => 2, "value" => "lorem ipsum dolor sit amet" ] ], [ "id" => 2, "fk" => [ "id" => 1, "value" => "consectetur adipiscing elit" ] ] ]