Git Product home page Git Product logo

Comments (2)

paullla avatar paullla commented on July 24, 2024 1

Hi @yalagin, there are a few things you should change in your code:

  1. Event entity should not map name and description to the database, those fields are meant to be saved only in EventTranslation.
  2. You should map translations field in Event:
    /**
     * @ORM\OneToMany(targetEntity="EventTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     * @Groups({"event_write", "translations"})
     */
    protected $translations;
  1. And map translatable field in EventTranslation:
    /**
     * @ORM\ManyToOne(targetEntity="Event", inversedBy="translations")
     */
    protected $translatable;

So the final result would be:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Class Event
 * @package App\Entity
 * @ORM\Entity
 */
class Event extends AbstractTranslatable
{
    /**
     * @var int The entity Id
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Groups({"event_read", "event_write"})
     * @ORM\Column
     */
    private $startAt;

    /**
     * @Groups({"event_read"})
     */
    private $title;

    /**
     * @Groups({"event_read"})
     */
    private $description;

    /**
     * @ORM\OneToMany(targetEntity="EventTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
     * @Groups({"event_write", "translations"})
     */
    protected $translations;


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

    public function getStartAt()
    {
        return $this->startAt;
    }

    public function setStartAt($startAt)
    {
        $this->startAt = $startAt;
    }

    public function setTitle($title)
    {
        $this->getTranslation()->setTitle($title);
    }

    public function getTitle(): string
    {
        return $this->getTranslation()->getTitle();
    }

    public function setDescription($description)
    {
        $this->getTranslation()->setDescription($description);
    }

    public function getDescription(): string
    {
        return $this->getTranslation()->getDescription();
    }

    protected function createTranslation(): TranslationInterface
    {
        return new EventTranslation();
    }
}
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;
use Locastic\ApiPlatformTranslationBundle\Model\TranslatableInterface;
use Symfony\Component\Serializer\Annotation\Groups;


/**
 * Class EventTranslation
 * @package App\Entity
 * @ORM\Entity
 */
class EventTranslation extends AbstractTranslation
{
    /**
     * @var int The entity Id
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column
     * @Groups({"event_read", "event_write", "translations"})
     */
    private $title;

    /**
     * @var string
     * @ORM\Column
     * @Groups({"event_read", "event_write", "translations"})
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column
     * @Groups({"event_write", "translations"})
     */
    protected $locale;

    /**
     * @ORM\ManyToOne(targetEntity="Event", inversedBy="translations")
     */
    protected $translatable;

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

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getTitle(): string
    {
        return $this->title;
    }

    /**
     * @param string $title
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    /**
     * @return string
     */
    public function getDescription(): string
    {
        return $this->description;
    }

    /**
     * @param string $description
     */
    public function setDescription(string $description): void
    {
        $this->description = $description;
    }

    /**
     * @return string
     */
    public function getLocale(): string
    {
        return $this->locale;
    }

    /**
     * @param string $locale
     */
    public function setLocale(?string $locale): void
    {
        $this->locale = $locale;
    }
}

Example request:

{
  "startAt": "2021-10-10",
  "translations": {
    "en":{
      "title": "Test title",
      "description": "Test description",
      "locale": "en"
   }
  }
}

Please let me know if this works for you.

PS. Make sure you update the bundle to the latest version, we just released some upgrades :)

from apiplatformtranslationbundle.

yalagin avatar yalagin commented on July 24, 2024

Thanks it's working now =) I already implemented another approach in last project but for current one I am using your bundle.

from apiplatformtranslationbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.