Php 递归函数,用于查找MySQL表的所有依赖项
我有一个数据库,其中有两个相关的表。例如,表用户包含系统中的所有用户。 然后我有一个名为User\u friend的索引表,其中包含一个用户和它的朋友之间的关系 我有一个函数loadObject($class,$id),它的调用方式如下:Php 递归函数,用于查找MySQL表的所有依赖项,php,mysql,recursion,Php,Mysql,Recursion,我有一个数据库,其中有两个相关的表。例如,表用户包含系统中的所有用户。 然后我有一个名为User\u friend的索引表,其中包含一个用户和它的朋友之间的关系 我有一个函数loadObject($class,$id),它的调用方式如下: loadObject('User', 1); 并将id=1的用户返回为以下格式的数组: array( 'id' => 1, 'username' => 'My user', // the following array
loadObject('User', 1);
并将id=1的用户返回为以下格式的数组:
array(
'id' => 1,
'username' => 'My user',
// the following array contains all the entries in User_invited
'friends' => [2, 3, 4, 5],
// same for comments
'comments' => [6, 7]
'type' => 'User'
);
我试图提出一个递归函数,它检查id=1的用户,找到所有的朋友(在“朋友”数组中),然后循环遍历每个值,找到这些用户及其朋友,直到它到达链的末尾,而不复制任何条目
这似乎很直截了当。问题是,除了朋友之外,我们还可以与评论、事件和许多其他表格建立其他关系
棘手的部分是,这个函数不仅应该与“User”类一起工作,还应该与我们定义的任何类一起工作
我所做的是使用某种索引数组来定义哪些索引表引用哪些主表
例如:
$dependencies = [
'friends' => 'User'
];
这意味着,当我们找到“friends”键时,我们应该查询“User”表
这是我的密码:
<?php
$class = $_GET['class'];
// if we receive a collection of ids, find each individual object
$ids = explode(",", $_GET['ids']);
// load all main objects first
foreach($ids as $id) {
$error = isNumeric($id);
$results[] = loadObject($class,$id);
}
$preload = $results;
$output = [];
$output = checkPreload($preload);
print json_encode($output);
function checkPreload($preload)
{
$dependencies = [
'comment' => 'Comment',
'friend' => 'User',
'enemy' => 'User',
'google' => 'GoogleCalendarService',
'ical' => 'ICalCalendarService',
'owner' => 'User',
'invited' => 'User'
];
foreach($preload as $key => $object)
{
foreach($object as $property => $values)
{
// if the property is an array (has dependencies)
// i.e. event: [1, 2, 3]
if(is_array($values) && count($values) > 0)
{
// and if the dependency exists in our $dependencies array, find
// the next Object we have to retrieve
// i.e. event => CatchAppCalendarEvent
if(array_key_exists($property, $dependencies))
{
$dependentTable = $dependencies[$property];
// find all the ids inside that array of dependencies
// i.e. event: [1, 2, 3]
// and for each ID load th the object:
// i.e. CatchAppCalendarEvent.id = 1, CatchAppCalendarEvent.id = 2, CatchAppCalendarEvent.id = 3
foreach($values as $id)
{
$dependantObject = loadObject($dependencies[$property], $id);
// if the object doesn't exist in our $preload array, add it and call the
// function again
if(!objectDoesntExist($preload, $dependantObject)) {
$preload[] = $dependantObject;
reset($preload);
checkPreload($preload);
}
}
}
}
}
}
return $preload;
}
// 'id' and 'type' together are unique for each entry in the database
function objectDoesntExist($preload, $object)
{
foreach($preload as $element)
{
if($element['type'] == $object['type'] && $element['id'] == $object['id']) {
return true;
}
}
return false;
}
在一些失败的测试之后,我决定不使用递归方法,而是使用迭代方法
我要做的是从一个元素开始,把它放在一个“队列”(数组)中,找到该元素的依赖项,将它们附加到“队列”中,然后后退一步,重新检查同一个元素,看看是否还有其他依赖项
检查依赖项的函数现在有点不同:
/**
* This is the code function of our DRA. This function contains an array of dependencies where the keys are the
* keys of the object i.e. User.id, User.type, etc. and the values are the dependent classes (tables). The idea
* is to iterate through this array in our queue of objects. If we find a property in one object that that matches
* the key, we go to the appropriate class/table (value) to find more dependencies (loadObject2 injects the dependency
* with it's subsequent dependencies)
*
*/
function findAllDependenciesFor($element)
{
$fields = [
'property' => 'tableName',
...
];
$output = [];
foreach($element as $key => $val) {
if(array_key_exists($key, $fields)) {
if(is_array($val)) {
foreach($val as $id) {
$newElement = loadObject($fields[$key], $id);
$output[] = $newElement;
}
}
else {
// there's been a field conversion at some point in the app and some 'location'
// columns contain text and numbers (i.e. 'unknown'). Let's force all values to be
// and integer and avoid loading 0 values.
$val = (int) $val;
if($val != 0) {
$newElement = loadObject($fields[$key], $val);
$output[] = $newElement;
}
}
}
}
return $output;
}
我还使用与之前相同的函数来检查“队列”是否已经包含该元素(我已将该函数重命名为“objectExists”而不是“ObjectDoesTextist”)。正如您所看到的,我检查类型(表)和id,因为这两个属性的组合对于整个系统/数据库是唯一的
function objectExists($object, $queue)
{
foreach($queue as $element) {
if($object['type'] == $element['type'] && $object['id'] == $element['id']) {
return true;
}
}
return false;
}
最后,主要功能:
// load all main objects first
foreach($ids as $id) {
$error = isNumeric($id);
$results[] = loadObject($class,$id);
}
$queue = $results;
for($i = 0; $i < count($queue); $i++)
{
// find all dependencies of element
$newElements = findAllDependenciesFor($queue[$i]);
foreach($newElements as $object) {
if(!objectExists($object, $queue)) {
$queue[] = $object;
// instead of skipping to the next object in queue, we have to re-check
// the same object again because is possible that it included new dependencies
// so let's step back on to re-check the object
$i--;
}
}
$i++;
}
//首先加载所有主要对象
foreach($id作为$id){
$error=isNumeric($id);
$results[]=loadObject($class,$id);
}
$queue=$results;
对于($i=0;$i
如您所见,我使用常规的“for”而不是“foreach”。这是因为我需要能够在“队列”中向前/向后移动。在一些失败的测试之后,我决定不使用递归方法,而是使用迭代方法
我要做的是从一个元素开始,把它放在一个“队列”(数组)中,找到该元素的依赖项,将它们附加到“队列”中,然后后退一步,重新检查同一个元素,看看是否还有其他依赖项
检查依赖项的函数现在有点不同:
/**
* This is the code function of our DRA. This function contains an array of dependencies where the keys are the
* keys of the object i.e. User.id, User.type, etc. and the values are the dependent classes (tables). The idea
* is to iterate through this array in our queue of objects. If we find a property in one object that that matches
* the key, we go to the appropriate class/table (value) to find more dependencies (loadObject2 injects the dependency
* with it's subsequent dependencies)
*
*/
function findAllDependenciesFor($element)
{
$fields = [
'property' => 'tableName',
...
];
$output = [];
foreach($element as $key => $val) {
if(array_key_exists($key, $fields)) {
if(is_array($val)) {
foreach($val as $id) {
$newElement = loadObject($fields[$key], $id);
$output[] = $newElement;
}
}
else {
// there's been a field conversion at some point in the app and some 'location'
// columns contain text and numbers (i.e. 'unknown'). Let's force all values to be
// and integer and avoid loading 0 values.
$val = (int) $val;
if($val != 0) {
$newElement = loadObject($fields[$key], $val);
$output[] = $newElement;
}
}
}
}
return $output;
}
我还使用与之前相同的函数来检查“队列”是否已经包含该元素(我已将该函数重命名为“objectExists”而不是“ObjectDoesTextist”)。正如您所看到的,我检查类型(表)和id,因为这两个属性的组合对于整个系统/数据库是唯一的
function objectExists($object, $queue)
{
foreach($queue as $element) {
if($object['type'] == $element['type'] && $object['id'] == $element['id']) {
return true;
}
}
return false;
}
最后,主要功能:
// load all main objects first
foreach($ids as $id) {
$error = isNumeric($id);
$results[] = loadObject($class,$id);
}
$queue = $results;
for($i = 0; $i < count($queue); $i++)
{
// find all dependencies of element
$newElements = findAllDependenciesFor($queue[$i]);
foreach($newElements as $object) {
if(!objectExists($object, $queue)) {
$queue[] = $object;
// instead of skipping to the next object in queue, we have to re-check
// the same object again because is possible that it included new dependencies
// so let's step back on to re-check the object
$i--;
}
}
$i++;
}
//首先加载所有主要对象
foreach($id作为$id){
$error=isNumeric($id);
$results[]=loadObject($class,$id);
}
$queue=$results;
对于($i=0;$i
如您所见,我使用的是常规的“for”而不是“foreach”。这是因为我需要能够在“队列”中向前/向后移动.确保objectdoesntexist确实有效。如果无效,您只需不断地将相同的内容插入数组中,并且由于您通过再次传入原始数组进行递归,因此每次递归时基本上都会重新启动整个过程。快速/脏测试:var\u dump($preload)
在循环中,查看循环增长时发生的情况。确保ObjectDoesTextist实际工作。如果不工作,您只需不断地将相同的内容插入数组中,并且由于您通过再次传入原始数组进行递归,因此每次递归时基本上都会重新启动整个过程。快速/脏测试:var\u dump($preload)
在循环内部,并查看它增长时发生了什么。