在奏鸣曲中定义扩展实体

时间:2019-02-11 13:04:46

标签: sonata-admin symfony4

我有一些基本实体,这些基本实体定义了所有(或至少大多数)其他实体都应具有的某些字段。看起来像:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity()
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks()
 */
abstract class BaseEntity {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $published;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $createdAt;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $updatedAt;



    public function getId(): ?int
    {
        return $this->id;
    }

等等...获取器和设置器。

然后我有一个实体,即扩展该BaseEntity的ArticleCategory:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ArticleCategoryRepository")
 */
class ArticleCategory extends BaseEntity
{

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;
        return $this;
    }
}

因此,它只是添加了一个额外的字段-标题。

然后,我有基本的管理类:

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Entity\BaseEntity;

class BaseAdmin extends AbstractAdmin
{

    /**
     * @param BaseEntity $entity
     */
    public function prePersist($entity)
    {
        $now = new \DateTime();
        $entity->setCreatedAt($now);
        $entity->setUpdatedAt($now);
        $entity->setPublished(true);
    }
}

然后是该ArticleCategory实体的管理类:

namespace App\Admin;

use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;

final class ArticleCategoryAdmin extends BaseAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', TextType::class);
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('title');
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('title');
    }
}

问题是当我进入奏鸣曲列表页面而不是在ArticleCategory类型中显示所有实体时,出现了SQL错误:

“字段列表”中的未知列“ b0_.id”

查询如下:

SELECT b0_.id AS id_0, b0_.published AS published_1, b0_.created_at AS created_at_2, b0_.updated_at AS updated_at_3, a1_.title AS title_4 
FROM article_category a1_ 
WHERE a1_.id IN (?, ?) 
ORDER BY a1_.id ASC' with params [2, 3]

因此,奏鸣曲正在从正确的表(article_category)中获取数据并为其创建别名“ a1_”,并且很好地收集了具有该别名的ArticleCategory(标题)内的所有字段。 但是,属于基类(BaseEntity)奏鸣曲的所有其他字段都试图通过某个不存在的别名“ b0_”获取,并且查询当然会失败。

任何想法如何解决这个问题?如何告诉索纳塔所有字段都属于同一个表,即使它们属于2个实体类呢?

1 个答案:

答案 0 :(得分:0)

发现了问题。在定义基本实体的一开始,我有:

/**
 * @ORM\Entity()
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks()
 */

由于“ Entity()”行的原因,这是不正确的。那必须去除。不能同时是实体和映射的晚餐类。

/**
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks()
 */