Symfony从EAV模型中检索唯一值

问题描述

我正在尝试进行产品过滤,但无法生成正确的查询

А快速查看基础 db visually

这是我的实体:

AttributeType:

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string",length=100,nullable=true)
 */
private $name;

/**
 * @ORM\OnetoMany(targetEntity=AttributeValue::class,mappedBy="attributeType")
 */
private $attributeValue;

public function __construct()
{
    $this->attributeValue = new ArrayCollection();
}

属性值:

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity=Product::class,inversedBy="attributeValues")
 */
private $product;

/**
 * @ORM\Column(type="string",length=100)
 */
private $value;

/**
 * @ORM\ManyToOne(targetEntity=AttributeType::class,inversedBy="attributeValue")
 */
private $attributeType;

例如AttributeType(Color)具有AttributeValue(红色,蓝色,绿色),而我为单个Color选项检索了数百个红色,蓝色,绿色AttributeValue

查询返回具有所有值的选项(不是唯一的):

        return $this->createqueryBuilder('at')
        ->innerJoin('at.attributeValue','attribute_value')
        ->addSelect('attribute_value')
        ->getQuery()
        ->getResult();

我试图这样修改请求:

        return $this->createqueryBuilder('at')
    ->innerJoin('at.attributeValue','attribute_value')
    ->addSelect('attribute_value.value')->distinct()
    ->getQuery()
    ->getResult();

(还有其他尝试,但都没有完成)

如何为每个选项获取唯一值?

感谢您的帮助

还有段时间。

解决方法

我为每个选项获得唯一的值

    public function findOptionsWithUniqueValue()
{
    $result = $this->getEntityManager()->createQueryBuilder()
        ->addSelect('attribute_type.name,attribute_value.value')
        ->distinct()
        ->from(AttributeType::class,'attribute_type')
        ->from(AttributeValue::class,'attribute_value')
        ->andWhere('attribute_type.id = attribute_value.attributeType')
        ->getQuery()
        ->getResult()
        ;

    $out = [];
    while( $a = array_shift($result)) {
        $out[$a['name']][] = $a['value'];
    }

    return $out;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...