无法确定 class "App\Entity\GameGenre" 中 属性 "games" 的访问类型:

Could not determine access type for property "games" in class "App\Entity\GameGenre":

我有一个 Symfony 4.2 应用程序。有实体游戏和游戏类型。它们彼此之间具有 ManyToMany 关系。我正在尝试加载固定装置并收到以下错误:

Could not determine access type for property "games" in class "App\Entity\GameGenre": The property "games" in class "App\Entity\GameGenre" can be defined with the methods "addGame()", "removeGame()" but the new value must be an array or an instance of \Traversable, "App\Entity\Game" given.

我的代码如下。

Game.php

<?php

namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\GameRepository")
 */
class Game
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

...

    /**
     * @ORM\ManyToMany(
     *     targetEntity="App\Entity\GameGenre",
     *     inversedBy="games"
     * )
     */
    private $genres;

...

    /**
     * @return Collection|GameGenre[]
     */
    public function getGenres() : Collection
    {
        return $this->genres;
    }

    public function addGenre(GameGenre $genre): self
    {
        if (!$this->genres->contains($genre)) {
            $this->genres[] = $genre;
            $genre->addGame($this);
        }

        return $this;
    }

    public function removeGenre(GameGenre $genre): self
    {
        if ($this->genres->contains($genre)) {
            $this->genres->removeElement($genre);
            $genre->removeGame($this);
        }

        return $this;
    }

...

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

GameGenre.php

<?php

namespace App\Entity;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\GameGenreRepository")
 */
class GameGenre
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(
     *     targetEntity="App\Entity\Game",
     *     mappedBy="genres"
     * )
     * @ORM\OrderBy({"name" = "ASC"})
     */
    private $games;

...

    /**
     * @return Collection|Game[]
     */
    public function getGames() : Collection
    {
        return $this->games;
    }

    public function addGame(Game $game): self
    {
        if (!$this->games->contains($game)) {
            $this->games[] = $game;
            $game->addGenre($this);
        }

        return $this;
    }

    public function removeGame(Game $game): self
    {
        if ($this->games->contains($game)) {
            $this->games->removeElement($game);
            $game->removeGenre($this);
        }

        return $this;
    }

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

}

并且看起来 fixtures yaml 中没有什么奇怪的地方:

genre.yaml

App\Entity\GameGenre:
    genre_{1..9}:
...
        games: '@game_*'

game.yaml 没有提到流派字段,但我试图通过调用 addGenre() 而不是 addGame() 来改变关系端,或者在两个夹具中都使用它们文件,但没有任何帮助,所以我认为还有其他问题。

你能帮帮我吗?

你的字段是一个数组,但你试图插入单个值,它应该是:

App\Entity\GameGenre:
    genre_{1..9}:
...
        games: ['@game_*']

App\Entity\GameGenre:
    genre_{1..9}:
...
        games:
            - '@game_*'