Symfony2的:将检索对象数组(和解决倾销变量) [英] Symfony2: Convert retrieved objects to array (and dumping variables for troubleshooting)

查看:387
本文介绍了Symfony2的:将检索对象数组(和解决倾销变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体类别,这是与本身,以形成一树(一类可以具有一个类别作为父母和一个类别可以有一堆类如儿童)。这些被标记为实体内的私人和不暴露于串行

I have an Entity Category, which is linked to itself in order to form a tree (a category can have a category as a parent and a category can have a bunch of categories as children). These are marked as private inside the Entity and not exposed to the serializer.

当我这样做 $类别 - &GT;的getChildren() - GT;的toArray(),给我的孩子们组成的数组,但是当我做<$c$c>$this->getDoctrine()->getRepsitory('PmbLicensing:Category')->findByParent($category)->toArray(),我得到的的toArray()没有定义的错误。我需要使用后者,因为顶级类别有其父设置为空,所以我不能用以前的方法。如何转换在后一种方法获得的数组类别的集合

When I do $category->getChildren()->toArray(), I get an array of the children, but when I do $this->getDoctrine()->getRepsitory('PmbLicensing:Category')->findByParent($category)->toArray(), I get an error that toArray() is not defined. I need to use the latter because the top level categories have their parent set to null, so I cannot use the former method. How do I convert the collection of categories obtained in the latter method to an array?

此外,试图麻烦拍摄的时候,我常常想打印出来的变量,但是当我做类似的print_r($类); 的print_r((阵列)$类); 的var_dump($类); 通话只是运行了大约两分钟,然后返回null 。我想这是因为进入一个infinate循环关系映射,但我怎么阻止这种情况发生?

Also, when trying to trouble shoot, I often would like to print out variables, but when I do something like print_r($categories);, print_r((array)$categories); or var_dump($categories); the call just runs for about two minutes and then returns null. I assume it is because of the relational mapping that goes into an infinate loop, but how do I stop this from happening?

编辑:我要的对象(或对象的集合)转换为数组,因为我想建在哪里提供的类别的儿童的类别可以被检索最多n个递归函数-深度。如果提供的类别可以为null,以从分类的主要水平(与父设置为NULL)检索。这里是我的功能:

I want to convert the object (or collection of objects) to an array, because I want to build a recursive function where the children categories of the supplied category can be retrieved up to n-depth. If the supplied category can be null, in order to retrieve from the main level of categories (with parent set to null). Here is my function:

private function getRecursiveChildren(Category $category = null, $depth, $iteration)
{   
    $children = $this->getDoctrine()->getRepository('PmbLicensingBundle:Category')->findByParent($category);
    // \Doctrine\Common\Util\Debug::dump($children); die();
    if ($depth > $iteration)
        foreach ($children as $child) {
            $child['children'] = $this->getRecursiveChildren($child, $depth, $iteration+1);
    }
    return $children;
}

在有小孩$ ['孩子'],是该行说,我不能用一个对象作为一个数组。

On the line that has $child['children'], is says that I cannot use an object as an array.

推荐答案

其实你的

$children = $this->getDoctrine()->getRepository('PmbLicensingBundle:Category')
                                ->findByParent($category);` 

已返回一个数组,所以你不必(也不能)用 - 方式&gt;的toArray()

当您通过您的类别与

foreach ($children as $child)
    $child["..."] = ...

您正在治疗的obect $子数组[...] 。那是你的错误信息是关于什么的。

You are treating an obect $child like an array with ["..."]. That's what your error message is about.

如果可以的话,你应该使用原则,让它填补了相关子和父类。请参阅<一个href=\"http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#one-to-many-self-referencing\"相对=nofollow称号=学说文档>这个学说文档。然后,你必须自动所有的孩子们,并像 $类别 - &GT访问它们;的getChildren()(这人会返回一个ArrayCollection)。这将节省您大量的工作。

If you can, you should probably use doctrine and let it fill the related child and parent categories. See the Doctrine Documentation on this. Then you have automatically all your Children and can access them like $category->getChildren() (This one will return an ArrayCollection). This will save you a lot of work.

这篇关于Symfony2的:将检索对象数组(和解决倾销变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆