0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Symfony5のエンティティクラスの記述メモ

Posted at

Symfony5のエンティティクラスの書き方について

カラムの属性を記述する方法はどうすればいい?

結論、こんな感じで書きます。

Person.php
<?php

namespace App\Entity;

use App\Repository\PersonRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PersonRepository::class)]
class Person
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Name]
    #[ORM\Column(length:255,nullable:true)]
    private ?string $name = null;

    #[ORM\Mail]
    #[ORM\Column(length:255,nullable:true)]
    private ?string $mail = null;
    
    #[ORM\Age]
    #[ORM\Column(nullable:true)]
    private ?int $age = null;

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

    public function getName():?string
    {
        return $this->name;
    }

    public function getMail():?string
    {
        return $this->mail;
    }

    public function getAge():?int
    {
        return $this->age;
    }

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

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

    public function setAge(int $age):self
    {
        $this->age = $age;
        return $this;
    }
}

たとえば。nameカラムの属性情報は、下記の通り記載します。

Person.php
    #[ORM\Name]
    #[ORM\Column(length:255,nullable:true)]
    private ?string $name = null;

symfonyのバージョンによって記載は少し違うので、バージョンごとに公式サイトで確認してみましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?