Php Symfony2:ContextErrorException

Php Symfony2:ContextErrorException,php,symfony,Php,Symfony,我在尝试创建具有特定可变类型的电影列表时遇到了一个问题。例如,所有类型为“浪漫”的电影等等 错误是: ContextErrorException: Notice: Undefined index: genre in /(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php 实体类型如下所示: /** * Genre entity. * */ namespace AppBundle\En

我在尝试创建具有特定可变类型的电影列表时遇到了一个问题。例如,所有类型为“浪漫”的电影等等

错误是:

ContextErrorException: Notice: Undefined index: genre in /(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php
实体类型如下所示:

/**
 * Genre entity.
 *
 */

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Genre.
 *
 * @package Model
 * @ORM\Table(name="genre")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Genre")
 */
class Genre
{
 /**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Name.
 *
 * @ORM\Column(
 *     name="name",
 *     type="string",
 *     length=128,
 *     nullable=false
 * )
 *
 * @var string $name
 */
private $name;

/**
 * Movies array
 *
 * @ORM\OneToMany(
 *      targetEntity="AppBundle\Entity\Movie",
 *    mappedBy="genre"
 * )
 */
protected $movies;

/**
 * Add movies.
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function addMovie(\AppBundle\Entity\Movie $movies)
{
    $this->movies[] = $movies;
}
/**
 * Remove movies
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function removeAnswer(\AppBundle\Entity\Movie $movies)
{
    $this->movies->removeElement($movies);
}
/**
 * Get movies.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getMovies()
{
    return $this->movies;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->movies = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
 * Remove movies
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function removeMovie(\AppBundle\Entity\Movie $movies)
{
    $this->movies->removeElement($movies);
}

/**
 * Get Id.
 *
 * @return integer Result
 */
public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
}

/**
 * Set name.
 *
 * @param string $name Name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * Get name.
 *
 * @return string Name
 */
public function getName()
{
    return $this->name;
}
}
/**
 * Movie entity.
 */

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

/**
 * Class Movie.
 *
 * @package Model
 * @ORM\Table(name="movies")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
 */
class Movie
{

/**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Title.
 *
 * @ORM\Column(
 *     name="title",
 *     type="string",
 *     length=255,
 *     nullable=false
 * )
 *
 * @var string $title
 */
private $title;

/**
 * Notes.
 *
 * @ORM\Column(
 *     name="notes",
 *     type="text",
 *     nullable=true
 * )
 *
 * @var string $notes
 */
private $notes;

/**
 * Genre array
 *
 * @ORM\ManyToOne(targetEntity="Genre")
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 * )
 *
 * @var \Doctrine\Common\Collections\ArrayCollection $genres
 */
 protected $genres;


/**
 * Constructor.
 */
public function __construct()
{
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get id.
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set id.
 *
 * @param integer $id
 */
public function setId($id)
{
    $this->id = $id;
}


/**
 * Set title.
 *
 * @param string $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

/**
 * Get title.
 *
 * @return string 
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set notes.
 *
 * @param string $notes
 */
public function setNotes($notes)
{
    $this->notes = $notes;
}

/**
 * Get notes.
 *
 * @return string 
 */
public function getNotes()
{
    return $this->notes;
}

/**
 * Add genre
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function addGenre(\AppBundle\Entity\Genre $genres)
{
    $this->genres[] = $genres;

    return $this;
}  

/**
 * Remove genre
 *
 * @param \AppBundle\Entity\Genre $genres
 */
public function removeGenre(\AppBundle\Entity\Genre $genres)
{
    $this->generes->removeElement($generes);
}  

/**
 * Get genre
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getGenre()
{
    return $this->genres;
}  

/**
 * Set genre
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function setGenre(\AppBundle\Entity\Genre $genres = null)
{
    $this->genres = $genres;

    return $this;
}
虽然电影看起来像这样:

/**
 * Genre entity.
 *
 */

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Genre.
 *
 * @package Model
 * @ORM\Table(name="genre")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Genre")
 */
class Genre
{
 /**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Name.
 *
 * @ORM\Column(
 *     name="name",
 *     type="string",
 *     length=128,
 *     nullable=false
 * )
 *
 * @var string $name
 */
private $name;

/**
 * Movies array
 *
 * @ORM\OneToMany(
 *      targetEntity="AppBundle\Entity\Movie",
 *    mappedBy="genre"
 * )
 */
protected $movies;

/**
 * Add movies.
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function addMovie(\AppBundle\Entity\Movie $movies)
{
    $this->movies[] = $movies;
}
/**
 * Remove movies
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function removeAnswer(\AppBundle\Entity\Movie $movies)
{
    $this->movies->removeElement($movies);
}
/**
 * Get movies.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getMovies()
{
    return $this->movies;
}
/**
 * Constructor
 */
public function __construct()
{
    $this->movies = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
 * Remove movies
 *
 * @param \AppBundle\Entity\Movie $movies Movies
 * 
 * @return mixed
 */
public function removeMovie(\AppBundle\Entity\Movie $movies)
{
    $this->movies->removeElement($movies);
}

/**
 * Get Id.
 *
 * @return integer Result
 */
public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
}

/**
 * Set name.
 *
 * @param string $name Name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * Get name.
 *
 * @return string Name
 */
public function getName()
{
    return $this->name;
}
}
/**
 * Movie entity.
 */

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

/**
 * Class Movie.
 *
 * @package Model
 * @ORM\Table(name="movies")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Movie")
 */
class Movie
{

/**
 * Id.
 *
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 *
 * @var integer $id
 */
private $id;

/**
 * Title.
 *
 * @ORM\Column(
 *     name="title",
 *     type="string",
 *     length=255,
 *     nullable=false
 * )
 *
 * @var string $title
 */
private $title;

/**
 * Notes.
 *
 * @ORM\Column(
 *     name="notes",
 *     type="text",
 *     nullable=true
 * )
 *
 * @var string $notes
 */
private $notes;

/**
 * Genre array
 *
 * @ORM\ManyToOne(targetEntity="Genre")
 * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
 * )
 *
 * @var \Doctrine\Common\Collections\ArrayCollection $genres
 */
 protected $genres;


/**
 * Constructor.
 */
public function __construct()
{
    $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get id.
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set id.
 *
 * @param integer $id
 */
public function setId($id)
{
    $this->id = $id;
}


/**
 * Set title.
 *
 * @param string $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

/**
 * Get title.
 *
 * @return string 
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set notes.
 *
 * @param string $notes
 */
public function setNotes($notes)
{
    $this->notes = $notes;
}

/**
 * Get notes.
 *
 * @return string 
 */
public function getNotes()
{
    return $this->notes;
}

/**
 * Add genre
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function addGenre(\AppBundle\Entity\Genre $genres)
{
    $this->genres[] = $genres;

    return $this;
}  

/**
 * Remove genre
 *
 * @param \AppBundle\Entity\Genre $genres
 */
public function removeGenre(\AppBundle\Entity\Genre $genres)
{
    $this->generes->removeElement($generes);
}  

/**
 * Get genre
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getGenre()
{
    return $this->genres;
}  

/**
 * Set genre
 *
 * @param \AppBundle\Entity\Genre $genres
 * @return Movie
 */
public function setGenre(\AppBundle\Entity\Genre $genres = null)
{
    $this->genres = $genres;

    return $this;
}
在尝试这样做的同时,我在GenreController中使用了:

 public function viewAction(Genre $genre)
{
    if (!$genre) {
        throw new NotFoundHttpException('Genre not found!');
    }

    $movies= $genre->getMovies();

    return $this->templating->renderResponse(
        'AppBundle:Genre:view.html.twig',
        array('genre' => $genre)
    );
}
现在,每次我尝试生成页面时,都会收到一条错误消息:

ContextErrorException: Notice: Undefined index: genre in /home/(...)/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1758 

这是我第一次做这样的事情,所以我完全意识到它可能充满了错误,但我不知道这是从哪里来的,所以我真的很难做任何事情。如果有人能告诉我我的错误,我将不胜感激。

实体名称是类型,但您在代码的某些部分将其引用为Genre

是的,这是正确的,尽管这不是主要问题。在我的文件中,我只有“genre”,虽然我知道这在技术上是一个拼写错误,但我想稍后再处理它。然后可能是Movies类中的$geners变量。关于genre类上的$movies关系,您用单数写了:mappedBy=“genre”,虽然variabla的名称是pluralNo,但不是它。就像我说的,在我自己的文件中,名字总是“genre”。在这里,我想改变它,所以它看起来会更好,但出于某种原因,一部分通用被改变了,而其余的保持不变。无论如何,这在我的文件中是正确的,所以这不是问题的根源。