2016-03-24 4 views
2

Doctrine2でAssociationに問題があります。Doctrine2の別のエンティティ経由でアクセスする

私のエンティティのマッピングは次のとおりです。 ブランド:

/** 
    * @ORM\Entity 
    */ 
    class Brand 
    { 
     /** 
     * @ORM\Id 
     */ 
     protected $id; 

     /** 
     * @ORM\Column(type="string") 
     */ 
     protected $name; 

     /** 
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Model", mappedBy="brand") 
     */ 
     protected $models; 

     /** 
     * @ORM\Column(type="boolean") 
     */ 
     protected $isPopular; 
    } 

モデル:

/** 
    * @ORM\Entity 
    */ 
    class Model 
    { 
     /** 
     * @ORM\Id 
     */ 
     protected $id; 

     /** 
     * @ORM\Column(type="string") 
     */ 
     protected $name; 

     /** 
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\VehicleType", inversedBy="models") 
     * @ORM\JoinColumn(name="vehicle_type_id", referencedColumnName="id") 
     */ 
     protected $vehicleType; 

     /** 
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Brand", inversedBy="models") 
     * @ORM\JoinColumn(name="brand_id", referencedColumnName="id") 
     */ 
     protected $brand; 
    } 

VehicleType:私は人気ブランドのエンティティVehicleType配列で取得できますか

/** 
    * @ORM\Entity 
    */ 
    class VehicleType 
    { 
     /** 
     * @ORM\Id 
     */ 
     protected $id; 

     /** 
     * @ORM\Column(type="string") 
     */ 
     protected $name; 

     /** 
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Model", mappedBy="vehicleType") 
     */ 
     protected $models; 
    } 

(ブランド.isPopular = true)? 私のマッピングは間違っていますか?

答えて

0

私はこの

$vehicleTypes = $this->getEm()->getRepository('AppBundle:VehicleType')->findAll(); 

    foreach ($vehicleTypes as $vehicleType) { 
     print ('<p>'.$vehicleType->getName().'</p>'); 
     $brands = $this->getQb() 
      ->select('brand') 
      ->from('AppBundle:Brand', 'brand') 
       ->join('brand.models', 'models') 
      ->where('brand.isPopular = true') 
       ->andWhere('models.vehicleType = :vehicleType') 
      ->setParameter('vehicleType', $vehicleType) 
      ->getQuery() 
      ->getResult() 
     ; 
     foreach ($brands as $brand) { 
      print('<p> - '.$brand->getNameEng().'</p>'); 
     } 
    } 

のように行うことができますしかし、私はDB、VehicleTypeのn個のサイズのn + 1 querysを持っています。確かなDBのクエリーを決定する?

関連する問題