我有一个Symfony 4.2应用程序。有实体游戏和游戏机。他们彼此之间有许多关系。我正在尝试加载固定装置并收到以下错误:
无法确定类" app entity gamegenre"中属性"游戏"的访问类型:可以使用方法"removegame(("但是新值必须是 traversable," app entity game"给定的数组或实例。
我的代码如下。
game.php
<?php
namespace AppEntity;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoryGameRepository")
*/
class Game
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
protected $id;
...
/**
* @ORMManyToMany(
* targetEntity="AppEntityGameGenre",
* 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 AppEntity;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoryGameGenreRepository")
*/
class GameGenre
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
protected $id;
/**
* @ORMManyToMany(
* targetEntity="AppEntityGame",
* mappedBy="genres"
* )
* @ORMOrderBy({"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();
}
}
,看起来固定装置中没有什么真正奇怪的:
genre.yaml
AppEntityGameGenre:
genre_{1..9}:
...
games: '@game_*'
game.yaml 没有提及流派字段,但是我试图通过调用AddGenre((而不是AddGame((或在两个固定文件中使用它们来更改关系方面,但没有任何帮助,所以我认为还有其他问题。
您能帮我吗?
您的字段是一个数组,但是您正在尝试插入一个值,应该是:
AppEntityGameGenre:
genre_{1..9}:
...
games: ['@game_*']
或
AppEntityGameGenre:
genre_{1..9}:
...
games:
- '@game_*'