2
錯誤消息看起來像這樣的Symfony形式:爲多對多實體使用自定義查詢構建器
[Semantical Error] line 0, col 58 near 'events = :eventId': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
哪個是正確的,因爲它是一個ManyToMany
加盟,與連接表。但我該如何加載這些實體?
問題出在活動形式(最後)。我嘗試加載分配了ManyToMany
加入某個特定事件的小鎮。 典說,超過1000個字,所以來看看(:
所以我有三個實體在Symfony的應用Event
,Town
,Activity
的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;
// ...
這一切都工作。然而,當我想要分配Town
或多個時,問題出現在Activity
的形式中。我嘗試只加載映射到該事件的城鎮。
// ...
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)
;
}
))
// ...
;
}
// ...
正是我在找的東西。謝謝 – wawa