1

我还是 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”

4

1 回答 1

0

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

我认为您的模型与您要实现的目标不兼容。如果ProductInThemeProductInEvent都是同一种项目的表示,但每个都附加了不同的实体类型,为什么不创建一个实体,在and 和and之间Product创建多对多关系,然后在你的 query_builder 中进行操作?您仍然可以过滤query_builder 中的内容以匹配您想要的内容和。ProductsThemesProductsEventsProductsProductsEventsThemes

TL;DR:除非我误解了你的问题,否则那是概念问题,而不是实施问题。尝试修改模型以更好地满足您的需求。

于 2016-02-01T12:06:32.903 回答