2016-08-10 21 views
2

Doctrineで配列の配列を取得するのはどういうことでしょうか?Symfony Doctrineは配列として国を取得します

この例では、国を配列にする必要があります。

$user['country']['name'] 

or 

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

私の現在の要求:

$query = $this->createQueryBuilder('u') 
     ->select('u') 
     ->getQuery(); 
    return $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); 

私はここでしか同上のを取得します。 YMLと

Doctrineのエンティティ:

ユーザー:

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: { } 

国:

私は周りに10 000オブジェクトのを持っているので、それは、私の例では速度に依存
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: { } 

別の解決策は、など、「グループ」

答えて

1
のように、この例では、上記の一つだけを短縮さ ので、私は、周りに7本の列の持っている「参加する」たくさんで、SQL文を書くこと、だろう

アレイを水和する代わりに、単にオブジェクトを使用することができます。次に、モデルクラスで定義したはずのゲッターを使ってCountry属性にアクセスできます。配列が本当に必要な場合は、PHPを使って配列を埋め込むことができます。

しかし、本当にスピードが必要な場合は、実際には複雑なものではなく、Doctrineよりもはるかに優れた最適化ステートメントを使用することを検討する必要があります。

+0

それは役に立ちましたか?それともあなたの質問にまだ何かが開いていますか? –

関連する問題