Symfony 实体类型,具有来自另一个实体的值


Symfony Entity Type with values from another Entity

我仍然是Symfony的新手,我正在尝试一些(我认为(常见的东西。

我有一些实体:产品,主题,事件,具有一对多和多对一的连接;所以我有其他实体称为products_in_theme,products_in_event。主题是一种聚会,使用某种产品(如颜色、纸张等(。当我举办活动时,我需要全部或部分产品,并且我需要将该产品保存在products_in_event中。使用CraueFormFlowBundle,我创建了表单(步骤1:选择主题和日期,步骤2:选择产品(。问题是:我需要在实体类型"事件中的产品"中显示(为了保持它(,来自"主题中的产品"的值。查询不起作用,因为从"事件中的产品"而不是"主题中的产品"查看

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">

如果我理解正确,您想查询ProductInThemes列表并将其中一些元素附加到 Events 中的 products 属性(这应该是ProductInEvents的列表(。这绝对是不可能的,因为这两种实体是不一样的。

我认为您的模型与您想要实现的目标不兼容。如果ProductInThemeProductInEvent都是同一类型项的表示形式,但分别附加了不同的实体类型,为什么不创建一个Product实体,在ProductsThemes之间以及ProductsEvents之间创建多对多关系,然后在query_builder中操作Products?您仍然可以过滤query_builder中的这些Products,以根据EventsThemes匹配所需的内容。

TL;DR:除非我误解了你的问题,否则这是一个概念问题,而不是实现问题。尝试重新设计模型以更好地满足您的需求。