微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Symfony从EAV模型中检索唯一值

如何解决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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。