【问题标题】:Triple level associations in phpactiverecordphpactiverecord 中的三级关联
【发布时间】:2012-04-02 21:04:13
【问题描述】:

现在,如果我尝试在 phpactiverecord 中预先加载两个以上的对象,我会收到一个错误。

是这样的:

$conditions['include'] = array( 'playlists' => array('playlist_songs' =>array('song')));
User::find('first', $conditions);

只是一个级别太多而无法尝试检索? 每当我尝试使用 3 级深度的关联时,我都会收到错误 Undefined offset: 0。 感谢您提供任何帮助或见解:D。

编辑:

所以我发现了一个有点奇怪的模式。 如果我有array('playlist_songs'=>array('song'=>array('album')));,那么急切的负载将为我打破。但是,如果我向数组添加另一个关联,它就会正常工作。 array('playlist_songs'=>array('song','song','song','song'=>array('album')));

我在该数组中多次使用“歌曲”只是为了使修复非常明显。

【问题讨论】:

    标签: associations eager-loading phpactiverecord


    【解决方案1】:

    只是通过更改执行_eager_load 中 Table.php 的第 247 行来为自己解决这个问题

    if (is_array($name))
    {
                $nested_includes = count($name) > 1 ? $name : $name[0];//<-- this line
                $name = $index;
    }
    

    if (is_array($name))
    {
                $nested_includes = count($name) > 0 ? $name : $name[0];
                $name = $index;
    }
    

    【讨论】:

    • 这是救命的一半!我会在今天或明天的某个时间对其进行一些测试。
    • 那应该是头发*。回想起来,那个评论看起来很不恰当……
    • 很高兴看到您将其放入回购 Anther,感谢您的跟进!
    【解决方案2】:

    这应该可行。我刚刚测试了我自己的代码的一个示例:

    $model = MyItem::find('first',array('include' => array('Group' => array('SomeGroupFeature'))));
    

    我能想到的唯一一件事是您在其中一件事上没有任何结果,或者您的条件包含以前使用数组的其他内容?

    【讨论】:

    • 为了让您的示例与问题完全匹配,包含数组需要如下所示:array('include' =&gt; array('Group' =&gt; array('SomeGroupFeature' =&gt;array('third_thing')))) ;)
    • 我将进行更多测试,看看它是否确实与空结果有关,因为我发现它确实按预期工作的情况......
    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多