【问题标题】:(Symfony 4 Forms) Passing non-entity query results to a form function(Symfony 4 Forms) 将非实体查询结果传递给表单函数
【发布时间】:2019-01-15 09:02:10
【问题描述】:

我不知道如何告诉我的表单类型类接受传入键/值数组的选择值函数参数。

这是我的表单类型 buildForm 函数的样子:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('tiers', ChoiceType::class, [
            'choices'  => $this->userSubscriptionTierRepository->getTiersSubscribedToByUser($this->userProvider->getCurrentUser()),
            'choice_label' => 'name',
            'required' => false,
            'multiple' => true,
            'expanded' => false,
            'attr'=> array('class'=>'custom-select'),
            'choice_value' => function (array $val /* this is wrong */) {
                return $val['tierNumber'];
            }
            ])
    ;
}

在底部,'choice_value' 所在的位置,我不知道如何正确声明函数以接受传入的值。

下面是选项“getTiersSubscribedToByUser”函​​数的样子:

/**
 * @param User $user
 * @return array
 */
public function getTiersSubscribedToByUser(User $user) : array
{
    $qb = $this->createQueryBuilder('t');
    $qb->select('t.tierNumber, COUNT(t.tierNumber)')
        ->innerJoin('t.subscriptions', 'subscriptions', 'WITH', 'subscriptions.isCancelled != :true')
        ->where('subscriptions.subscriber = :user')
        ->groupBy('t.tierNumber')
        ->orderBy('t.tierNumber', 'ASC')
        ->setParameter('user', $user)
        ->setParameter('true', true);

    $query = $qb->getQuery();
    return $query->getResult();
}

getTiersSubscribedToByUser 的结果如下所示:

array:3 [▼
  0 => array:2 [▼
    "tierNumber" => 1
    1 => "2"
  ]
  1 => array:2 [▼
    "tierNumber" => 2
    1 => "1"
  ]
  2 => array:2 [▼
    "tierNumber" => 3
    1 => "1"
  ]
]

所以我必须告诉表单类型类选择值以接受如下所示:

1 => array:2 [▼
        "tierNumber" => 2
        1 => "1"

我该怎么做?

它给了我错误:

Argument 1 passed to App\Form\FeedFilterType::App\Form\{closure}() must be of the type array, integer given

【问题讨论】:

  • 你试过dump()数组$val吗?只需删除 array 部分并放置一个 dump() 和一个 exit 以查看其值。
  • If there are choice values that are not scalar or the stringified representation is not unique Symfony will use incrementing integers as values. When the form gets submitted the correct values with the correct types will be assigned to the model. symfony.com/doc/4.1/reference/forms/types/choice.html#choices
  • JorgeeFG,我尝试转储并得到数字“1”。但这仍然不能解决我的问题。

标签: forms function symfony choice


【解决方案1】:

选择价值将准确地告诉你他在说什么,每个选择的价值。如果你发送 $val['tierNumber'] ,根据你给定的数组,你会得到一个整数,但它需要一个数组。

根据choice_value 的文档:

This can be a callable or a property path. If null is given, an incrementing integer is used as the value. If you pass a callable, it will receive one argument: the choice itself.

由于您不使用 EntityType,因此您不需要处理choice_value,因为您的选择不是对象:

如果你传递一个可调用对象,它将接收一个参数:选择 本身。使用 EntityType 字段时,参数将是 每个选项的实体对象或在某些情况下为 null,您需要 句柄:

'choice_value' => function (MyOptionEntity $entity = null) {
    return $entity ? $entity->getId() : '';
},

你试过不使用choice_value吗?

这里的文档: https://symfony.com/doc/4.1/reference/forms/types/choice.html#choice-value

【讨论】:

  • 如果我不使用choice_value,我会收到以下错误:PropertyAccessor 需要对象或数组的图形来操作,但它在尝试遍历属性路径“名称”时发现类型“整数” “名称”。
  • 是的,因为如果你在你的choice_value 上有一个对象,你可以做 object.name (property path) ,但是这里你没有对象,所以你必须将一个可调用传递给你的choice_label为了得到正确的数据
猜你喜欢
  • 2018-04-17
  • 2020-01-09
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2021-04-05
  • 1970-01-01
  • 2013-01-17
相关资源
最近更新 更多