Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow map calculated DB field to entity property #6263

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from

Conversation

zorn-v
Copy link
Contributor

@zorn-v zorn-v commented Apr 17, 2024

Imagine that you want to restrict delete action on entities with relation by other entities.
And you don't want/can't use cascade behavior.
In such case you need to do additional query (or maybe much more if relation has fetch eager fields) for every entity in index page.

With proposed fix you can addSelect in createIndexQueryBuilder and it will be mapped correctly.
For example you have Category and Product and want to restrict delete category if it has products.
As bonus you can get product count in category within one query.
Like

// Category entity
public $productsCount;
//Category crud controller
    public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {
        $qb = parent::createIndexQueryBuilder($searchDto, $entityDto, $fields, $filters);

        $qb->leftJoin('entity.products', 'p')->addSelect('COUNT(p.id) AS productsCount');

        return $qb;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant