我想要的是为每个对象列出其子项。
在简单的老式PHP中,我只需循环一个mysql_fetch_array来拥有我所有的帐户,并将每个请求再次发送到其中parent = id的DB,并将其放在我的帐户对象的属性children(数组)中。
在Symfony2 / doctrine,似乎我不能这样做,至少不是那么简单。
我该怎么办? >
编辑: 在我的控制器中,这是我想做的:
$ COA = $ this-> getDoctrine() - > getRepository('NRtworksChartOfAccountsBundle:Account') - > findAll(); foreach($ COA as $ one_account) { echo $ one_account.getChildren(); }但这不行。当我将这个$ COA传递给我的树枝时,我可以循环使用它,但不能在PHP中。
<?php 命名空间NRtworks\ChartOfAccountsBundle\Entity; 使用Doctrine\ORM\Mapping作为ORM; 使用Doctrine\Common\Collections\ArrayCollection; / ** * @ ORM\Entity * @ ORM\Table(name =Account) * / class帐户 { / ** * @ ORM\Id * @ ORM\Column(type =integer) * @ ORM\GeneratedValue(strategy =AUTO) * / protected $ id; / ** * @ ORM\Column(type =string,length = 100,unique = true) * / 受保护的$ name; / ** * @ ORM\Column(type =string,length = 50) * / protected $ code; / ** * @ ORM\OneToMany(targetEntity =Account,mappedBy =parent) * / private $儿童; / ** * @ ORM\ManyToOne(targetEntity =Account,inversedBy =children) * / private $父母; public function __construct() { $ this-> children = new ArrayCollection(); } // getter& setter ?>解决方案
答案 p>
你可以简单地做:
$ children = $ account->的getChildren();然后循环这些孩子以获取他们的孩子等等...
或者你可以使用 DQL 。将其放在自定义存储库:
public function findByParent(Account $ account) { $ dql ='SELECT a FROM帐户a WHERE a.parent =:parentId'; $ q = $ this-> getEntityManager() - > createQuery($ dql); $ q-> setParameter('parentId',$ account-> getId()); return $ q-> getResult(); }然后(在控制器中)可以执行以下操作:
$ children = $ em-> getRepository('Account') - > findByParent($ parent);然后循环这些孩子以获取他们的孩子等等...
某些建议
此过程不是很有效,特别是当树变大时。 / p>
你应该看看 l3pp4rd的原则扩展的树行为。它使用一个不同的设置,您可以只用一个查询来获取整个树。
PS:如果您使用Symfony 2,可以使用 StofDoctrineExtensionsBundle 以整合这些教义扩展。
您的修改回答
$ one_account.getChildren()应为 $ one_account-> getChildren()。 php中的点(。)用于连接字符串。
I have an entity account which is a classic entity with children and parents. The way I did it, the SQL relation is identified only with the parents.
What I want is to have for each object the list of its children.
In plain old PHP, I would simply loop on a mysql_fetch_array to have all my accounts, and for each one request again to the DB where parent = id so and put that in the property children (array) of my account object.
In Symfony2/doctrine, it seems I cannot do that, at least not that simply.
How am I gonna do then ?
edit: In my controller this is what I would like to do:
$COA = $this->getDoctrine()->getRepository('NRtworksChartOfAccountsBundle:Account')->findAll(); foreach($COA as $one_account) { echo $one_account.getChildren(); }But that doesn't work. When I pass this $COA to my twig I can loop on it but not in the PHP.
<?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(); } //getter & setter ?>解决方案
The answer
You can simply do:
$children = $account->getChildren();Then loop over these children to get their children, etc, etc...
Or you can use DQL. Put this in a custom repository:
public function findByParent(Account $account) { $dql = 'SELECT a FROM Account a WHERE a.parent = :parentId'; $q = $this->getEntityManager()->createQuery($dql); $q->setParameter('parentId', $account->getId()); return $q->getResult(); }Then (in a controller for example) you can do:
$children = $em->getRepository('Account')->findByParent($parent);Then loop over these children to get their children, etc, etc...
Some advice
This process isn't very efficient, especially when the tree gets large.
You should take a look at the Tree behavior of l3pp4rd's Doctrine Extensions. It uses a different setup with which you can fetch an entire tree with only 1 query.
PS: If you're using Symfony 2, you can use the StofDoctrineExtensionsBundle to integrate these Doctrine Extensions.
Answer to your edit
$one_account.getChildren() should be $one_account->getChildren(). A dot (.) in php is used to concatenate strings.
更多推荐
在数据库中查找对象的子对象
发布评论