gpt4 book ai didi

forms - Symfony DataMapper 不更新对象

转载 作者:行者123 更新时间:2023-12-04 16:55:12 26 4
gpt4 key购买 nike

根据文档,我正在使用 DataMapper:https://symfony.com/doc/current/form/data_mappers.html从表单中 3 个字段中的 1 个更新日期:

表格类

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('period_start_daily', DateType::class, [
'widget' => 'single_text',
'label' => 'Day',
'html5' => false,
'attr' => ['class' => 'js-datepicker'],
'mapped' => false,
'required' => false
])
->add('period_start_weekly', DateType::class, [
'widget' => 'single_text',
'label' => 'Week',
'html5' => false,
'attr' => ['class' => 'js-datepicker'],
'mapped' => false,
'required' => false
])
->add('period_start_monthly', ChoiceType::class, [
'required' => true,
'label' => 'Month',
'choice_loader' => new CallbackChoiceLoader(function () {
$months = [];
for ($i = 0; $i <= 12; $i++) {
$month = strtotime('first day of this month + ' . $i . ' months');
$months[date('M Y', $month)] = date('Y-m-d', $month);
}
return $months;
}),
'mapped' => false,
'required' => false
])
...
$builder
->setDataMapper($this);
...
public function mapFormsToData($forms, &$data)
{
/** @var FormInterface[] $forms */
$forms = iterator_to_array($forms);

switch (intval($forms['Type']->getViewData())){
case 1:
$start = $forms['period_start_daily']->getNormData();
$end = clone $start;

break;
case 2:
$start = $forms['period_start_weekly']->getNormData();
$end = clone $start;
$end->add(new \DateInterval('P6D'));
break;
case 3:
$start = $forms['period_start_daily']->getNormData();
$end = (new \DateTime(
strtotime($this->getData()['period_start_monthly']->format('Y-m-d').' last day of month')
));
break;
}
$data = (new DataSet())
->setPeriodStart($start)
->setPeriodEnd($end)
->setType($forms['Type']->getNormData())
->setUpload($forms['upload']->getNormData());

Controller
    $form = $this->createForm(DataSetType::class, $dataset);

$form->handleRequest($request->getCurrentRequest());

if ($form->isSubmitted() && $form->isValid()){
$dataset
->setPeriodStart($form->getNormData()->getPeriodStart())
->setPeriodEnd($form->getNormData()->getPeriodEnd());
$dataset->setType($form->getNormData()->getType());
$dataset->setActive(false);
$dataset->setUpload($form->getNormData()->getUpload());
$dataset->getPeriodStart()->setTime(0, 0, 0);
$dataset->getPeriodEnd()->setTime(0, 0, 0);

$em->persist($dataset);
$em->flush();

我希望 DataMapper 直接更新我的对象,但是我仍然需要从 $form->getNormData() 复制数据。

Symfony 版本是 4.2

最佳答案

我下载了your project调试它,我发现了一个错误 - 很难检测到:) 我开始认为 Symfony 4 可能有问题,但不是。一切都很好,一切都很好。

看:

enter image description here

spl_object_hash — Return hash id for given object

This function returns a unique identifier for the object. This id can be used as a hash key for storing objects, or for identifying an object, as long as the object is not destroyed. Once the object is destroyed, its hash may be reused for other objects.


$beforeHash$afterHash具有不同的值 - 因此,它不是同一个对象

DataSetType.php (第 138 行)您创建了 DataSet 的新实例丢失对作为 mapFormsToData 中的第二个参数传递的前一个对象的引用:

enter image description here

您应该修改 $data 中的对象参数改为创建 DataSet 的新实例.

第一步:

更改此代码:
$data = (new DataSet())
->setPeriodStart($start)
->setPeriodEnd($end)
->setType($forms['Type']->getNormData())
->setUpload($forms['upload']->getNormData())
->setActive($forms['active']->getNormData())
;

对于这段代码:
$data->setPeriodStart($start)
->setPeriodEnd($end)
->setType($forms['Type']->getNormData())
->setUpload($forms['upload']->getNormData())
->setActive($forms['active']->getNormData());

第二步:

从 Controller 中删除不必要的代码。 create方法应如下所示:
public function create(RequestStack $request)
{
$dataset = new DataSet();

$form = $this->createForm(DataSetType::class, $dataset);

$form->handleRequest($request->getCurrentRequest());

if ($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($dataset);
$em->flush();

if ($dataset->getUpload()) {
$em->getRepository('App:DataSet')
->disableUploadOthers($dataset->getId(), $dataset->getType());
}
$this->addFlash('success', 'Dataset created.');
return $this->redirectToRoute('dataset_index');
}

return $this->render('dataset/create.html.twig', [
'form' => $form->createView()
]);
}
$dataset 中的值必须在没有你帮助的情况下改变:)

Documentation可能有点困惑,但请注意 example from documentation基于 不可变对象(immutable对象)

enter image description here

顺便说一句 - 扩展文档并展示 DataMapper 如何与 Doctrine 模型一起工作会很好......

关于forms - Symfony DataMapper 不更新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com