php - 刷新到 mongodb 时,数组集合会自动转换为对象
问题描述
我config_settings
在我的 mongo 数据库中调用了以下集合
/**
* @MongoDB\Document(collection="config_settings", repositoryClass="AppBundle\Repository\SettingRepository")
* @MongoDB\HasLifecycleCallbacks()
*/
class Setting
{
/**
* @MongoDB\Id()
* @var ObjectId $id
*/
protected $id;
/**
* @MongoDB\Field(type="string")
* @var string $name
*/
protected $name;
/**
* @MongoDB\Field(type="raw")
* @var array<array> $value
*/
protected $value;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value): void
{
$this->value = $value;
}
}
我有一个带有值和名称的表单,并将其保存到数据库中
<?php
namespace AppBundle\Form;
use AppBundle\Document\Setting;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SettingCollectionType extends AbstractType
{
/**
* {@inheritdoc}
* @param FormBuilderInterface<string|FormBuilderInterface> $builder
* @param array<array> $options
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$choices = [];
foreach ($options['featureKeys'] as $key) {
$choices[$key] = $key;
}
$builder
->add('name', ChoiceType::class, [
'label' => 'Feature',
'choices' => $choices,
'required' => true,
])
->add('value', CollectionType::class, array(
'entry_type' => TextType::class,
'prototype' => true,
'allow_add' => true,
'allow_delete' => true,
'label' => false,
))
->add('submit', SubmitType::class, [
'attr' => [
'class' => 'btn btn-outline-secondary'
],
]);
}
/**
* {@inheritdoc}
* @param OptionsResolver $resolver
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Setting::class,
'featureKeys' => null
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return '';
}
}
所以这就是问题所在,每当我尝试更新表单并更改值时,它总是刷新并将其保存为对象,而不是将其保存为数组。
这是在刷新到我的数据库之前。
这是在刷新到我的数据库之后。
这是我在控制器中保存表单的方法,非常简单。
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
$dm->persist($entity);
$dm->flush();
}
}
难道我做错了什么 ?
解决方案
MongoDB 需要有一个具有连续索引的数组才能将其保存为数据库中的数组。很可能不是因此它被保存为一个对象(我想用数字键)。尝试为您的字段使用collection
类型,value
因为它可以确保您保存到数据库的内容实际上是一个列表。collection
幕后的作用是一个额外的array_values
调用,而raw
type 按原样保存数据。
推荐阅读
- javascript - JavaScript 滑块上的 TypeError 类名
- javascript - 解析 JSON 对象时如何保留键的顺序
- android - 获得 500 - Youtube 广播插入 API 遇到内部错误
- matlab - 对数据使用 pol2cart 并从中获取图像
- c++ - ')' 标记之前的预期主表达式
- c# - ASP.NET Core 3.1 Windows 身份验证不断提示输入凭据
- pandas - 来自一列熊猫或字典的堆积条形图
- php - 为什么 PHP 将我的函数打印为字符串?
- css - 下划线 WP 主题 CSS 将侧边栏移到一边
- python-3.x - 我正在随机修改 HSV 颜色,但颜色不是应有的颜色