Php Doctrine2:如何使用DQL选择ArrayCollection

Php Doctrine2:如何使用DQL选择ArrayCollection,php,mysql,symfony,doctrine-orm,doctrine,Php,Mysql,Symfony,Doctrine Orm,Doctrine,我有两个实体,OneToMany按照条令文档中描述的方式映射: // Product Entity /** * @OneToMany(targetEntity="Feature", mappedBy="product") */ private $features; // Feature Entity /** * @ManyToOne(targetEntity="Product", inversedBy="features") * @JoinColumn(na

我有两个实体,
OneToMany
按照条令文档中描述的方式映射:

 // Product Entity
 /**
  * @OneToMany(targetEntity="Feature", mappedBy="product")
  */
  private $features;

 // Feature Entity    
 /**
  * @ManyToOne(targetEntity="Product", inversedBy="features")
  * @JoinColumn(name="product_id", referencedColumnName="id")
  */
  private $product;
当我在
Product
上执行
findAll()
操作时,它返回带有
功能数组的产品

我正在使用
DQL
进行查询,因此我想知道如何使用
DQL
选择
Feature
的数组

我做到了:
从产品中选择p.name、p.price、f加入p.features f

但它给出了一个错误

如果不选择至少一个根实体别名,则无法通过标识变量选择实体

我也尝试过:
从产品p中选择p.name、p.price、p.features

这也给出了一个错误

无效的路径表达式。必须是StateFieldPathExpression


问题是,如果不获取产品,就无法获取功能实体。所以你必须这么做

SELECT p , f FROM Product p JOIN p.features f

希望这对你有所帮助。

@abdiel给出了正确的解决方案

class ProductsRepository extends \Doctrine\ORM\EntityRepository
{

    public function getAllProductsWithFeatures()
    {
        $dql = 'SELECT p , f FROM Product p JOIN p.features f';
        $query = $this->getEntityManager()->createQuery($dql);
        return $query->getResult();
    }

}
然后,例如在控制器中:

$products = $this->get('doctrine')->getRepository('YourBundle:Product')->getAllProductsWithFeatures();
所有产品都将预装功能集合


祝你好运

你能不能把你的PHP代码也写下来?DQL本身看起来并没有错,但也许您如何称呼它才是问题所在。