首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony规则将Country作为数组

Symfony规则将Country作为数组
EN

Stack Overflow用户
提问于 2016-08-10 23:19:54
回答 1查看 202关注 0票数 2

有没有可能用Doctrine得到数组的数组?

在本例中,我希望country应该是一个数组。

代码语言:javascript
复制
$user['country']['name'] 

or

$user['country']['cid']

我现在的要求是:

代码语言:javascript
复制
    $query = $this->createQueryBuilder('u')
        ->select('u')
        ->getQuery();
    return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);

我这里只有身份证。

带有yml的教义实体:

用户:

代码语言:javascript
复制
TestBundle\Entity\User:
    type: entity
    table: user
    repositoryClass: TestBundle\Entity\UserRepository
    id:
        uid:
            type: integer
            nullable: false
            unsigned: true
            id: true
            generator:
                strategy: IDENTITY
    fields:
        firstname:
            type: string
            nullable: true
            length: 255
            fixed: false
        lastname:
            type: string
            nullable: true
            length: 255
            fixed: false
        email:
            type: string
            nullable: true
            length: 255
            fixed: false
    oneToOne:
        country:
            targetEntity: Country
            joinColumn:
                name: country
                referencedColumnName: cid
    lifecycleCallbacks: {  }

国家/地区:

代码语言:javascript
复制
TestBundle\Entity\Country:
    type: entity
    table: country
    repositoryClass: TestBundle\Entity\CountryRepository
    id:
        cid:
            type: integer
            nullable: false
            unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        cid:
            type: integer
            nullable: true
            unsigned: false
            options:
                default: 0
        name:
            type: string
            nullable: true
            length: 255
            fixed: false
    lifecycleCallbacks: {  }

这取决于我的例子中的速度,因为我有大约10000个对象。

另一种解决方案是,编写一条带有大量“join”的SQL语句,因为上面的例子只是一个简短的例子,我有大约7个这样的列,比如"group“等等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-14 03:40:40

您可以简单地使用对象,而不是使数组水合。然后,您可以通过应该在模型类中定义的getter来访问Country属性。如果你确实需要一个数组,你可以使用php来填充它们。

但是如果你真的需要速度,你实际上应该考虑使用一个原始的select语句,它可以被优化得比Doctrine可能做的要好得多,不管复杂性如何。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38877513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档