2 - Approved
3 - Denied
Entity
我们有两个ManyToOne关系的实体,Article实体有三种状态。status ...
In annotations it's not possible. There are a few different methods you can try, I suggest a pre-persist event.
Possible
<?php
namespace App\Entity;
use App\Repository\ArticleRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ArticleRepository::class)
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $article_name;
/**
* @ORM\ManyToOne(targetEntity=ArticleStatus::class)
* @ORM\JoinColumn(nullable=false)
*/
private $status;
...
public function getStatus(): ?ArticleStatus
{
return $this->status;
}
public function setStatus(?ArticleStatus $status): self
{
$this->status = $status;
return $this;
}
}
duplicate
<?php
namespace App\Entity;
use App\Repository\ArticleStatusRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ArticleStatusRepository::class)
*/
class ArticleStatus
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=100, nullable=false)
*/
private $status;
public function getId(): ?int
{
return $this->id;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
}
ArticleStatus表的内容。
id Article.php