Closed yalagin closed 3 years ago
Hi @yalagin, there are a few things you should change in your code:
Event
entity should not map name
and description
to the database, those fields are meant to be saved only in EventTranslation
.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;
translatable
field in EventTranslation
:
/**
* @ORM\ManyToOne(targetEntity="Event", inversedBy="translations")
*/
protected $translatable;
<?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 :)
Thanks it's working now =) I already implemented another approach in last project but for current one I am using your bundle.
I've got a fresh template form https://github.com/api-platform/api-platform and I got api/config/api_platform/resources.yaml
got an errror
well I try sec time
got the same error