我有一个带有3个实体类的文件 /src/AppBundle/Entity/Questionnaire.php ,我试图在Symfony 2.7上使用Doctrine 2实现单表继承。 调查问卷 是一个父抽象类,有2个子类 FirstQuestions 和 SecondsQuestions 扩展 调查问卷 。我之所以选择这个模型是因为我需要在表格中分两步编写数据。该文件的代码如下:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* @ORM\Entity
* @ORM\Table(name="questionnaire")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
/**
* FirstQuestions
*/
class FirstQuestions extends Questionnaire {
/**
* @var string
*
* @ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* @var integer
*
* @ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* @param string $firstName
*
* @return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
*
* @return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* @param string $email
*
* @return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* @param \DateTime $birthday
*
* @return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* @param integer $shoeSize
*
* @return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* @return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
/**
* SecondQuestions
*/
class SecondQuestions extends Questionnaire {
/**
* @var string
*
* @ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* @var string
*
* @ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* @var string
*
* @ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* @var \DateTime
*
* @ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* @var string
*
* @ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* @param string $favoriteIceCream
*
* @return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* @return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* @param string $favoriteSuperHero
*
* @return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* @return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* @param string $favoriteMovieStar
*
* @return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* @return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* @param \DateTime $endOfTheWorld
*
* @return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* @return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* @param string $superBowlWinner
*
* @return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* @return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
所以问题是当我尝试创建子类的对象时( FirstQuestions 或 SecondsQuestions 在控制器方法中,Symfony显示错误" 500内部服务器错误" 。带方法的控制器代码如下:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* @Route("/test", name="test")
*/
public function indexAction(Request $request)
{
$item = new FirstQuestions(); // everything works well without this line
return new Response(
'ok'
);
}
}
也许我做错了什么或者没有设置任何重要的注释。谁能帮我?
答案 0 :(得分:2)
这将是一个令人烦恼的小疏忽错误之一 - 一个额外的分号或某些你不会寻找的东西。我正在创建这个额外的答案,以便我可以准确地为您提供我正在使用的代码。希望您能够使用这个新代码进行剪切和粘贴,替换您自己的文件,它将神奇地开始工作。
首先 - 为了证明这一点,这里是我的(修改过的)输出:
Veromo\Bundle\CoreBundle\Entity\FirstQuestions Object
(
[firstName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[lastName:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[email:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[birthday:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[shoeSize:Veromo\Bundle\CoreBundle\Entity\FirstQuestions:private] =>
[id:Veromo\Bundle\CoreBundle\Entity\Questionnaire:private] =>
)
这表明我所做的与你不同的是使用我自己的开发环境的命名空间。
<强>的appbundle \实体\ Questionnaire.php 强>
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Questionnaire
*
* @ORM\Entity
* @ORM\Table(name="questionnaire")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"questionnaire"="Questionnaire", "firstquestions" = "FirstQuestions", "secondquestions" = "SecondQuestions"})
*/
abstract class Questionnaire {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
<强>的appbundle \实体\ FirstQuestions.php 强>
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* FirstQuestions
* @ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* @var string
*
* @ORM\Column(name="firstName", type="string", length=64)
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="lastName", type="string", length=64)
*/
private $lastName;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=32)
*/
private $email;
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="date")
*/
private $birthday;
/**
* @var integer
*
* @ORM\Column(name="shoeSize", type="integer")
*/
private $shoeSize;
/**
* Set firstName
*
* @param string $firstName
*
* @return Questionnaire
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
*
* @return Questionnaire
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Set email
*
* @param string $email
*
* @return Questionnaire
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set birthday
*
* @param \DateTime $birthday
*
* @return Questionnaire
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
/**
* Set shoeSize
*
* @param integer $shoeSize
*
* @return Questionnaire
*/
public function setShoeSize($shoeSize)
{
$this->shoeSize = $shoeSize;
return $this;
}
/**
* Get shoeSize
*
* @return integer
*/
public function getShoeSize()
{
return $this->shoeSize;
}
}
<强>的appbundle \实体\ SecondQuestions.php 强>
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SecondQuestions
* @ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
/**
* @var string
*
* @ORM\Column(name="favoriteIceCream", type="string", length=128)
*/
private $favoriteIceCream;
/**
* @var string
*
* @ORM\Column(name="favoriteSuperHero", type="string", length=32)
*/
private $favoriteSuperHero;
/**
* @var string
*
* @ORM\Column(name="favoriteMovieStar", type="string", length=64)
*/
private $favoriteMovieStar;
/**
* @var \DateTime
*
* @ORM\Column(name="endOfTheWorld", type="date")
*/
private $endOfTheWorld;
/**
* @var string
*
* @ORM\Column(name="superBowlWinner", type="string", length=32)
*/
private $superBowlWinner;
/**
* Set favoriteIceCream
*
* @param string $favoriteIceCream
*
* @return Questionnaire
*/
public function setFavoriteIceCream($favoriteIceCream)
{
$this->favoriteIceCream = $favoriteIceCream;
return $this;
}
/**
* Get favoriteIceCream
*
* @return string
*/
public function getFavoriteIceCream()
{
return $this->favoriteIceCream;
}
/**
* Set favoriteSuperHero
*
* @param string $favoriteSuperHero
*
* @return Questionnaire
*/
public function setFavoriteSuperHero($favoriteSuperHero)
{
$this->favoriteSuperHero = $favoriteSuperHero;
return $this;
}
/**
* Get favoriteSuperHero
*
* @return string
*/
public function getFavoriteSuperHero()
{
return $this->favoriteSuperHero;
}
/**
* Set favoriteMovieStar
*
* @param string $favoriteMovieStar
*
* @return Questionnaire
*/
public function setFavoriteMovieStar($favoriteMovieStar)
{
$this->favoriteMovieStar = $favoriteMovieStar;
return $this;
}
/**
* Get favoriteMovieStar
*
* @return string
*/
public function getFavoriteMovieStar()
{
return $this->favoriteMovieStar;
}
/**
* Set endOfTheWorld
*
* @param \DateTime $endOfTheWorld
*
* @return Questionnaire
*/
public function setEndOfTheWorld($endOfTheWorld)
{
$this->endOfTheWorld = $endOfTheWorld;
return $this;
}
/**
* Get endOfTheWorld
*
* @return \DateTime
*/
public function getEndOfTheWorld()
{
return $this->endOfTheWorld;
}
/**
* Set superBowlWinner
*
* @param string $superBowlWinner
*
* @return Questionnaire
*/
public function setSuperBowlWinner($superBowlWinner)
{
$this->superBowlWinner = $superBowlWinner;
return $this;
}
/**
* Get superBowlWinner
*
* @return string
*/
public function getSuperBowlWinner()
{
return $this->superBowlWinner;
}
}
<强>的appbundle \控制器\ TestController.php 强>
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Questionnaire;
use AppBundle\Entity\FirstQuestions;
use AppBundle\Entity\SecondQuestions;
class TestController extends Controller
{
/**
* @Route("/test",name="test")
*/
public function indexAction( Request $request )
{
$item = new FirstQuestions();
return new Response(
'<pre>'.print_r( $item, true ).'</pre>'
);
}
}
只是为了确定......
应用\设置\ routing.yml中强>
test:
resource: "@AppBundle/Controller/TestController.php"
type: annotation
GOT是一个愚蠢,烦人的小错误,没有人在寻找。
希望这会有所帮助......
答案 1 :(得分:1)
需要在@DiscriminatorMap中指定属于映射实体层次结构的所有实体类。所以,是的,你的注释是不正确的。
Doctrine Single Table Inheritance
修改强>
您有另一个注释错误 - 您的子类都没有@Entity注释:
/**
* FirstQuestions
* @ORM\Entity()
*/
class FirstQuestions extends Questionnaire {
/**
* SecondQuestions
* @ORM\Entity()
*/
class SecondQuestions extends Questionnaire {
修复此问题后,我可以使用Doctrine的架构更新工具构建表格并成功创建 FirstQuestions 对象。