微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 mongodb 时,数组集合会自动转换为对象

如何解决使用 mongodb 时,数组集合会自动转换为对象

我的 mongo db 中有以下名为 config_settings 的集合

/**
 * @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 '';
    }
}

所以问题来了,每当我尝试更新表单并更改值时,它总是刷新并将其保存为对象而不是将其保存为数组。

这是在刷新到我的数据库之前。

enter image description here

这是在刷新到我的数据库之后。

enter image description here

这是我在控制器中保存表单的方法,非常简单。

 if ('POST' === $request->getmethod()) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $dm->persist($entity);
                $dm->flush();
             }
    }

我做错了什么吗?

解决方法

MongoDB 需要有一个带有连续索引的数组,才能将其保存为数据库中的数组。很可能不是,因此它被保存为一个对象(我认为是数字键)。尝试为您的 collection 字段使用 value 类型,因为它确保您保存到数据库的内容实际上是一个列表。 collection 在后台执行的是 additional array_values 调用,而 raw 类型按原样保存数据。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。