使用自定义查询构建器的ManyToMany实体的Symfony表单

时间:2015-04-28 19:27:50

标签: php mysql symfony symfony-forms dql

错误消息如下所示:

  

[语义错误]第0行,第58行附近'events =:eventId':   错误:无效的PathExpression。 StateFieldPathExpression或   SingleValuedAssociationField预期。

哪个是对的,因为它是ManyToMany连接,带有连接表。但是我如何加载这些实体?

问题出在活动表格中(最后)。我尝试将分配有ManyToMany联接的城镇加载到特定事件。 代码说超过1000个单词,所以看看(:

所以我在Symfony应用程序中有三个实体。 EventTownActivity

Event看起来像这样:

// ...

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 * @Gedmo\Versioned
 */
private $name;

/**
 * @ORM\ManyToMany(targetEntity="Town", inversedBy="events")
 * @ORM\JoinTable(name="Events_to_Towns")
 **/
private $towns;

/**
 * @ORM\OneToMany(targetEntity="Activity", mappedBy="event")
 */
private $activitys;

// ...

Town看起来像这样:

// ...

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 * @Gedmo\Versioned
 */
private $name;

/**
 * @ORM\ManyToMany(targetEntity="Event", mappedBy="towns")
 */
private $events;

/**
 * @ORM\OneToMany(targetEntity="Activity", mappedBy="town")
 */
private $activitys;

// ...

Activity看起来像这样:

// ...

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 * @Gedmo\Versioned
 */
private $name;

/**
 * @ORM\ManyToOne(targetEntity="Town", inversedBy="activitys")
 * @ORM\JoinColumn(name="town_id", referencedColumnName="id")
 */
private $town;

/**
 * @ORM\ManyToOne(targetEntity="Event", inversedBy="activitys")
 * @ORM\JoinColumn(name="event_id", referencedColumnName="id")
 */
private $event;

// ...

这一切都有效。但是,当我想分配Activity或多个Town时,问题就出现在// ... public function __construct($eventId) { $this->_eventId = $eventId; } public function buildForm(FormBuilderInterface $builder, array $options) { $eventId = $this->_eventId; $builder // ... // this work's as expected! ->add('category', 'entity', array( 'required' => true, 'label' => 'Kategorie', 'class' => 'FooEventBundle:Category', 'property' => 'name', 'query_builder' => function(\Foo\EventBundle\Entity\CategoryRepository $er) use ($eventId) { return $er->createQueryBuilder('c') ->select('c') ->where('c.event = :eventId') ->setParameter('eventId', $eventId) ; } )) // here's the problem! ->add('town', 'entity', array( 'required' => true, 'label' => 'Stadt', 'class' => 'FooEventBundle:Town', 'property' => 'name', 'query_builder' => function(\Foo\EventBundle\Entity\TownRepository $er) use ($eventId) { return $er->createQueryBuilder('t') ->select('t') ->where('t.events = :eventId') ->setParameter('eventId', $eventId) ; } )) // ... ; } // ... 的格式中。我尝试只加载映射到事件的城镇。

driver.switchTo().frame((WebElement) By.name("contents"));
System.out.println("Frame is selected");

1 个答案:

答案 0 :(得分:2)

类似的东西:

            $er->createQueryBuilder('t')
            ->select('t')
            ->join('t.events', 'e')
            ->where('e.id = :eventId')
            ->setParameter('eventId', $eventId)

应该工作。