我今天的任务是编写一个鉴别器,这是我第一次了解Doctrine鉴别器的工作原理。阅读文档后,这就是我所做的。
我需要创建一个命令来更改博客/帖子作者,然后还更新有关事件的操作实体。
动作实体:
/**
* @ORM\Table(name="action")
* @ORM\Entity()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({"action" = "Action", "blog" = "Blog", "post" = "Post"})
*/
class Action
{ ...
eventCommand中的某处:
...
$action = new Action();
$action->setType('author_change');
$this->em->persist($action);
所有这些在运行命令后都有效。唯一的问题是动作鉴别器每次都保存“动作”,而不是“博客”或“发布”。
即使我这样强制,该列也不会更改:
$action->setDiscriminator('blog');
所以我认为我的问题是Action如何知道以及如何才能从地图中触发要使用的鉴别器?
答案 0 :(得分:1)
要检查的事情:
Action
,请确保Blog
和Post
都对其进行扩展Action
类中的所有属性都应受到保护(包括ID)我尝试了以下课程的设置:
操作:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ActionRepository")
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discriminator", type="string")
* @ORM\DiscriminatorMap({"action" = "Action", "blog" = "Blog", "post" = "Post"})
*/
class Action
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $name;
// ... getters and setters for $id and $name (notice they are also protected, not private !!!)
}
博客:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\BlogRepository")
*/
class Blog extends Action
{
/**
* @var string
* @ORM\Column(name="blog_prop", length=255, nullable=true)
*/
private $blogProp;
// ... getters and setters for $blogProp
}
帖子:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
*/
class Post extends Action
{
/**
* @var string
* @ORM\Column(name="post_prop", length=255, nullable=true)
*/
private $postProp;
// .... getters and setters for $postProp
}
Doctrine将为您的每个实体创建一个单独的表。存储子实体时,还将在父实体中添加一行,并在其中填充公共(继承)字段。子实体特有的属性将插入到它们各自的表中。这将导致以下数据库结构:
mysql> describe action;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| discriminator | varchar(255) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> describe blog;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| blog_prop | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
mysql> describe post;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| post_prop | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
和控制器:
/**
* @Route("/test", name="test")
*/
public function test()
{
$manager = $this->getDoctrine()->getManager();
$action = new Action();
$action->setName('my action');
$blog = new Blog();
$blog
->setName('my blog')
->setBlogProp('my blog prop');
$post = new Post();
$post
->setName('my post')
->setPostProp('my post prop');
$manager->persist($action);
$manager->persist($blog);
$manager->persist($post);
$manager->flush();
}
运行控制器代码后:
mysql> select * from action;
+----+-----------+---------------+
| id | name | discriminator |
+----+-----------+---------------+
| 1 | my post | post |
| 2 | my blog | blog |
| 3 | my action | action |
+----+-----------+---------------+
3 rows in set (0.00 sec)
mysql> select * from blog;
+----+--------------+
| id | blog_prop |
+----+--------------+
| 2 | my blog prop |
+----+--------------+
1 row in set (0.00 sec)
mysql> select * from post;
+----+--------------+
| id | post_prop |
+----+--------------+
| 1 | my post prop |
+----+--------------+
1 row in set (0.00 sec)