从一个数组的数组的对象检索数据 [英] Retrieving Data from an object of an array of arrays
问题描述
我知道这个问题是更多的数据结构,但因为我在Symfony的做这件事有可能是一个更简单的方法。我有一个递归函数TreeBuilder作为()我想在一些数据调用创建一个层次。说的人的数据库,我想创建一个树状结构,如果他们和父母住在一起。我知道我传递对象的数组功能,但它需要一个数组。我是pretty知道我需要那么其处理对象的数组,但我难倒重写此功能。我不知道如何访问数组的元素来检查的parentid。我知道下面的code是不正确的,但就是我在现在的位置。
控制器:
公共职能的indexAction()
{
$ EM = $这个 - > getDoctrine() - GT; getManager(); $实体= $的EM> getRepository('CompanyMyBundle:组织') - GT;的findAll();
后续代码var_dump($实体);
$树= $这个 - > TreeBuilder作为($实体);
返回阵列(
'实体'=> $树,
);
}
私有函数TreeBuilder作为(AR $,$ PID = NULL)
{
$ OP =阵列();
的foreach($ AR为$项目){
//我知道我有对象的数组
如果($项目['的ParentId'] == $ PID){
$运[$项目['ID'] =阵列(
'街'=> $项目['街'],
'的ParentId'=> $项目['的ParentId']
);
$ =儿童自我:: TreeBuilder作为(AR $,$项目['身份证']);
如果($子女){
$运[$项目['ID'] ['孩子'] = $儿童;
}
}
}
返回$运算; }
从的indexAction()的var_dump($实体):
/export/www/working/symfony/src/Company/MyBundle/Controller/DepController.php:34:
阵列(大小= 60)
0 =>
对象(公司\\ MyBundle \\实体\\组织)[1556]
私人'名称'=>字符串'我'(长度= 46)
私人'街'=>字符串'123芝麻'(长度= 255)
私人'市'=>字符串'Myhometown'(长度= 255)
私人的ParentId'=> INT 0
私人'ID'=> INT 1
1 =>
对象(公司\\ MyBundle \\实体\\组织)[1557]
私人'名称'=>字符串ME2(长度= 46)
私人'街'=>字符串'123芝麻'(长度= 255)
私人'市'=>字符串'Myhometown'(长度= 255)
私人的ParentId'=> INT 1
私人'ID'=> INT 2
如果你需要得到实体数组而不是对象,您将需要使用的主义的水化:
$ EM = $这个 - > getDoctrine() - GT; getManager();
$ orgRepo = $的EM> getRepository('CompanyMyBundle:组织');
$实体= $ orgRepo-> createQueryBuilder('组织')
- > getQuery()
- >的getResult(\\学说\\ ORM \\ AbstractQuery :: HYDRATE_ARRAY);
注意:
我会建议离开实体对象和使用干将:
公共职能的indexAction()
{
$ EM = $这个 - > getDoctrine() - GT; getManager();
$实体= $的EM> getRepository('CompanyMyBundle:组织') - GT;的findAll();
$树= $这个 - > TreeBuilder作为($实体); 返回阵列(
'实体'=> $树,
);
}
私有函数TreeBuilder作为($实体$ PID = NULL)
{
$ OP =阵列();
/ **组织$实体* / //类型提示,如果使用自动完成
的foreach($实体$实体){
如果($实体 - > getParentId()== $ PID){
$运[$实体 - >的getId()] = [
'街'=> $实体 - > getStreet(),
'的ParentId'=> $实体 - > getParentId()
];
$孩子=自:: TreeBuilder作为($实体,$实体 - >的getId());
如果(!空($子女)){
$运[$实体 - > geId()] ['孩子'] = $儿童;
}
}
} 返回$运算;
}
I know this question is more data structures but since I am doing it in Symfony there might be a simpler way. I have a recursive function treeBuilder() I want to call on some data to create a hierarchy. Say a database of people and I want to create a tree structure if they live with their parents. I know I am passing an array of object to the function but it needs to be an array. I am pretty sure I need to rewrite this function so that it handles the the array of object but am stumped. I am not sure how to access the elements of the array to check the parentid. I know the code below is not correct but that is where I am at now.
Controller:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CompanyMyBundle:Org')->findAll();
var_dump($entities);
$tree=$this->treeBuilder($entities);
return array(
'entities' => $tree,
);
}
private function treeBuilder($ar, $pid=null)
{
$op=array();
foreach( $ar as $item ) {
// I know I have an array of objects
if( $item['ParentId'] == $pid ) {
$op[$item['Id']] = array(
'Street' => $item['Street'],
'ParentId' => $item['ParentId']
);
$children = self::treeBuilder( $ar, $item['Id'] );
if( $children ) {
$op[$item['Id']]['children'] = $children;
}
}
}
return $op;
}
var_dump($entities) from indexAction():
/export/www/working/symfony/src/Company/MyBundle/Controller/DepController.php:34:
array (size=60)
0 =>
object(Company\MyBundle\Entity\Org)[1556]
private 'Name' => string 'Me' (length=46)
private 'Street' => string '123 Sesame' (length=255)
private 'City' => string 'Myhometown' (length=255)
private 'ParentId' => int 0
private 'Id' => int 1
1 =>
object(Company\MyBundle\Entity\Org)[1557]
private 'Name' => string 'Me2' (length=46)
private 'Street' => string '123 Sesame' (length=255)
private 'City' => string 'Myhometown' (length=255)
private 'ParentId' => int 1
private 'Id' => int 2
If you need to get entities as arrays instead of objects, you would need to use Doctrine's hydrator:
$em = $this->getDoctrine()->getManager();
$orgRepo = $em->getRepository('CompanyMyBundle:Org');
$entities = $orgRepo->createQueryBuilder('org')
->getQuery()
->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
Note: I would suggest to leave entities as objects and use getters:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CompanyMyBundle:Org')->findAll();
$tree = $this->treeBuilder($entities);
return array(
'entities' => $tree,
);
}
private function treeBuilder($entities, $pid = null)
{
$op = array();
/** Org $entity */ //Type hinting, if you use autocompletion
foreach ($entities as $entity) {
if ($entity->getParentId() == $pid) {
$op[$entity->getId()] = [
'Street' => $entity->getStreet(),
'ParentId' => $entity->getParentId()
];
$children = self::treeBuilder($entities, $entity->getId());
if (!empty($children)) {
$op[$entity->geId()]['children'] = $children;
}
}
}
return $op;
}
这篇关于从一个数组的数组的对象检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!