Symfony2 sonata admin bundle list mapping with mongodb odm - mongodb

I have a collection 'Category' which contains its parent and childs in itself.
I am using sonata admin bundle to generate list. And I want is to list the categories in such a way that the default list should contains only the top level categories (i.e. only those categories which do not have parents).
And with a link to sub-categories list all the child categories of that particular parent category.
How could I do that?

First define method createQuery in your admin which restricts query to top parents.
When children are in collection self referenced to entity Category then you can display this collenction in list column.

Related

Finding code for laravel category subcategories task

Create Category Module with Parent and subcategory. Both Category handle in same table. Table name should be category_master with fields id, cat_parent_id, cat_title, cat_status, created-at, updated_at. Than after get all parent category with subcategory in frontend give checkbox the all category. max 3 parent category should be checkable at a time
I want code of this task.

Required Many-to-Many field problem with API

I am wondering if there is a way to create an item in a collection through the API, when that collection has a required Many-to-Many field.
Right now my process for creating an item with the API is to create the item with all regular fields, and then after the create I use the item's ID to create the values in the Many-to-Many field's collection.
However that create item API call fails when that M-M field is required, because there is no value set for that field.
Is there a way to create a collection item, and its many-to-many relationships in a single API call? Or some other workaround?

Getting records through parentID from CategoryCollection in Typo3

I am using CategoryCollection to get the records of a specific category ID, but the problem is it only loads the exact category for e.g I have parent > child and I have attached child category ID to a record and I select child category, then it shows me the record fine, but if I select parentID, then it does not show the child category record.
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
$categoryID,
true,
'tx_myextension_table_name',
'categories'
);
Is there any built-in way to get the records of all child category if I select parent ID from CategoryCollectionor do I have to write something custom for that?
Unfortunately there is no built-in solution for complex selections like this. You will indeed need to write your own logic which could work like this:
Find categories whose parent is your category
Repeat this recursively for every category found until you don't find any children for each category anymore
Do a custom IN() query with the list of category UIDs
If you have deep category trees, the list of category UIDs could be put in a custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be tagged with sys_category. Alternatively you can add a sys_category_<uid> tag for every category UID in your list. This ensures that whenever something changes about one of the categories, the cache entries are dropped and you can rebuild the list.

Symfony2: Collection of dropdown select lists for a many-to-many relationship

The objective:
Having a many-to-many relation be displayed as a dynamic list of select inputs(single choice dropdown list)
User arrives on page with a single select field (multiple = false) populated with persisted entities and add/remove buttons. By clicking the add button, a new select field with the same options appears below the first, which adds a new entry in the M2M relation. By clicking remove the field disappears and the entry should be removed.
The model:
Two entities: User & Manager. A User has exactly one "special" Manager and unlimited normal Managers.
Managers manage unlimited users.To model this I have created two relationships for which the user is the "owner" (not sure how to translate this)
ManyToOne specialManager
ManyToMany normalManagers
I haven't created a many to many relationship with attribute "special" because the requirement is exactly one special manager and I wasn't sure if Symfony/Doctrine would cause problems down the line.
What I have:
I can display a multiple select field with the existing entities using Entity field type, as per the documentation. Functionally this is what I need, visually it is not.
I can also use the Collection field type to display a single text field, and add or remove more with JS, as per the documentation. Visually this is what I need, but The text fields (entity attribute) need to be replaced by choice field.
The question:
Before I continue digging, is there a simple way to achieve this list of select tags?
For anyone else who may eventually need a dynamic list of select fields:
I initially solved this issue by detaching the field(s) in event listeners, and handling the display/submission manually in the controller.
However I wasn't satisfied with this clunky solution and when I encountered the same need I used a second solution: creating an intermediary entity xxxChoice (in this case ManagerChoice) which is Mto1 inversed related to User and Mto1 related to Manager. Then by creating a ManagerChoiceType form with "Manager" entity field type I was able to easily display my collection of dropdown select lists.

How to handle autopopulated selects (from oneToMany relation) in Symfony form?

I have a ProductCategory class that has a parent and some children, properly annotated using Doctrine.
Then I have a ProductCategoryType (form class) that I use to render the html form.
Doctrine does its magic and creates a select box for parent which consists of previously added categories.
My problem: How to I prepend a default option (say '0' => 'No parent category') and how do I remove a particular category from list (ex: the currently edited category, so user can't select the very category to be its own parent)?
This can easily be achieved by using DataTransformers.
You can find more information in the documentation chapter How to use Data Transformers.