为什么学说getter方法返回ID的null



我在Symfony项目中使用类表"继承设置"中使用自动生成的Getters。getId()返回null,而其他所有GETTER都可以使用。你能发现任何问题吗?我应该搜索什么?我手动导入数据库条目,但我认为这不是原因。

抽象父型实体

//src/Acme/WebzineBundle/Entity/Content.php
namespace AcmeWebzineBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Content
 *
 * @ORMTable()
 * @ORMEntity
 * @ORMInheritanceType("JOINED")
 * @ORMDiscriminatorColumn(name="heading", type="integer")
 * @ORMDiscriminatorMap({
 *   0 = "Review"
 * })
 */
abstract class Content
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var DateTime
     *
     * @ORMColumn(name="edited", type="date")
     */
    private $edited;

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

儿童实体

//src/Acme/WebzineBundle/Entity/Review.php
namespace AcmeWebzineBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Review articles
 *
 * @ORMTable()
 * @ORMEntity
 */
class Review extends Content
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     *
     * @ORMColumn(name="title", type="string", length=127)
     */
    private $title;

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

下一个外键约束在儿童实体的表上:

CONSTRAINT `FK_7EEF84F0BF396750` FOREIGN KEY (`id`) REFERENCES `Content` (`id`)
ON DELETE CASCADE

查询

//src/Acme/AdminBundle/Controller/MainController.php
namespace AcmeAdminBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
class MainController extends Controller
{
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
            'SELECT post FROM AcmeWebzineBundle:Content post
             ORDER BY post.edited DESC'
        );
        $query->setMaxResults(30);
        $posts = $query->getResult();
        $latest_post = $posts[0];
        return $this->render('AcmeAdminBundle:Main:index.html.twig', array(
            'posts' => $posts,
            'id' => gettype($latest_post->getId()), // This returns null!
            'edited' => $latest_post->getEdited(),  // Any other getter works
            'title' => $latest_post->getTitle(),    // also from the child entity.
        ));
    }
}

您需要从子类删除id属性和getId()方法

//src/Acme/WebzineBundle/Entity/Review.php
namespace AcmeWebzineBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * Review articles
 *
 * @ORMTable()
 * @ORMEntity
 */
class Review extends Content
{ 
    /**
     * @var string
     *
     * @ORMColumn(name="title", type="string", length=127)
     */
    private $title;

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

您无法创建Content类的对象。

更好的仅使用Review类中的唯一属性和方法,因为其他属性和方法是从摘要Content类中继承的。

相关内容

  • 没有找到相关文章

最新更新