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.
我正在使用doctrine从我的数据库中获取一个对象,我想像浏览一个php数组一样浏览它,这应该是可能的。
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
这是创建到我的数据库和doctrine工作正常
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:
有了这个,我实际上有我想要的对象。但是我还有很多东西,例如用于我的用户实体的Fos_UserBundle东西(在另一个包中),但是与我在这里使用的实体帐户无关。查看var_dump结果:
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.
我尝试过ladybug_dump它并且它工作正常,只是因为我看不到孩子或父系列中的内容。在我的树枝上,我可以毫无问题地浏览它。
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 个解决方案
#1
0
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).
你看到的“更多东西”与Doctrine的收藏品有关。这些实现为ArrayCollection(您在OneToMany和ManyToMany关联的实体的构造函数中实例化它们)和PersistentCollection(从数据库中获取实体时)。
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.
特别是PersistentCollection包含Doctrine管理它需要的大量数据,比如跟踪收集更改时需要持久保存到数据库的数据。
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().
如果您正在寻找转储实体的便捷方法,请查看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.
如果要将实体用作数组,可以使用Doctrine \ ORM \ Query的getArrayResult()方法将其作为数组获取。或者你可以看看Johannes Schmitt的Serializer库。
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.
您看到的“Fos_UserBundle内容”是“entityNamespaces”下的数组中的条目“FOSUserBundle”=>“FOS \ UserBundle \ Entity”。这是集合中元数据的一部分。
#1
0
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).
你看到的“更多东西”与Doctrine的收藏品有关。这些实现为ArrayCollection(您在OneToMany和ManyToMany关联的实体的构造函数中实例化它们)和PersistentCollection(从数据库中获取实体时)。
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.
特别是PersistentCollection包含Doctrine管理它需要的大量数据,比如跟踪收集更改时需要持久保存到数据库的数据。
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().
如果您正在寻找转储实体的便捷方法,请查看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.
如果要将实体用作数组,可以使用Doctrine \ ORM \ Query的getArrayResult()方法将其作为数组获取。或者你可以看看Johannes Schmitt的Serializer库。
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.
您看到的“Fos_UserBundle内容”是“entityNamespaces”下的数组中的条目“FOSUserBundle”=>“FOS \ UserBundle \ Entity”。这是集合中元数据的一部分。