EntityType 形式的 PersisCollection [英] PersisCollection in EntityType Form
问题描述
我正在将表单中的所有数据保存到 DB 中的 JSON_arrays.现在我遇到了 EventListeners 的问题.
I am saving all of data from forms to JSON_arrays in DB. Now i got a problem with EventListeners.
当我从 $event->getClientsShippings() 获取数据并尝试将其传递给另一个相关实体中的选择"时,这给了我一个错误:
When i taking a data from $event->getClientsShippings() and try to pass it to 'choices' in another related Entity this gives me a error that:
Catchable Fatal Error: Object of class App\Entity\ClientsShippings could not be converted to string
我会试试这个:$shipping->getJson()["clients_shippings"]["name"] 但还有另一个错误:
I`ll try this: $shipping->getJson()["clients_shippings"]["name"] but there is another error that:
Attempted to call an undefined method named "getJson" of class "Doctrine\ORM\PersistentCollection".
唯一有效的方法是我将其用作函数示例:
Only way that`s working is if i use it as function example:
'choices' => function($shipping) {
return ''.$shipping->getJson()["clients_shippings"]["name"].''
}
但这会从该字段的实体而不是事件侦听器中获取数据.
But this tooks data from entity of this field not from eventlistener.
这是我的代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('client', EntityType::class, array(
'data' => $options['client'],
'mapped' => false,
'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz klienta'],
'class' => UserDetails::class,
'choice_label' => function ($client) {
if(isset($client->getJson()["client"]["firma"]))
{
$firma = $client->getJson()["client"]["imie"];
}
else {
$firma = "";
}
return ''.$firma.' '.$client->getJson()["client"]["imie"] .' '. $client->getJson()["client"]["nazwisko"].'';
},
'label' => 'Wybierz klienta'
))
->add('product', EntityType::class, array(
'data' => $options['product'],
'mapped' => false,
'multiple' => true,
'class' => Products::class,
'attr' => ['class' => 'chosen-select','data-placeholder'=>'Wybierz produkt'],
'choice_label' => function ($product) {
return ''.$product->getJson()["products"]["name"] .' | Stan Magazynowy: '.$product->getJson()["products"]["stock"].'';
},
'label' => 'Wybierz produkty'
))
->add('shipping', EntityType::class, [
'class' => ClientsShippings::class,
'placeholder' => 'Wybierz adres dostawy',
'choices' => []
])
->add('save', SubmitType::class, [
'label' => 'Zapisz',
'attr' => ['class' => 'btn btn-primary pull-right']])
;
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event)
{
$form = $event->getForm();
$shipping = $form->getData()->getClientsShippings();
$form->getParent()->add('shipping', EntityType::class, [
'class' => ClientsShippings::class,
'placeholder' => 'Wybierz adres dostawy',
'choices' => $shipping->getJson()["clients_shippings"]["name"]
]);
}
);
}
知道如何将我从 EventLitener 获得的内容传递给字段吗?
Any idea how i can pass persistCollection what i got from EventLitener to field ?
如果我离开清除"数组,我从 getClientsShipings() 函数中获取的内容会导致错误,我将尝试将数组转换为字符串.
If i leave "clear" array what i took from getClientsShipings() function that gives error that i`ll try to convert array to string.
推荐答案
好吧,解决方案很简单.我过去了,如果有人会读它.
Ok the solution is easy. I past if someone will read it.
$builder->get('client')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event)
{
$form = $event->getForm();
$shippings = $form->getData()->getClientsShippings()->getValues();
$form->getParent()->add('shipping', EntityType::class, [
'class' => ClientsShippings::class,
'placeholder' => 'Wybierz adres dostawy',
'choices' => $shippings,
'choice_label' => function ($shipping) {
return "".$shipping->getJson()['clients_shippings']['name']."";
}
如果有人获得了 PersistentCollection 并且需要像我一样将其转换为 ArrayCollection,请使用 getValues() 函数,然后我将此数组传递给 'choices',然后如果我从循环函数中读取choice_label,它会获取正确的条目.
If someone got PersistentCollection and need to convert it to ArrayCollection as I, use getValues() function, and then I passed this array to 'choices' and then if i readed choice_label from loop function it took correct entrys.
;)
这篇关于EntityType 形式的 PersisCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!