Bettinz Bettinz - 1 year ago 99
PHP Question

Symfony Entity Type with values from another Entity

I'm still new in Symfony and I'm trying something (I think) common.

I've some entities: product, theme, event, with one to many and many to one connection; so I've other entities called products_in_theme, products_in_event.
A theme is a type of party, that use some kind of products (like colors, paper, etc).
When I made an event, I need all or some of that products, and I need to save that products in products_in_event.
With CraueFormFlowBundle I've created the form (step1: select theme and date, step2: select products).
The problem is that: I need to show in an EntityType "Product in Event" (in order to persist that), values from "Product In Theme".
The query doesn't work because is looking from "product in event" instead only "product in theme"

public function buildForm(FormBuilderInterface $builder, array $options)
$theme= $options['theme'];
->add('products', EntityType::class, array(
'class' => 'AppBundle\Entity\ProductInEvent',
'multiple' => 'true',
'query_builder' => function (EntityRepository $er) use ($theme){
return $er->createQueryBuilder('product_in_event')
->from('AppBundle:ProductInTheme', 'product_in_theme')
->where('product_in_theme.theme = :theme')
->setParameter('theme', $theme);



The query is:

SELECT AS id_0, p0_.price AS price_1, p0_.event_id AS event_id_2, p0_.product_id AS product_id_3
FROM product_in_event p0_, product_in_theme p1_
WHERE p1_.theme_id = 27;`

which returns 0 entries (because product_in_event is empty)

Update 2015/02/01
I've some entities:
-Id, Name, quantity, price

Id, Name, Products (mapped by product_id in product_in_event)

Id, Event_id, Product_id (inversed by products in Event)

Id, Name, Products (mapped by theme_id)

Id, Product_id, Theme_id (inversed by products in theme)

It's a common connection: A product in event have some FK related to Event and Product.

You can view the situation like: Categories and Products. When you buy something, the "product_in_category" became "product_in_order"

Answer Source

If I understood correctly, you want to query a list of ProductInThemes and attach some of those elements to your products attribute in Events (which is supposed to be a list of ProductInEvents). This is definitely impossible, since those two kind of entities are not the same.

I think your model is not compatible with what you are trying to achieve. If ProductInTheme and ProductInEvent are both representation of the same kind of items, but attached with a different entity type each, why don't you create a Product entity, create many-to-many relationships between Products and Themes and between Products and Events, and then manipulate Products in your query_builder? You could still filter those Products in the query_builder to match what you want based on Events and Themes.

TL;DR: Except if I misunderstood your problem, that's a conception problem, not an implementation problem. Try reworking your model to fit your needs a bit better.