创建子实体对象时 Symfony 2 出错 class

Error in Symfony 2 when create object of child entity class

我有一个包含 3 个实体 class 的文件 /src/AppBundle/Entity/Questionnaire.php,我试图在其中实现 Single table 在 Symfony 2.7 上继承 Doctrine 2。 问卷是一个父摘要class,还有2个子classes FirstQuestionsSecondsQuestions 扩展 Questionnaire。我选择这个模型是因为我需要分2步在table中写入数据。该文件的代码如下:

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;
    }
}

所以问题是当我试图创建子对象 class(FirstQuestionsSecondsQuestions) 在控制器方法中,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'
        );
    }
}

可能我做错了什么或者没有设置任何重要的注释。谁能帮我?

ALL Entity 类 是映射实体层次结构的一部分,需要在@DiscriminatorMap 中指定。所以,是的,您的注释不正确。

Doctrine Single Table Inheritance

编辑

您还有另一个注释错误 - 您的子 类 都没有 @Entity 注释:

/**
 * FirstQuestions
 * @ORM\Entity()
 */
class FirstQuestions extends Questionnaire {

/**
 * SecondQuestions
 * @ORM\Entity()
 */
class SecondQuestions extends Questionnaire {

修复此问题后,我能够使用 Doctrine 的模式更新工具构建表并成功创建了一个 FirstQuestions 对象。

这将是那些烦人的小疏忽错误之一 - 一个额外的分号或其他您不希望出现的地方。我正在创建这个额外的答案,以便我可以准确地为您提供我正在使用的代码。希望您能够剪切和粘贴,用这个新代码替换您自己的文件,它会神奇地开始工作。

首先 - 为了证明这一点,这是我的(修改后的)输出:

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\Entity\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\Entity\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\Entity\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\Controller\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>'
        );
    }
}

而且只是为了确定...

app\config\routing.yml

test:
    resource:   "@AppBundle/Controller/TestController.php"
    type:       annotation

一定是一些愚蠢、烦人的小错误,没人在找。

希望这对您有所帮助...