我有一些基本实体,它定义了所有(或至少大多数)其他实体应该拥有的一些字段。看起来像:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
abstract class BaseEntity {
protected $id;
protected $published;
protected $createdAt;
protected $updatedAt;
public function getId(): ?int
{
return $this->id;
}
等等……打手和二传手。
然后,我有实体,即扩展该基本实体的ArticleCategory:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
class ArticleCategory extends BaseEntity
{
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
{
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');
}
}
问题是,当我转到SONATA列表页而不是显示ArticleCategory类型中的所有实体时,我会得到一个SQL错误:
“字段列表”中的未知列“b0\uid”
查询如下:
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]
因此,Sonata从正确的表(项目类别)中获取数据,并为其创建一个别名“a1”,并且所有直接位于项目类别(标题)中的字段都会很好地收集到该别名。
但是,属于基类(baseEntity)sonata的所有其他字段都试图通过一些不存在的别名“b0_uuu”获取,查询当然失败了。
知道怎么解决这个问题吗?如何告诉Sonata所有字段都属于同一个表,即使它们属于两个实体类?