Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

ManyToMany values as columns #700

Open
@stephanvierkant

Description

@stephanvierkant

Let's assume these are my tables:

Product

id description
1 Small car
2 Big car

Property

id property
1 color
2 size

ProductProperty:

id product_id property_id value
1 1 1 red
2 1 2 S
3 2 2 XL

Expected output:

id description color size
1 Small car red S
2 Big car XL

The idea behind this is I have Products with many different Product Types (for example: Cars, Phones, etc.). Instead of having multiple tables with different columns, I've got one Product table and users can add extra properties to each ProductType. A DataTable instance will have products with a specific Product Type, since a table with both cars and phones isn't very useful.

What's the best way to achieve a product list with those extra properties as a column? Sorting and searching should be possible.

A quick 'n dirty solution would be something like this:

foreach ($properties as $property) {
    $this->columnBuilder->add(
        $property->id,
        Column::class,
        [
            'dql' => "(SELECT {p}.value FROM ProductProperty {p} WHERE {p}.product = product.id AND {p}.property = " . $property->id . ")",
            'title' => $property->description)
        ]
    );
}

But that would not allow searching and sorting.

Any ideas how to solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions