status

问题描述 投票:0回答:1
1 - Pending

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
php symfony doctrine-orm annotations
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.