Ошибка в Symfony 6 Object of class could not be converted to string

Рейтинг: 1Ответов: 0Опубликовано: 10.07.2023

Есть связь "Many-To-Many" между продуктом и доступности, при входе в сонату выдает ошибку:

Object of class App\Entity\Availability could not be converted to string

Проблема в конвертации Availability в products. Попробовал все, и метод __toString, и менял в конструкторе встроенные классы по типу ArrayCollection - ничего не помогает.

product.php:

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Dto;
use App\Dto\EmptyDto;
use App\Entity\Base\BaseModel;
use App\Entity\Trait\MainTranslationTrait;
use App\Entity\Translation\ProductTranslation;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;
use mysql_xdevapi\Collection;
use phpDocumentor\Reflection\Types\Boolean;

#[ORM\Table(name: 'product')]
#[Gedmo\TranslationEntity(class: self::TRANSLATION_ENTITY)]
#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ApiResource(
    operations: [
    new Post(uriTemplate: '/view/{id}', input: EmptyDto::class),
    new Post(routeName: 'app_product_new', input: Dto\ProdDto::class),
    new Post(uriTemplate: '/edit/{id}', input: Dto\ProdDto::class),
    new Post(uriTemplate: '/delete/{id}', input: EmptyDto::class),
    ],
)]
class Product implements Translatable
{
    use MainTranslationTrait;
    use BaseModel;

    public const TRANSLATION_ENTITY = ProductTranslation::class;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'string', nullable: true)]
    #[Gedmo\Translatable]
    private ?string $title = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $description = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $direction = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTime $date = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $image = null;

    #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'product')]
    private ?object $category;

    #[ORM\ManyToMany(targetEntity: Availability::class, inversedBy: 'product')]
    private ?object $availability;


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

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

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

        return $this;
    }

    public function __toString()
    {
        if ($this->title) {
            return $this->title;
        }
        if ($this->availability) {
            return $this->availability;
        }

        return '';
    }

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

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

    /**
     * @return string|null
     */
    public function getDirection(): ?string
    {
        return $this->direction;
    }

    /**
     * @param string|null $direction
     */
    public function setDirection(?string $direction): void
    {
        $this->direction = $direction;
    }

    public function getAvailability(): ?Availability
    {
        return $this->availability;
    }

    public function setAvailability(?Availability $availability): static
    {
        $this->availability = $availability;

        return $this;
    }



    /**
     * @return \DateTime|null
     */
    public function getDate(): ?\DateTime
    {
        return $this->date;
    }

    /**
     * @param \DateTime|null $date
     */
    public function setDate(?\DateTime $date): void
    {
        $this->date = $date;
    }

    /**
     * @return string|null
     */
    public function getImage(): ?string
    {
        return $this->image;
    }

    /**
     * @param string|null $image
     */
    public function setImage(?string $image): void
    {
        $this->image = $image;
    }



    public function getCategory(): ?Category
    {
        return $this->category;
    }

    public function setCategory(?Category $category): static
    {
        $this->category = $category;

        return $this;
    }
}

availability.php

<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Dto;
use App\Dto\EmptyDto;
use App\Entity\Base\BaseModel;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;

#[ORM\Entity]
#[ORM\Table(name: 'availability')]
//#[Gedmo\TranslationEntity(class: self::TRANSLATION_ENTITY)]
//#[ORM\Entity(repositoryClass: ProductRepository::class)]
#[ApiResource(
    operations: [
        new Post(uriTemplate: '/view/{id}', input: EmptyDto::class),
        new Post(routeName: 'app_post_new', input: Dto\EmptyDto::class),
        new Post(uriTemplate: '/edit/{id}', input: Dto\EmptyDto::class),
        new Post(uriTemplate: '/delete/{id}', input: EmptyDto::class),
    ],
)]
class Availability
{

    use BaseModel;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'string', nullable: true)]
    private ?string $language = null;

    #[ORM\ManyToMany(targetEntity: Product::class, mappedBy: 'availability')]
    private mixed $product;

    public function __construct()
    {
        $this->product = new ArrayCollection();
    }

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

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

    /**
     * @return string|null
     */
    public function getLanguage(): ?string
    {
        return $this->language;
    }

    /**
     * @param string|null $language
     */
    public function setLanguage(?string $language): void
    {
        $this->language = $language;
    }



    /**
     * @return mixed
     */
    public function getProduct(): mixed
    {
        return $this->product;
    }

    /**
     * @param mixed $product
     */
    public function setProduct(mixed $product): void
    {
        $this->product = $product;
    }


}

Ответы

Ответов пока нет.