如何正确获取发送到集合中的数据?
重要:
如果我从Create Form类中删除以下行:
'data'=> array_values($builder->getData()->cpus), //TODO: shit code send data And add in Collection
并在Collection Form类中添加以下内容:
'data_class' => Command::class, //namespace (Collection\Command)
这仅在视图(细枝)阶段有效,但首先我必须得到那些 收集表单之前创建数据(视图)。
我该怎么做?
此刻,我找到了这个解决方案(看起来很“糟糕”):)。标记为 TODO
的行Collection \ Command:
class Command
{
public $id_item;
/**
* @var ItemInterface $interface
*/
public $interface;
public $id_object;
public function __construct(string $id_item, ItemInterface $interface)
{
$this->id_item = $id_item;
$this->interface = $interface;
}
public function getIdItem(): string
{
return $this->id_item;
}
public function getInterface(): ItemInterface
{
return $this->interface;
}
}
有问题,我不能很好地从$ cpus获取数据:)
收藏\表格:
class Form extends AbstractType
{
private $cpuModelFetcher;
public function __construct(CpuModelFetcher $cpuModelFetcher)
{
$this->cpuModelFetcher = $cpuModelFetcher;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$index = (int)$builder->getName(); //TODO: shit code - use name of form as index.
/**
* @var Command $command
*/
$command = $builder->getData()[$index]; //TODO: shit code get data from array
$builder
->add('id_object', Type\ChoiceType::class,
[
'choices' => array_flip($this->cpuModelFetcher->getSupportedCpuModelListForModelBasedDeviceBySocketType(
$command->getIdItem(),$command->getInterface()->getId())),
'placeholder' => 'not installed in '.$command->getInterface()->getName().' socket.',
'required' => false,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
//'data_class' => Command::class,
'data_class' => null,
));
}
}
创建命令:
class Command
{
/**
* @Assert\NotBlank()
*/
private $id;
public $cpus;
private function __construct(Id $id)
{
$this->id = $id;
}
public static function fromDevice(Device $device): self
{
$command = new self($device->getId());
if($device->getBasedType()->isModelBasedDevice()){
$command->cpus = $command->transformDeviceModelInterfaceCollectionToCommandArray($device->getId(),
$device->getModelBasedDevice()->getDeviceModel()->getCpuInterfaces());
}
return $command;
}
private function transformDeviceModelInterfaceCollectionToCommandArray(Id $id_item, ArrayCollection $interfaces): array
{
$object_specific_interfaces = [];
foreach ($interfaces as $one){
/**
* @var DeviceModelInterface $one
*/
for($i=0; $i<$one->getAmount(); $i++){
$object_specific_interfaces[] = new Collection\Command($id_item->getValue(), $one->getInterface());
}
}
unset($one);
return $object_specific_interfaces;
}
}
创建表单:
class Form extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
//dump($builder);
$builder
->add('cpus', CollectionType::class, [
'label' => false,
'entry_type' => Collection\Form::class,
'entry_options' => [
'label' => false,
'data'=> array_values($builder->getData()->cpus), //TODO: shit code send data
],
'by_reference' => true,
'allow_add' => false,
'allow_delete' => false,
'delete_empty' => true,
])
->add('save', Type\SubmitType::class, [
'attr' => ['class' => 'btn btn-primary'],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
'data_class' => Command::class,
));
}
}
谢谢您的帮助。
答案 0 :(得分:0)
我发现此解决方案看起来更好,我只是从Collection \ Form类中的PRE_SET_DATA事件中获取数据。
class Form extends AbstractType
{
private $cpuModelFetcher;
public function __construct(CpuModelFetcher $cpuModelFetcher)
{
$this->cpuModelFetcher = $cpuModelFetcher;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
}
protected function addElements(FormInterface $form, $data): void
{
/**
* @var Command $data
*/
$form->add('id_object', Type\ChoiceType::class,
[
'choices' => array_flip($this->cpuModelFetcher->getSupportedCpuModelListForModelBasedDeviceBySocketType(
$data->getIdItem(), $data->getInterface()->getId())),
'placeholder' => 'not installed in '.$data->getInterface()->getName().' socket.',
'required' => false,
]);
//dump($data);
//dump($form);
}
function onPreSetData(FormEvent $event): void
{
$form = $event->getForm();
$data = $event->getData();
$this->addElements($form, $data);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
'data_class' => Command::class,
));
}
}
无论如何,如果有人知道其他解决方案,我将很高兴听到它。
谢谢。