Comments (2)
Hi @yalagin, there are a few things you should change in your code:
Event
entity should not mapname
anddescription
to the database, those fields are meant to be saved only inEventTranslation
.- You should map
translations
field inEvent
:
/**
* @ORM\OneToMany(targetEntity="EventTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
* @Groups({"event_write", "translations"})
*/
protected $translations;
- And map
translatable
field inEventTranslation
:
/**
* @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.
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)
- PUT requests not updating translations, they always INSERT and DELETE all of them HOT 5
- Uncaught Error: Return value of Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable::getTranslations() must implement interface Doctrine\Common\Collections\Collection, null returned HOT 1
- Can't force certain locale HOT 1
- Translations create not working as expected when GraphQl is used HOT 1
- best practice to prevent the user from translating the same word in the same language
- inconsistency of returning types HOT 2
- Error on loading AliceBundleFixtures HOT 1
- The Symfony validator don't with this bundle HOT 1
- validation of the fields of my entity with PUT REQUEST HOT 2
- Loader Load Exception HOT 1
- OrderFilter on translated fields and a certain locale HOT 2
- editing object after increment 'Translation' id
- Automatically add translation when it does not exist HOT 4
- Force length 2 for locales HOT 5
- Filter on current locale translations
- support api-platform/core^3.0@rc HOT 1
- unique constraint " name + locale " HOT 2
- Usage question about Api Platforms json-response HOT 1
- New release? HOT 2
- Doctrine/orm 3 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apiplatformtranslationbundle.