Skip to content

Conversation

@lukasluecke
Copy link
Contributor

@lukasluecke lukasluecke commented Jul 13, 2020

Resolves #3476. Depends on #3550.

Works the same for "default" and autocomplete fields.

AssociationField::new('property')
    ->autocomplete() // optional
    ->modifyQuery(fn(QueryBuilder $queryBuilder) => $queryBuilder
        ->where('entity.id > 20')
        ->orderBy('entity.field', 'DESC')
    );

return $this->getController(DashboardControllerInterface::class, $dashboardControllerFqcn, 'index', $request);
}

public function getCrudController(?string $crudControllerFqcn, ?string $crudAction, Request $request): ?CrudControllerInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$field->setFormTypeOption('attr.data-ea-autocomplete-endpoint-url', $autocompleteEndpointUrl);
} else {
$field->setFormTypeOption('query_builder', static function(EntityRepository $repository) use($field) {
// TODO: should this use `createIndexQueryBuilder` instead, so we get the default ordering etc.?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure about this one yet. We could also work around this, by using Crud::getDefaultSort and applying the conditions directly, but I'm not convinced that that would be better.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, this code generates an error (see #4229) if the $field type doesn't have a "query_builder" option. Which, in practice, if it doesn't inherit of EntityType.

@seb-jean
Copy link
Contributor

Could that solve #3490?

@lukasluecke lukasluecke changed the title Modify association query Add option to modify query of AssociationField (with autocomplete) Jul 14, 2020
javiereguiluz added a commit to javiereguiluz/EasyAdminBundle that referenced this pull request Jul 15, 2020
This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Extract ControllerFactory service

This is a pre-requisite for EasyCorp#3476 (MR EasyCorp#3551) and EasyCorp#3352 (see EasyCorp#3543 (comment) for details).

Also deduplicated a bit of the shared logic between CrudController and DashboardController creation.

Commits
-------

ed63197 Extract ControllerFactory service
@javiereguiluz javiereguiluz added this to the 3.x milestone Jul 15, 2020
javiereguiluz added a commit that referenced this pull request Nov 11, 2020
…tocomplete) (lukasluecke)

This PR was squashed before being merged into the 3.0.x-dev branch.

Discussion
----------

Add option to modify query of AssociationField (with autocomplete)

Resolves #3476. Depends on #3550.

Works the same for "default" and autocomplete fields.

```php
AssociationField::new('property')
    ->autocomplete() // optional
    ->modifyQuery(fn(QueryBuilder $queryBuilder) => $queryBuilder
        ->where('entity.id > 20')
        ->orderBy('entity.field', 'DESC')
    );
```

Commits
-------

4716e81 Add option to modify query of AssociationField (with autocomplete)
@javiereguiluz
Copy link
Collaborator

Lukas, thanks a lot for contributing this feature. I did some very minor tweaks and renames while merging, but I left most of your contribution intact. Thanks!

javiereguiluz added a commit that referenced this pull request Nov 12, 2020
…s (javiereguiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Don't override the query_builder form option in associations

This is related to #3551, which we merged yesterday. One of my apps started failing because it used a custom `query_builder` form option.

Commits
-------

3cc9c8a Don't override the query_builder form option in associations
javiereguiluz added a commit that referenced this pull request Nov 17, 2020
…reguiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Fixed the autocomplete feature of association fields

This is related to #3551. Autocomplete stopped working in one of my apps. The reason is that the autocomplete field is not shown on `index/detail` but only on `edit/new` ... so let's not assume that the page where the field is defined is always `index`.

Commits
-------

de42aa4 Fixed the autocomplete feature of association fields
Yopai added a commit to Yopai/EasyAdminBundle that referenced this pull request Mar 6, 2021
…syCorp#3551

See EasyCorp#4229 :
If the AssociationType is used with a FormType that doesn't have a query_builder option,
Symfony will complain.

As for now, it is just a hack, I suppose there may be a better way to do that.
That's why I voluntarily kept the FQCN during the discussion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter AssociationField results

4 participants