Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top