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.

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路