ModelType::class in Sonata Admin Project do not persist the changes made in the form

24 Views Asked by At

I cannot persist changes in a OneToMany relationship using the ModelType::class of the Sonata Admin Bundle. The forms retrieve the data correctly but after submitting the changes regarding this relationship are not saved, the other fields work properly.

Entities:

class Banner implements TranslatableInterface
{
...
    #[ORM\OneToMany(mappedBy: 'banner', targetEntity: Pagina::class)]
    private Collection $paginas;
    
    public function __construct()
    {
        $this->paginas = new ArrayCollection();
    }
    public function getPaginas(): Collection
    {
        return $this->paginas;
    }

    public function addPagina(Pagina $pagina): static
    {
        if (!$this->paginas->contains($pagina)) {
            $this->paginas->add($pagina);
            $pagina->setBanner($this);
        }

        return $this;
    }

    public function removePagina(Pagina $pagina): static
    {
        if ($this->paginas->removeElement($pagina)) {
            
            if ($pagina->getBanner() === $this) {
                $pagina->setBanner(null);
            }
        }

        return $this;
    }
...
}
class Pagina implements TranslatableInterface, TreeNodeInterface
{
...
    #[ORM\ManyToOne(inversedBy: 'paginas')]
    private ?Banner $banner = null;
...
}

I am managing Banner entities using sonata project admin.

final class BannerAdmin extends AbstractAdmin
{
...
    protected function configureFormFields(FormMapper $form): void
    {
        
        $form->add('paginas')   
        ;
    }
...
}

I can see the instances of Pagina but it does not take effect any change in this field. I tried to use this ->add('paginas', ModelType::class, ['multiple' => true,]) But it didn't work and changes are note being persisted without giving any error.

This is my composer:

{
    "type": "project",
    "license": "proprietary",
    "minimum-stability": "stable",
    "prefer-stable": true,
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "doctrine/doctrine-bundle": "^2.11",
        "doctrine/doctrine-migrations-bundle": "^3.3",
        "doctrine/orm": "^2.17",
        "friendsofsymfony/ckeditor-bundle": "^2.4",
        "gedmo/doctrine-extensions": "^3.13",
        "knplabs/doctrine-behaviors": "^2.6",
        "knplabs/knp-paginator-bundle": "^6.3",
        "liip/imagine-bundle": "^2.12",
        "phpdocumentor/reflection-docblock": "^5.3",
        "phpstan/phpdoc-parser": "^1.24",
        "sonata-project/doctrine-orm-admin-bundle": "*",
        "sonata-project/formatter-bundle": "*",
        "sonata-project/translation-bundle": "^3.3",
        "sonata-project/user-bundle": "*",
        "symfony/asset": "6.3.*",
        "symfony/console": "6.3.*",
        "symfony/doctrine-messenger": "6.3.*",
        "symfony/dotenv": "6.3.*",
        "symfony/expression-language": "6.3.*",
        "symfony/flex": "^2",
        "symfony/form": "6.3.*",
        "symfony/framework-bundle": "6.3.*",
        "symfony/http-client": "6.3.*",
        "symfony/intl": "6.3.*",
        "symfony/mailchimp-mailer": "6.3.*",
        "symfony/mailer": "6.3.*",
        "symfony/mime": "6.3.*",
        "symfony/monolog-bundle": "^3.0",
        "symfony/notifier": "6.3.*",
        "symfony/process": "6.3.*",
        "symfony/property-access": "6.3.*",
        "symfony/property-info": "6.3.*",
        "symfony/runtime": "6.3.*",
        "symfony/security-bundle": "6.3.*",
        "symfony/serializer": "6.3.*",
        "symfony/string": "6.3.*",
        "symfony/translation": "6.3.*",
        "symfony/twig-bundle": "6.3.*",
        "symfony/validator": "6.3.*",
        "symfony/web-link": "6.3.*",
        "symfony/yaml": "6.3.*",
        "twig/extra-bundle": "^2.12|^3.0",
        "twig/twig": "^2.12|^3.0",
        "vich/uploader-bundle": "^2.2"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true,
            "symfony/flex": true,
            "symfony/runtime": true
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php72": "*",
        "symfony/polyfill-php73": "*",
        "symfony/polyfill-php74": "*",
        "symfony/polyfill-php80": "*",
        "symfony/polyfill-php81": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd",
            "ckeditor:install": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "6.3.*"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^9.5",
        "symfony/browser-kit": "6.3.*",
        "symfony/css-selector": "6.3.*",
        "symfony/debug-bundle": "6.3.*",
        "symfony/maker-bundle": "^1.52",
        "symfony/phpunit-bridge": "^7.0",
        "symfony/stopwatch": "6.3.*",
        "symfony/web-profiler-bundle": "6.3.*"
    }
}
0

There are 0 best solutions below