教义 - 如何将结果及其关系提取为数组 [英] Doctrine - How to extract results and their relationships as array

查看:84
本文介绍了教义 - 如何将结果及其关系提取为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,称它为Stones和Stones与Attributes有一个ManyToMany关系。



所以我查询实体获取石头,然后我将其水平转换为数组。

  $ result = $ this-> stoneRepository-> find($ stone_id); 

if(!$ result)
{
return false;
}

$ resultArray = $ this-> doctrineHydrator-> extract($ result);

对于Stone实体,这可以正常工作,但是我注意到连接(Attributes)仍然是对象。

  array(size = 12)
'id'=> int 1
'name'=> string'Agate'(length = 5)
'title'=> string'Title'(length = 5)
'attribute'=>
数组(size = 5)
0 =>
object(Stone\Entity\StAttribute)$ 19 $ b private'id'=> int 2
private'name'=> string'Hay fevor'(length = 9)
private'state'=> boolean true
private'created'=> null
private'modified'=> null
1 =>
object(Stone\Entity\StAttribute)$ 19 $ b private'id'=> int 15
private'name'=> string'Libra'(length = 5)
private'state'=> boolean true
private'created'=> null
private'modified'=> null
2 =>

等。



什么是进程水合属性对象?

解决方案

水合使用与提取相反的数组填充一个对象(实体) / p>

由于您希望以数组格式显示结果集,您应该防止在ORM级别下发生的不必要的水合和提取过程。



尝试使用查询生成器Api 而不是实体存储库的内置 find()方法。这不是一个单行但真正简单和快速的解决方案,它应该是有效的:

  $ qb = $ this-> stoneRepository-> createQueryBuilder( 'S'); 
$ query = $ qb-> addSelect('A')
- > leftJoin('S.attribute','A')
- > where('S.id =:sid')
- > setParameter('sid',(int)$ stone_id)
- > getQuery();

$ resultArray = $ query-> getOneOrNullResult(\Doctrine\ORM\Query :: HYDRATE_ARRAY);

这样,您还将阻止运行对数据库的其他SQL查询来获取关联实体。 (在你的情况下为StAttribute)


I have an entity, call it Stones and Stones has a ManyToMany relationship with Attributes.

So I query the entity to get the Stones and then I hydrate this to convert it into an array.

    $result =  $this->stoneRepository->find($stone_id);

    if ( ! $result )
    {
        return false;
    }

    $resultArray =  $this->doctrineHydrator->extract($result);

This works fine for the Stone entity however I noticed that the join (Attributes) remain as objects.

array (size=12)
  'id' => int 1
  'name' => string 'Agate' (length=5)
  'title' => string 'Title' (length=5)
'attribute' => 
    array (size=5)
      0 => 
        object(Stone\Entity\StAttribute)[1935]
          private 'id' => int 2
          private 'name' => string 'Hay fevor' (length=9)
          private 'state' => boolean true
          private 'created' => null
          private 'modified' => null
      1 => 
        object(Stone\Entity\StAttribute)[1936]
          private 'id' => int 15
          private 'name' => string 'Libra' (length=5)
          private 'state' => boolean true
          private 'created' => null
          private 'modified' => null
      2 => 

etc.

What is the process to hydrate the Attribute objects?

解决方案

Hydration is populating an object (entity) using an array which is opposite of the extraction.

Since you want the resultset in array format, you should prevent unnecessary hydration and extraction process which already occurs in the ORM level under the hood.

Try to use Query Builder Api instead of built-in find() method of the entity repository. This is not a single-line but really straightforward and faster solution, it should work:

$qb = $this->stoneRepository->createQueryBuilder('S');
$query = $qb->addSelect('A')
            ->leftJoin('S.attribute', 'A')
            ->where('S.id = :sid')
            ->setParameter('sid', (int) $stone_id)
            ->getQuery();

$resultArray = $query->getOneOrNullResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

This way, you will also prevent running additional SQL queries against database to fetch associated entities. (StAttribute in your case)

这篇关于教义 - 如何将结果及其关系提取为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆