我还是 Symfony 的新手,我正在尝试一些(我认为)常见的东西。
我有一些实体:产品、主题、事件,具有一对多和多对一的连接;所以我还有其他实体,称为 products_in_theme、products_in_event。主题是一种聚会,使用某种产品(如颜色、纸张等)。当我创建一个事件时,我需要所有或部分产品,我需要将这些产品保存在 products_in_event 中。使用CraueFormFlowBundle我创建了表单(步骤 1:选择主题和日期,步骤 2:选择产品)。问题是:我需要在 EntityType“事件中的产品”中显示(为了保持它),来自“主题中的产品”的值。该查询不起作用,因为从“事件中的产品”而不是“主题中的产品”中查找
public function buildForm(FormBuilderInterface $builder, array $options)
{
$theme= $options['theme'];
$builder
->add('products', EntityType::class, array(
'class' => 'AppBundle\Entity\ProductInEvent',
'expanded'=>'true',
'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);
}
));
}
查询是:
SELECT p0_.id 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;`
返回 0 个条目(因为 product_in_event 为空)
Update 2015/02/01
I've some entities:
Product:
-Id, Name, quantity, price
Event:
Id, Name, Products (mapped by product_id in product_in_event)
Product_In_Event:
Id, Event_id, Product_id (inversed by products in Event)
Theme
Id, Name, Products (mapped by theme_id)
Product_in_theme:
Id, Product_id, Theme_id (inversed by products in theme)
这是一个常见的连接:事件中的产品具有与事件和产品相关的一些 FK。
您可以查看以下情况:类别和产品。当你买东西时,“product_in_category”变成了“product_in_order”