Easyadminbundle: Question: Access to joined column in list view?

Created on 22 Jan 2019  路  5Comments  路  Source: EasyCorp/EasyAdminBundle

I have a "product" entity with an associated "price" table. The price of the product can change over time but when listing the products in easyadmin I want to show the most current price as a separate column which should search- and sortable.

I have already created a custom queryBuilder overriding the "createListQueryBuilder" method.

My question is: How can I add the joined column to my list view? When I add the property with the alias from the query, I just get "INACCESSIBLE" label.

feature help wanted

Most helpful comment

I've added this issue into future features https://github.com/EasyCorp/EasyAdminBundle/projects/1#card-17291836 since there is something that we can to improve here.

(A) Auto sortable: true for compound mapped properties like price.value and price.value.currency if they are valid (Doctrine) mapped properties.

Later, split and add all parts to the query:
https://github.com/EasyCorp/EasyAdminBundle/blob/c06dc8f173dc49412c2d39b185376b6924c57a72/src/Search/QueryBuilder.php#L46-L50

(B) Add a new sortable_field option useful for virtual fields. This sortable_field need accomplish the same criteria as mapped properties to be sortable.

Later, use metadata.sortable_field value here (by default it's the property name to keep BC):
https://github.com/EasyCorp/EasyAdminBundle/blob/c06dc8f173dc49412c2d39b185376b6924c57a72/src/Resources/views/default/list.html.twig#L115

All 5 comments

I think this feature is not supported. You can list related properties with the "dot notation":

# typical case
- { property: 'price' }

# display the 'value' property of the 'price' associated with 'product'
- { property: 'price.value' }

# you can go deeper if needed
- { property: 'price.value.currency' }

But this info is not sortable. Someone with experience in Doctrine should take a look at this: maybe implementing this feature is not complicated and therefore, this is an arbitrary limitation in this bundle.

I don't have time for this, so we'll need some volunteer. Thanks!

- { property: 'price.value' }

Yes, this case is not sortable by default, althrough it is a valid sortable property as long as both parts price and value are valid mapped properties. The first one must be a TO_ONE association, and the second one may be a simple mapped column or an association TO_ONE.

Even if it is possible to detect it, it works on your own now. To achieve this, just add the sortable: true option manually to the configuration and test it.

- { property: 'price.value', sortable: true }

If the property does not meet the conditions mentioned above, or it's composed by three parts price.value.currency, or simply it's a virtual field, then you have to override also the createListQueryBuilder and do something like this:

// ProductController.php
protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
{
    if ('price.value.currency' === $sortField) {
        $sortField = null; // <-- needed to skip auto statements

        return parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter)
            ->leftJoin('entity.price', 'price')
            ->leftJoin('price.value', 'value')
            ->orderBy('value.currency', $sortDirection)
        ;
    }

    return parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
}

I've added this issue into future features https://github.com/EasyCorp/EasyAdminBundle/projects/1#card-17291836 since there is something that we can to improve here.

(A) Auto sortable: true for compound mapped properties like price.value and price.value.currency if they are valid (Doctrine) mapped properties.

Later, split and add all parts to the query:
https://github.com/EasyCorp/EasyAdminBundle/blob/c06dc8f173dc49412c2d39b185376b6924c57a72/src/Search/QueryBuilder.php#L46-L50

(B) Add a new sortable_field option useful for virtual fields. This sortable_field need accomplish the same criteria as mapped properties to be sortable.

Later, use metadata.sortable_field value here (by default it's the property name to keep BC):
https://github.com/EasyCorp/EasyAdminBundle/blob/c06dc8f173dc49412c2d39b185376b6924c57a72/src/Resources/views/default/list.html.twig#L115

Just had to perform this to allow entity.x.y.z sorts.
Is the https://github.com/EasyCorp/EasyAdminBundle/projects/1#card-17291836 link private?

Also I think @yceruto 's implementation of createListQueryBuilder with proper parent calls is much better than the one in https://github.com/javiereguiluz/easy-admin-demo/blob/master/src/Controller/Admin/PurchaseController.php

Was this page helpful?
0 / 5 - 0 ratings