SilverStripe filter on one-to-many relations data using filter method is not working - silverstripe

I am working on a SilverStripe project. In my project, I am filtering data using filter method in the relationship (one-to-many). But it is not working.
This is my code.
Order::get()->filter([ 'Items.Status' => "InStock" ]);
That does not work. It is returning all the records instead. How can I apply the filter on relation data instead?

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 form, EntytyType/ChoiceType from 2 different entities

I need to create a dropdown in a Symfony form that contains entities from 2 different tables, for example:
Where EntityA and EntityB are two different classes and MySQL tables, with a different structure.
Is it possible to achieve this?
You should use the choice_loader option of a ChoiceType field.
You can use a CallbackChoiceLoader to load your entities through a closure or implement your own choice loader as a service that you can inject to your form type.
This allows you to build a custom query (or execute two queries in this case) and return the choice list built from the results lazily.
Check the official documentation here (https://symfony.com/doc/current/reference/forms/types/choice.html#choice-loader).

Edit Symfony entity from the frontend

I have a Symfony app using multiple entities.
A third-party analytic tool plugs to my database to create reportings.
What I would like to achieve, is being able to update the Symfony entity from the frontend in order to add new fields to the database tables (in order to get the new fields showing up in the reporting tool).
Anyone has a idea on how to achieve that?
Thanks in advance.
If i understand correctly, you wan't to be able to add fields to your entity dynamicaly.
I don't know if this is doable and if so, it would probably be messy and unsecure.
What you can do however is using sub-entities with dynamic key => values fields.
You should have one main entity, an entity with the list of your dynamic fields, a many to many relation between your main entity and your fields entities and a third entity with the actual values from those fields.

Symfony2 Model reverse DataTransformer on Collection using dynamic form generation and events

I have a Contract entity that has a Collection of TimekeepingEntries.
Normally I would simply create a form mapped to a Contract and add a collection for the TimekeepingEntries.
Unfortunately the form has to be very custom - grouped by user and adapted to a weekday table created inside a loop using a dynamic vars named days.
This is the expected HTML result:
https://gist.github.com/webdevilopers/42c3cdc6013fb09f1c58#file-batch-html
My form will have to create a collectionType timekeepingEntry for each day and for each user even if there is no data from the database.
This can be achieved by using the PRE_SET_DATA event and dynamically adding "sub-forms".
I havn't found a different way to dynamically add the collections that's why I had to use several createNamed methods:
https://gist.github.com/webdevilopers/42c3cdc6013fb09f1c58#file-timekeepingentrybatchtype-php
So far the form works as it gets generated AND populated correctely.
But after submitting I get the following error:
Catchable Fatal Error: Argument 1 passed to Plusquam\Bundle\ContractBundle\Entity\Contract::addTimekeepingEntry() must be an instance of Plusquam\Bundle\ContractBundle\Entity\TimekeepingEntry, array given
Which is correct because I HAVE TO REVERSE TRANSFORM my custom array notation of the dynamic generated "sub-forms" back the collection entities TimekeepingEntry.
Unfortunately the reverseTransform method is not called:
https://gist.github.com/webdevilopers/42c3cdc6013fb09f1c58#file-timekeepingtransformer-php
But this seems to be an expected behaviour as #webmozart aka #bernhard-schussek states:
https://github.com/symfony/symfony/issues/7807
Is there a way to use the reverse Transformer? Is there a better way to create the dynamic collections - maybe using the collection field type?

Add a search form in a manyToMany Relation interface

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...)

Resources