在数据库中查找对象的子对象

编程入门 行业动态 更新时间:2024-10-18 21:26:09
本文介绍了在数据库中查找对象的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个实体账户,它是一个有孩子和父母的经典实体。我做的方式,SQL关系只与父母一起确定。

我想要的是为每个对象列出其子项。

在简单的老式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.

更多推荐

在数据库中查找对象的子对象

本文发布于:2023-11-07 06:03:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   数据库中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!