与艺术家实体并在Manytomany关系中展示,试图构建一种表格,该表格列出了尚未在给定的节目中列出的现有艺术家导致错误
错误:无效的路径表达。必须是一个Collection ValuedAssociationField
编辑:
在学说的文档中有 notIn($x, $y)
的表达式,但根本不清楚(即尝试失败)如何在表单类中使用。
编辑2:
可以在艺术家存储库中获得预期的结果,并显示下面的功能。不过,它在表单类中不可用,因为它的结果不是查询布构成的实例![感谢Xurshid29的So回答。]
public function notInShow($show)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$ids = $qb
->select('a.id')
->from('AppBundle:Artist', 'a', 'a.id')
->leftJoin('a.shows', 's')
->where('s.show = ?1')
->setParameter(1, $show->getShow())
->getQuery()
->getArrayResult();
$ids = array_keys($ids);
$qbA = $this->getEntityManager()->createQuery(
'SELECT a FROM AppBundle:Artist a '
. 'WHERE a.id NOT IN (:ids)')
->setParameter(':ids', $ids)
->getResult();
return $qbA;
}
(许多人)建立一个领域的最新尝试看起来像这样:
表格类(片段):
public function buildForm(FormBuilderInterface $builder, array $options)
{
$show = $options['show'];
$builder->add('artists', EntityType::class,
[
'class' => Artist::class,
'choice_label' => function($artist, $key, $index) {
return $artist->getLastName() . ', ' . $artist->getFirstName();
},
'query_builder' => function (EntityRepository $er) use($show) {
return $er->createQueryBuilder('a')
->join('a.shows', 's')
->where('a NOT MEMBER of s')
->andWhere('IDENTITY(s) = :show')
->setParameter(':show', $show->getId())
->orderBy('a.firstName', 'ASC')
->orderBy('a.lastName', 'ASC')
;
},
'expanded' => true,
'multiple' => true,
]);
}
实体片段:
艺术家:
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="Show", mappedBy="artists")
*/
protected $shows;
显示:
/**
* @var DoctrineCommonCollectionsCollection
*
* @ORMManyToMany(targetEntity="Artist", inversedBy="shows", cascade={"persist"})
* @ORMJoinTable(name="participation",
* joinColumns={@ORMJoinColumn(name="show_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="artist_id", referencedColumnName="id")}
* ))
*/
protected $artists;
终于!一些最终的调整,这有效:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$show = $options['show'];
$builder->add('artists', EntityType::class,
[
'class' => Artist::class,
'choice_label' => function($artist, $key, $index) {
return $artist->getLastName() . ', ' . $artist->getFirstName();
},
'query_builder' => function (EntityRepository $er) use($show) {
$qb = $er->createQueryBuilder('a');
$ids = $qb
->select('a.id')
->leftJoin('a.shows', 's')
->where('s.show = ?1')
->setParameter(1, $show->getShow())
->getQuery()
->getResult();
if (empty($ids)) {
return $er->createQueryBuilder('a');
} else {
return $er->createQueryBuilder('a')
->where($er->createQueryBuilder('a')->expr()->notIn('a.id', ':ids'))
->setParameter(':ids', $ids);
}
},
'expanded' => true,
'multiple' => true,
])
->add('save', SubmitType::class,
array(
'label' => 'Add artist(s)',
'label_format' => ['class' => 'text-bold']
));
}