Add a search form in a manyToMany Relation interface - symfony

How to implement a search form in a many to many relation between entities.
I want to search items from an entity before to add them to my other entity. I am using a long list of items (product) that i need to link to Shops and i can't use a simple listbox to select my items.
I need you to point me to a tutorial or any explaination to deal with this interface problem.
The goal is to use a minimum of javascript

I would suggest to create an view where you could select a category or define your search condition. And a second View where you only display products by the previously selected condition. In your second view you could use an entity Field Type ( http://symfony.com/doc/current/reference/forms/types/entity.html#query-builder ) and provide a custom query for the entities like:
use Doctrine\ORM\EntityRepository;
// ...
$builder->add('users', 'entity', array(
'class' => 'AcmeHelloBundle:Product',
'query_builder' => function(ProductRepository $er) {
return $er->createQueryBuilder('p')
->where('p.category = 1);
},
));
This solution doesn't require JavaScript at all.

I spent lot's of time trying to figure out the best solution to make the compromise between re-usability, performance and ergonomy and i found a nice solution
I did this way :
I created a custom form field that show a collection like entity field type but i pass field names that I want to show in a nice table :
->add('products','reflist',array(
'columns'=>array('name','cost','description'),
'actions'=>array('select'=>true,'remove'=>true),
'entityName'=>'VendorProductBundle:Product',
'searchForm'=> 'Vendor\ProductBundle\Form\ProductSearchType'
));
Then I created a generic searching service that takes in input the entity to search on. The result is sent in a popup paginated.
Finally, I created a controller related to my new field to manage actions like add, remove
Thats's it for the logic.
I can't really share the work since it is really dependent of my framework (depend of search service, layout,etc...)

Related

setQueryBuilder sort by name easy admin

I am developping a Symfony 5.4 application with easy admin (v3.5). I have an entity Article and an entity Category. I have a ManyToMany relationship between my two entities. In my easy admin Article crud controller, I can create new Article entities and add a Category to my entity. Everything works fine. Now what I would like to do is to sort the result given by the select tag for my Category association by name.
In my code i have tried this :
//App\Controller\Admin\ArticleCrudController.php
AssociationField::new('categories')->setQueryBuilder(
function (QueryBuilder $queryBuilder) {
return $queryBuilder->getEntityManager()->getRepository(Category::class)->findBy([], ['name' => 'ASC']);
}
)->setFormTypeOptions([
"by_reference" => false,
]),
based on easy admin association field documention.
When i dump my $queryBuilder line, i do have all my entites sorted by name. But when I open the select tag, they do not appear sorted. (I would expect "Espace métiers" to appear first, then "Gourvernance et pilotage" and so on).
I have noticed that the documentation I am using is for version 4.x and that mine is 3.5 but as I have a result when I dump I am still asking just in case.

Symfony - Discard association field during hydration process of an EntityType querybuilder

My Symfony (3.3) Form EntityType is displayed as a select input and lists all the clients we have in database. The client entity is associated to several other entities using lazy mode.
When the select box is rendered, 204 DB queries are issued. I suspect the form component to call setters against each query result, resulting in the loading of many additional database queries.
We could set association mapping as "EAGER" I guess, or use join('…')->addSelect('…') methods inside the form's querybuilder option to force the datas to be part of the results, but the hydration process still costly when several entities are involved.
As you can see, I tried to use the Doctrine Query HINT, hoping it would solve the problem but It did not change anything.
Then, what is the way to go for such a use case ?
What should I do in order to only get the fields I need to populate the dropdown input ?
Here is what I tried so far:
$builder->add('parent', EntityType::class, [
'class' => Client::class,
,'required' => false
,'multiple' => false
,'query_builder' => function (EntityRepository $er) {
$qb = $er ->createQueryBuilder('c')
// All I want doctrine to fetch are the following fields
->select('PARTIAL c.{id,uuid,name,shortName}');
// I expected this flag to help but it does not change the total amount of queries executed
$qb->getQuery()->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
return $qb;
}
])…
Thank you.
Solved
One of the association is a oneToOne and is the only one which has its mapping "fetch" key set to 'EAGER'.
I expected Doctrine to automatically join and select such an association when using the default EntityType's QueryBuilder, but it does not and I had to explicitly tell the querybuilder to do so (once again, despite the fetch flag set to 'EAGER').
return $qb->select('c, p')->leftJoin('c.param', 'p');
I don't really get the point here on what's going on underneath, still the number of database requests dropped down back to 4 queries.

Sonata admin brainstorm

I have 3 entities, two(with Sonata Admins) of which are referenced in the third through one-to-many relationships which also has extra fields. 1st Entity is Journalist which basically manages journalists, 2nd Entity is Event which manages events. Last admin is JournalistEvent which has Journalists who have indicated they want to cover Event and also includes extra fields like isApproved etc.
I want to create a custom list action in the Event Admin to show journalists attending that event. How do i go about it?
Do I create a custom action on Event Admin or do I create a custom query on Journalist Admin to filter journalists attending the event? I have an idea of the former but the latter seems a better way of creating the solution.
You can first list the events and then you can list the Journalists attending each event by using sonata_type_collection form type. If you want to filter out the event list, you may have to write a custom query for that. You can add the following function to the admin class for that.
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$query
->orderBy($query->getRootAlias().".eventName", "ASC")
->andWhere(
$query->expr()->orX(
$query->expr()->eq($query->getRootAlias().".isActive", 1),
$query->expr()->isNull($query->getRootAlias().".isActive")));
return $query;
}
This is just a sample query. You will have to modify it. Hope it helps.

symfony2 relation between two entities

i am started to learn symfony2, Here i have some basic doubts on entity relations. Totally i have two entities 1.Admission.php and 2.Mstcity.php , just i wanna make relation between these two entities.
mysql Table structure:1.admission= id, name , mst_city_id 2. mst_city = id,city_name .. just i am having simple admission form. in that form i need to load the city_name select box . the relation id is in admission table mst_city_id is foreign key of mst_city table .
admission.mst_city_id=mst_city.id ...... > need city_name by this matching
just help me to understand this process
There's no point for me to repost here the symfony docs, so go ahead and read them HERE There are examples in the docs that show exactly what you want to do.
Assuming you have proper Associations set in place, you can just add the field with entity as widget type
$builder->add('mst_city_id', 'entity', array(
'class' => 'BundleName:mst_city',
'property' => 'city_name',
));
You should check which type of relations you use -One2One, One2Many, Many2Many
Define relations in mappers/annotations and check for owned side
Create form types and bind data from it to entities
Persist and flush entities

Linq to Entities - Drill down filter (Asp.net)

I've been searching for a good way of doing multiple "where" filters on an entity collection from linq. There are lots of sites that use a filter for their searches on the side, like ebay.
The technique used is called a "drill down" filter. Now I'm trying to find the right way of implementing this technique in my 3-tier model working with Linq-to-Entities.
The technique uses the earlier used received entity collection and narrows it down with some kind of filter, but there are multiple filters which can both be applied and removed even within the same "category" of filtering.
Hope somebody finds me the right link to a tutorial or a method of how to use this in a proper way.
In my experience, each "filter" on the side maps to a field in the database. This makes it simple to do a filter:
var result = db.Table
.Where(t => t.Name.Contains(ddlName.Text))
.Where(t => t.Attribute1.Contains(Attribute1.Text));
.Where(t => t.Attribute2.Contains(Attribute2.Text));
Obviously you can substitue .Equals() where it makes sense, I've used this on several webapps with great success. This becomes a bit more trickey when the filters you want do not map directly to fields in your database, but a similar approach can be taken.

Resources