Doctrine messing up queries on Entities with an Extends

那年仲夏 提交于 2020-12-06 04:15:05

问题


My task is (or was) simple: There is a variety but finite types of "Post", each with a vastly different set of properties while all sharing a couple core properties (like: Title, Content, Published, etc)

What I set out to do, was create a base Post entity with all the shared properties (as mentioned above), and have this entity as sort of a "Governor" entity, but more on that in a second. All the different Category entities would extend off of this base Post entity.

I did it this way mostly to appeal to the strengths of the Symfony Form API. There are just way too many fields per different Category type to warrant not doing something a bit automated. With the Form API, I can have Symfony use Doctrine to automatically create the editable fields on a page and populate each field with the acquired value when editing (or blank if unpopulated or new), without me having to individually provide them in HTML.

When I said "Governor" I meant I also wanted the base Post to have it's own Repository, and ideally no associated table. This repository could be used to do certain tasks which is Post Type agnostic, such as a findAll() override that would query each of the Category repositories and merge the results into one result (for example)

Ideally I just wanted to define the unique property of each category in exactly one location, the Entity, and it would reflect across the entirety of the site. Rather than defining it in the entity, the Edit Form, the front end, table fields, etc.

the problem

It seems to be messing up the built in queries which I have not overridden.

consider (please note the bolded text):

An exception occurred while executing 'SELECT t1.id AS id2, t1.title AS title3, t1.slug AS slug4, t1.date_added AS date_added5, t1.date_updated AS date_updated6, t1.content AS content7, t1.published AS published8, t1.uid AS uid9, [SENSITIVE FIELDS REDACTED] FROM PostCategoryTest t1 WHERE t0.id = ?' with params ["1"]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' in 'where clause'

This is caused when I call a basic $this->getDoctrine()->getRepository(PostCategoryTest::DoctrinePath)->find(1)

The source

CommonBundle/Entity/Post.php

<?php

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Post
 *
 * @ORM\Entity(repositoryClass="CommonBundle\Entity\PostRepository")
 */
abstract class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    protected $title;

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255)
     */
    protected $slug;

    //... and so on and so fourth
}

CommonBundle/Entity/PostCategoryTest.php

<?php

namespace CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * Post
 *
 * @ORM\Table(options={"comment":"Post subclass"})
 * @ORM\Entity(repositoryClass="CommonBundle\Entity\PostCategoryTestRepository")
 */
class PostCategoryTest extends Post
{
    const DoctrinePath = "CommonBundle:PostCategoryTest";

    // ... all sensitive information redacted, sorry
    // setup just like a standard entity however, nothing special 
}

I do understand there's many more ways to accomplish this, and I may just go ahead and do that as this has provided a few more headaches than I had expected. But for sake of education, I'd like to understand why Doctrine is doing this exactly. This problem never happened when extendsing BaseUser from FOSUserBundle


回答1:


According to Doctrine's documentation, there are 3 ways to extend a class:

  • Mapped superclass: the parent class is not an entity.
  • Single table inheritance: one big table for all data (including the extra fields).
  • Class table inheritance: one main table, plus an extra table for each entity with extra fields.

You either want the single table inheritance or the class table inheritance.

The mapped superclass won't work because you want the parent class Post to be a standalone entity. In that case the Post class you should not define it as abstract.

Unless you have a large amount of extra fields you should probably use single table inheritance.



来源:https://stackoverflow.com/questions/30204048/doctrine-messing-up-queries-on-entities-with-an-extends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!