sonata-project / SonataTranslationBundle

SonataTranslationBundle
https://docs.sonata-project.org/projects/SonataTranslationBundle
MIT License
76 stars 69 forks source link

Saving a field in another locale override that field, losing data. Any kind of help is much appreciated. #615

Closed gremo closed 2 years ago

gremo commented 2 years ago

Libraries version (only show relevant ones):

gedmo/doctrine-extensions 3.1.0
stof/doctrine-extensions-bundle 1.6.0
sonata-project/translation-bundle 2.9.1
sonata-project/admin-bundle 3.105.3
doctrine/annotations 1.13.2
doctrine/cache 2.1.1
doctrine/collections 1.6.8
doctrine/common 3.1.2
doctrine/dbal 2.13.3
doctrine/deprecations v0.5.3
doctrine/doctrine-bundle 2.4.2
doctrine/doctrine-migrations-bundle 3.1.1
doctrine/event-manager 1.1.1
doctrine/inflector 1.4.4
doctrine/instantiator  1.4.0
doctrine/lexer 1.2.1
doctrine/migrations 3.2.1
doctrine/orm 2.10.1
doctrine/persistence 2.2.2
doctrine/sql-formatter 1.1.1

Configuration:

stof_doctrine_extensions:
    default_locale: 'it'
    translation_fallback: true
    orm:
        default:
            blameable: true
            #loggable: true
            #reference_integrity: true
            sluggable: true
            #softdeleteable: true
            sortable: true
            timestampable: true
            translatable: true
            #tree: true
            #uploadable: true

sonata_translation:
    locales: ['it', 'en']
    default_locale: 'it'
    gedmo:
        enabled: true

sonata_admin:
    assets:
        extra_stylesheets:
            - bundles/sonatatranslation/css/sonata-translation.css

doctrine:
    orm:
        mappings:
            gedmo_translatable:
                type: annotation
                prefix: Gedmo\Translatable\Entity
                dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translatable/Entity'
                is_bundle: false
            gedmo_translator:
                type: annotation
                prefix: Gedmo\Translator\Entity
                dir: '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Translator/Entity'
                is_bundle: false

Admin works just fine (flags are shown, fields are saved, etc.) but... when I switch the tab to English, and then save, the value in Italian gets overridden:

bug

I'm using this bundle since years and never had any kind of problem. Here is the example entity:

<?php

declare(strict_types=1);

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface;
use Sonata\TranslationBundle\Traits\Gedmo\TranslatableTrait;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\Table("product_feature")
 */
class ProductFeature implements TranslatableInterface
{
    use TranslatableTrait;

    /**
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var null|int
     */
    private $id;

    /**
     * @ORM\Column(length=64)
     * @Assert\NotBlank()
     * @Assert\Length(max=64)
     * @Gedmo\Translatable()
     *
     * @var null|string
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Media")
     * @ORM\JoinColumn(onDelete="CASCADE")
     *
     * @var null|Media
     */
    private $icon;

    public function __toString(): string
    {
        return null === $this->name ? '' : sprintf('[%s] %s', $this->id, $this->name);
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setName(?string $name): void
    {
        $this->name = $name;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setIcon(?Media $icon): void
    {
        $this->icon = $icon;
    }

    public function getIcon(): ?Media
    {
        return $this->icon;
    }
}
franmomu commented 2 years ago

hi @gremo, can you please try to upgrade gedmo/doctrine-extensions to the latest version or at least 3.3.1 to see if it works?

gremo commented 2 years ago

Hi @franmomu thanks for the input, you made my day!!! I updatedgedmo/doctrine-extensions to3.3.1 and doctrine/orm to 2.10.4 (to avoid a conflict) and now it's working again.

Are you aware of some kind of bug?

franmomu commented 2 years ago

you're welcome, the thing is that doctrine/orm replaced spl_object_hash calls by spl_object_id calls in https://github.com/doctrine/orm/pull/8837 and that broke some things in gemo/doctrine-extensions that were fixed in https://github.com/doctrine-extensions/DoctrineExtensions/pull/2272 and released in 3.3.0.

By the way, looking at the code:

use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface;
use Sonata\TranslationBundle\Traits\Gedmo\TranslatableTrait;

these were deprecated, just in case you want to upgrade to 3.0 (not released yet).

I'm closing the issue since it's resolved.