Symfony3 Doctrine select with many-to-many - symfony

I have 3 entities: User, Category (of posts) and Group. Category has allowedGroups field with many-to-many relation with groups. User also has groups. I'm looking for the simplest way show to current user only these categories which has at least one of the user's groups in allowedGroups. I tried to fetch all categories and "manualy" filter them in controller, but it is not perfect way I think. How can I do that?

You could add a function in the user entity which would be getCategories, which would do a for each loop on the $user->getGroups() and array_merge all the categories of each group then use array unique to remove duplicates.

Related

Thinking symfony relationship for set

I have 2 entity product (sigle product: id, name, category and set: id, name). Set is compose with one or more product. I add relation ManyToMany between product and set. Symfony add entity set_product.
I want in product add « product preferential » (of set_product) in case the product is a set.
I don’t know if thinking is correct ? What is the good way ? Thank in advance.
First, if only set is composed by one or more product, you should use OneToMany (set can have one or more product).
I think your thinking with product preferential should work anyway.

Create filter of an attibute and other of a date range

I've a list of items of my Entity. I want to filter it by an attribute as do SonataAdminBundle or other Admin Generator.
For example, if I have an attribute "color" then in my view should have a select field with the types of color and when the user choose one of them my view should only show the list of items filtered by "color"
How do I do it?
Edit:
I have almost solved first filter creating a form and in the controller action get the attribute using the request. Then I used findBy to filter the query.
Now is time of date range filter and merge it with the filter above. I want to filter my list of items by two o three filters.
How do I get from the controller to return only items that belong to a date range?
What is the sentence DQL to use three filters which are optional? I can filter out for none, one or all filters.
You already mentioned the solution: Doctrine Filters
This is what I use if I only want to get a subset of related entities. I think the Documentation is straight forward.

Many-to-many relationship using 2 grids in the same form

I am new to AX, so it could be a simple question.
Imagine two tables Users and Groups in many-to-many relationship:
One user can belongs to many groups.
One group can belongs to many users.
So, I created 3 tables in AX, to represent this relationship:
User
Group
GroupUser (intermediate table used to create the many-to-many relationship with PKs from User and Group tables)
Now imagine I have a form with 2 grids.
Each grid has only one column not editable.
One grid with the Users list (with the User table defined as DataSource) and another with the Group list (with the Group table defined as DataSource).
Everything is displaying fine, but I would like to have the behaviour that when selecting a Group just appears the Users it belongs, and them I can select the user.
How to do this last part ?
Showing users belonging to a group involves using an exists join on the GroupUser table.
It was not clear to me from your description, whether this was the case always or only when a group was selected.
Make a new form datasource using GroupUser with JoinMode set ExistsJoin and linked to the User table.
If only filtered when a group is selected, make the GroupUser datasource disabled:
user_ds.query().dataSourceTable(tableNum(GroupUser)).enabled(<group is selected>);

How to remove the compound PK from a Symfony2 ManyToMany

I need to allow multiple Products to be present in a Cart. I do not want to increase a quantity column, I actually want the same Product entity in the Cart twice. I want to reuse the Product entities and not create a CartProduct intermediary too.
Cart ManyToMany Product
However, the table is created by doctrine:schema:update with a compound primary key of cart_id + product_id. This prevents me adding the same Product twice.
How do I solve this?
This is not the only use-case I have where I need a ManyToMany to support duplicate entries. Is this just not possible with Symfony2/Doctrine?
It's not so much a limitation of doctrine as it is of relational databases. Every row needs to have a unique primary key which, by default in Doctrine 2, would be product_id,cart_id.
The only way around it is to make yourself an explicit CartProduct entity and add at least one more column. Not that hard to do. Just establish OneToMany relations to it from Cart and Product.

Subsonic many-to-many relationship

I have 3 tables, one is called Users, one is called Categories and one is a linking table called User_Categories_Map to link users to categories in a many-to-many relationship. The linking table consists of UserId's and CategoryId's. After generating the subsonic classes, I would assume I'd be able to then type User.singleOrDefault(x => x.ID == 1).Categories to select all the categories for a user. However, this doesn't work. If you can understand what I'm trying to accomplish here, can anyone tell me how I can make this work in subsonic? Consequently, I cannot find any documentation on subsonic. Subsonicproject.com only has a short page a few articles about how to set it up. Is there documentation somewhere for subsonic?
int lUserID =1; // suppose 1 is Id of user
CategoriesCollection lCategories = DB.Select().From<Categories>()
.InnerJoin(User_Categories_Map)
.InnerJoin(Users)
.Where(Users.Columns.Id).IsEqualTo(lUserID)
.ExecuteAsCollection<CategoriesCollection>();
It will return collection of categories associated to a specific user..

Resources