Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 递归查找Yii中的嵌套活动记录关系_Php_Activerecord_Recursion_Yii_Cactiverecord - Fatal编程技术网

Php 递归查找Yii中的嵌套活动记录关系

Php 递归查找Yii中的嵌套活动记录关系,php,activerecord,recursion,yii,cactiverecord,Php,Activerecord,Recursion,Yii,Cactiverecord,假设您有如下关系,并且希望查找给定客户下的所有帐户: - Customer 1 - accounts - Account 1 - customers - Customer 2 - accounts - Account 2 - Account 3 - Customer 3 - customers

假设您有如下关系,并且希望查找给定客户下的所有帐户:

- Customer 1
    - accounts
        - Account 1
    - customers
        - Customer 2
            - accounts
                - Account 2
                - Account 3
        - Customer 3
            - customers
                - Customer 4
                    - accounts
                        - Account 4
                        - Account 5
(对于客户1,它是帐户1、2、3、4和5;对于客户3,它是帐户4和5;等等)

你会怎么做

这在我的项目中出现过几次,我很好奇其他人是如何解决的

以下是(链接中的代码注释):


您是否考虑过使用preorder一次性获取所有详细信息?如果可能的话,将accounts和customers都放在一个表中,那么一个带有preorder的查询实际上会得到您所需要的一切

如果您将客户和帐户保存在两个不同的表中,我相信您仍然可以使用preorder来最小化查询的数量

我在Yii中使用了这个扩展,它也可以保持事物的有序性,这非常好

<?php
public function getNestedRelated($nested, $from, $nestedCriteria = array(), $fromCriteria = array(), $maxLevels = false, $includeFirst = true, $_currentLevel = 0, &$_recursedSoFar = array())
{
    // Always return an array (for array_merge)
    $related = array();

    // Prevent infinite recursion
    if (in_array($this->primaryKey, $_recursedSoFar)) {
        return $related;
    }
    $_recursedSoFar[] = $this->primaryKey;

    // Nested records at this level
    if ($_currentLevel > 0 || $includeFirst) {
        // Whether to refresh nested records at this level. If criteria are
        // provided, the db is queried anyway.
        $refreshNested = false;
        $related       = $this->getRelated($nested, $refreshNested, $nestedCriteria);
    }

    // Handle singular ("HAS_ONE", "BELONGS_TO") relations
    if (!is_array($related)) {
        $related = array($related);
    }

    // Don't recurse past the max # of levels
    if ($maxLevels !== false && $_currentLevel > $maxLevels) {
        return $related;
    }

    // Whether to refresh children of this record. If criteria are provided,
    // the db is queried anyway.
    $refreshFrom = false;

    // Go down one more level
    $_currentLevel++;
    foreach ($this->getRelated($from, $refreshFrom, $fromCriteria) as $child) {
        // Recursive step
        $nestedRelated = $child->getNestedRelated($nested, $from, $nestedCriteria, $fromCriteria, $maxLevels, $includeFirst, $_currentLevel, $_recursedSoFar);
        $related       = array_merge($related, $nestedRelated);
    }
    return $related;
}