Locastic / ApiPlatformTranslationBundle

Translation bundle for ApiPlatform based on Sylius translation
MIT License
85 stars 28 forks source link

Cannot get it to work #27

Closed BeyondRico closed 3 years ago

BeyondRico commented 3 years ago

whatever I try to do I always get the error:

"Argument 1 passed to Locastic\ApiPlatformTranslationBundle\EventListener\AssignLocaleListener::prePersist() must be an instance of Doctrine\Common\Persistence\Event\LifecycleEventArgs, instance of Doctrine\ORM\Event\LifecycleEventArgs given, called in C:\xampp\htdocs\Symfony\project_4\vendor\symfony\doctrine-bridge\ContainerAwareEventManager.php on line 58"

I followed the readme and uses two different json formats to post. both resulting in the error above.

{
  "translations": [
    {
      "title": "test",
      "locale": "en"
    }
  ]
}
{
    "translations": { 
        "en":{
            "title":"test",
            "locale":"en"
        },
        "de":{
            "title":"test de",
            "locale":"de"
        }
    }
}

Here are my entity's: Event.php

<?php

namespace App\Entity;

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

/**
 * @ApiResource(
 *     itemOperations={
 *         "get"={"normalization_context"={"groups"={"get"}}},
 *         "put"={"normalization_context"={"groups"={"translations"}}}
 *     },
 *     collectionOperations={
 *         "get",
 *         "post"={"normalization_context"={"groups"={"translations"}}}
 *    },
 *    normalizationContext={"groups"={"post_read"}},
 *    denormalizationContext={"groups"={"post_write"}}
 * )
 * @ORM\Entity(repositoryClass=EventRepository::class)
 */
class Event extends AbstractTranslatable
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

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

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

    public function setTitle(string $title): void
    {
        $this->getTranslation()->setLocale($title);
    }

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

EventTranslation.php

<?php

namespace App\Entity;

use App\Repository\EventTranslationRepository;
use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass=EventTranslationRepository::class)
 */
class EventTranslation extends AbstractTranslation
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Groups({"post_read", "post_write", "translations"})
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @Groups({"post_write", "translations"})
     */
    protected  $locale;

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

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

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }
}
paullla commented 3 years ago

Hi @BeyondRico, which versions or Symfony and Doctrine are you using?

BeyondRico commented 3 years ago

Hi @BeyondRico, which versions or Symfony and Doctrine are you using?

Symfony version: 5.1.6 Doctrine bundle: 2.1.2 Doctrine orm: 2.7.3

don't know which version you need from doctrine

paullla commented 3 years ago

Hi @BeyondRico, this was a bug and it should be fixed with: https://github.com/Locastic/ApiPlatformTranslationBundle/pull/31. Thank you for reporting, please update to new version and let me know if this solved your issue :)