dopq87915 2014-01-26 14:03
浏览 36
已采纳

结果带有学说:很多未知数据进入它

I'm fetching an object from my database with doctrine and I would like to browse it like I would browse a php array, which should be possible.

Let me show you my entity:

<?php
namespace NRtworks\ChartOfAccountsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * @ORM\Entity
 * @ORM\Table(name="Account")
 */

class Account
{
    /**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */

protected $id;

/**
 * @ORM\Column(type="string", length=100, unique = true)
 */

protected $name;  

 /**
 * @ORM\Column(type="string", length=50)
 */

protected $code;

/**
 * @ORM\OneToMany(targetEntity="Account", mappedBy="parent")
 */

private  $children;

/**
 * @ORM\ManyToOne(targetEntity="Account", inversedBy="children")
 */

private $parent;


public function __construct()
    {
        $this->children = new ArrayCollection();
        $this->parent = new ArrayCollection();

    }

//getter & setter    

?>

This is created into my database and doctrine works fine

Now let's try to fetch:

$COA = $this->getDoctrine()->getRepository('NRtworksChartOfAccountsBundle:Account')->findOneById(1);

   var_dump($COA);

With this, I actually have the object I want. However I also have so much more things, such as Fos_UserBundle stuff which is used for my user entity (in another bundle), but is nowhere related to my entity account used here. Look at var_dump result:

object(NRtworks\ChartOfAccountsBundle\Entity\Account)#408 (5) { ["id":protected]=> int(1) ["name":protected]=> string(5) "BILAN" ["code":protected]=> string(6) "000000" ["children":"NRtworks\ChartOfAccountsBundle\Entity\Account":private]=> object(Doctrine\ORM\PersistentCollection)#409 (9) { ["snapshot":"Doctrine\ORM\PersistentCollection":private]=> array(0) { } ["owner":"Doctrine\ORM\PersistentCollection":private]=> *RECURSION* ["association":"Doctrine\ORM\PersistentCollection":private]=> array(15) { ["fieldName"]=> string(8) "children" ["mappedBy"]=> string(6) "parent" ["targetEntity"]=> string(45) "NRtworks\ChartOfAccountsBundle\Entity\Account" ["cascade"]=> array(0) { } ["orphanRemoval"]=> bool(false) ["fetch"]=> int(2) ["type"]=> int(4) ["inversedBy"]=> NULL ["isOwningSide"]=> bool(false) ["sourceEntity"]=> string(45) "NRtworks\ChartOfAccountsBundle\Entity\Account" ["isCascadeRemove"]=> bool(false) ["isCascadePersist"]=> bool(false) ["isCascadeRefresh"]=> bool(false) ["isCascadeMerge"]=> bool(false) ["isCascadeDetach"]=> bool(false) } ["em":"Doctrine\ORM\PersistentCollection":private]=> object(Doctrine\ORM\EntityManager)#137 (10) { ["config":"Doctrine\ORM\EntityManager":private]=> object(Doctrine\ORM\Configuration)#150 (1) { ["_attributes":protected]=> array(13) { ["entityNamespaces"]=> array(3) { ["NRtworksChartOfAccountsBundle"]=> string(37) "NRtworks\ChartOfAccountsBundle\Entity" ["NRtworksSubscriptionBundle"]=> string(34) "NRtworks\SubscriptionBundle\Entity" ["FOSUserBundle"]=> string(21) "FOS\UserBundle\Entity" } ["metadataCacheImpl"]=> object(Doctrine\Common\Cache\ArrayCache)#159 (3) { ["data":"Doctrine\Common\Cache\ArrayCache":private]=> array(5) { ["DoctrineNamespaceCacheKey[sf2orm_default_3f554f15ae41595a1a142aaf979fc6f613a39d4a606464e7ca2715f45fc7d314]"]=> int(1)        

and it goes on and on until my patience is overflowed.

I tried to ladybug_dump it and it works fine, just that I can't see what's in the children or parent collection. In my twig I can browse it with no problem.

But I need to understand well the structure of result to re-order it.

So does someone know where all this data comes from and why it's here ?

  • 写回答

1条回答 默认 最新

  • doudeng2057 2014-01-26 18:21
    关注

    The "so much more things" you're seeing is related to Doctrine's Collections. These are implemented as ArrayCollection (you instantiate them in the constructor of entities for OneToMany and ManyToMany associations), and PersistentCollection (when an entity is fetched from the database).

    Especially PersistentCollection contains a lot of data Doctrine needs to manage it, like keeping track of what needs to be persisted towards the database when the collection changes.

    I don't really understand what you're trying to accomplish :(

    If you're looking for a convenient way to dump your entities, have a look at Doctrine\Common\Util\Debug::dump().

    If you want to use your entity as an array, you could fetch it as an array using the getArrayResult() method of Doctrine\ORM\Query. Or you could have a look at the Serializer library by Johannes Schmitt.

    update

    The "Fos_UserBundle stuff" you're seeing is an entry "FOSUserBundle" => "FOS\UserBundle\Entity" in an array under "entityNamespaces". This is part of the metadata within collections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题