奏鸣曲管理包更新文档导致删除嵌入的文档

问题描述

我有一个使用 SonataAdmin 创建的应用程序,使用 ODM(Mongo)。 我有 2 个类(部分和级别),其中第一个(部分)可以从 Level 中嵌入 Many。 当我创建一个文档(Section)并在其中嵌入一些级别时,它工作正常,但更新该文档将导致删除所有嵌入的文档(级别)。

两个类:

    use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
    
    /**
     * @MongoDB\Document
     */
    
    class Level
    {
        /**
         * @MongoDB\Id
         */
        protected $id;
    
        /**
         * @MongoDB\Field(type="string")
         */
        protected $label;
    ...
    }

    use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
    
    /**
     * @MongoDB\Document
     */
    
    class Section
    {
        /**
         * @MongoDB\Id
         */
        protected $id;
    
        /**
         * @MongoDB\Field(type="string")
         */
        protected $label;
    
        /**
         * @MongoDB\EmbedMany(targetDocument=Level::class)
         */
    
        protected $levels;
    }

管理文件部分

final class SectionAdmin extends AbstractAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('label')
            ->add('levels')
            // add custom action links
            ->add('_action','actions',[
                'actions' => [
                    'view' => [],'edit' => [],]
            ]);
    }
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('label')->add('levels');
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper->add('label')->add('levels');
    }
}

点击更新后登录:

php_1    | NOTICE: PHP message: [debug] MongoDB command: {"update":"Section","ordered":true,"$db":"symfony","updates":[{"q":{"_id":{"$oid":"6067980a450b7345b76bc8f4"}},"u":{"$unset":{"levels.0":true,"levels.1":true}},"upsert":false,"multi":false}]}
php_1    | NOTICE: PHP message: [debug] MongoDB command: {"update":"Section","u":{"$pull":{"levels":null}},"u":{"$push":{"levels":{"$each":[{"_id":{"$oid":"606666688041ee13136a5f52"},"label":"5eme"},{"_id":{"$oid":"6066666d8041ee13136a5f53"},"label":"6eme"},{"_id":{"$oid":"6067962f493d2118fc74f5c2"},"label":"1 ere"}]}}},"multi":false}]}
php_1    | NOTICE: PHP message: [debug] MongoDB command: {"delete":"Level","deletes":[{"q":{"_id":{"$oid":"606666688041ee13136a5f52"}},"limit":1}]}
php_1    | NOTICE: PHP message: [debug] MongoDB command: {"delete":"Level","deletes":[{"q":{"_id":{"$oid":"6066666d8041ee13136a5f53"}},"limit":1}]}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...