Php 获得一个办公室';儿童';返回';家长';如果存在关系
使用下面的docs示例,我希望能够在查询产品时返回所有功能 因此有效地Php 获得一个办公室';儿童';返回';家长';如果存在关系,php,symfony,doctrine-orm,silex,doctrine-query,Php,Symfony,Doctrine Orm,Silex,Doctrine Query,使用下面的docs示例,我希望能够在查询产品时返回所有功能 因此有效地选择feature.product\u id=product.id的所有产品 但是如果可能的话,我想用面向对象的方法来实现这一点。在教义中,有没有办法以相反的方式匹配这些关系 <?php use Doctrine\Common\Collections\ArrayCollection; /** @Entity **/ class Product { // ... /** * @OneToMany
选择feature.product\u id=product.id的所有产品
但是如果可能的话,我想用面向对象的方法来实现这一点。在教义中,有没有办法以相反的方式匹配这些关系
<?php
use Doctrine\Common\Collections\ArrayCollection;
/** @Entity **/
class Product
{
// ...
/**
* @OneToMany(targetEntity="Feature", mappedBy="product")
**/
private $features;
// ...
public function __construct() {
$this->features = new ArrayCollection();
}
}
/** @Entity **/
class Feature
{
// ...
/**
* @ManyToOne(targetEntity="Product", inversedBy="features")
* @JoinColumn(name="product_id", referencedColumnName="id")
**/
private $product;
// ...
}
在这种情况下,选择一个产品,其中product.id=feature.product\u id将只返回1个结果,假设产品表的id是它的主键,自动递增。您确定没有尝试返回feature.product_id==(特定的product.id)的所有功能吗
如果是后者,
$features = $em->getRepository('MyBundle:Feature')
->findBy(
array('product_id' => $productId)
);
您手中有$productId,一个唯一的整数。如果我错了,请纠正我,但这大致就是您要做的事情:只检索已分配功能的产品,但跳过没有功能的产品。我说的对吗?@JovanPerovic很抱歉延迟了响应Hi,不,我想要所有链接到特定产品的功能。例如,功能是“大小”。我回答中的代码应该返回链接到特定产品的所有功能,其中您有产品实体,因此有产品id。如果这不是您想要的,你可以考虑修改你的问题。离开Jovan的评论,“只检索那些有特征的产品,但是跳过那些没有特征的产品”。