I'm trying to implement my own ajax select2 to prevent big results for a relational fields, but they are loaded by default(I dont know is this error is from symfony or from easyadmin)
How can I limit this on the first form render? (I want an empty select wihtout execute the default search)
:+1: Related question how to use the query_builder option for an EntityType ? It expects a callable.
An Exception was thrown while handling: The option "query_builder" with value array is expected to be of type "null" or "callable" or "Doctrine\ORM\QueryBuilder", but is of type "array".
There are two ways to do this without using the object instance:
// An example callback method
class MyClass {
public static function myCallbackMethod() {
echo 'Hello World!';
}
}
// Type 1: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));
// Type 2: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');
Both callables.
An example for EasyAdmin:
easy_admin:
entities:
Product:
class: AppBundle\Entity\Product
form:
fields:
- 'name'
- property: 'category'
type_options:
query_builder: 'AppBundle\Repository\CategoryRepository::available'
Also can be:
# ...
query_builder: ['AppBundle\Repository\CategoryRepository', 'available']
Then, create a new class or use an existing one. Personally I prefer to use the entity repository class for that:
class CategoryRepository extends EntityRepository
{
public static function available(CategoryRepository $repository)
{
return $repository->createQueryBuilder('c')
->where("c.enabled IS TRUE")
->orderBy('c.name');
}
}
Remember that this method has to be static.
@rubengc if you want an empty select without execute the default search you can returns a QueryBuilder with empty result.
For example:
class CategoryRepository extends EntityRepository
{
public static function available(CategoryRepository $repository)
{
return $repository->createQueryBuilder('c')
->where("FALSE IS TRUE");
}
}
@yceruto Thanks !
I think this issue can be closed @rubengc , it worked for me.
Thanks guys, I have solved this yet implementing an ajax configuration and using easyadmin findBy for builder, but I forgot say here xD
@javiereguiluz if easyadmin supports select2 by default, would be awesome (and easy to do) an ajax implementation of this component
After @javiereguiluz response I will close this
I'm closing this as "solved" and I've opened #1043 to start discussing about the new easyadmin_autocomplete form field. Thanks!
Most helpful comment
There are two ways to do this without using the object instance:
Both callables.
An example for EasyAdmin:
Also can be:
Then, create a new class or use an existing one. Personally I prefer to use the entity repository class for that:
Remember that this method has to be
static.