Поле smallfloat
Поле smallfloat создаёт колонку
для хранения дробного числа малой точности.
Оно похоже на поле float, но использует
тип данных с меньшим объёмом занимаемой памяти.
Первым параметром передаётся имя поля,
вторым - массив опций. Значение можно хранить
как положительное, так и отрицательное.
Синтаксис
#[ORM\Column(type: 'smallfloat', options: [])]
Пример
Давайте создадим сущность Product
с полем price типа smallfloat:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;
#[ORM\Column(type: 'smallfloat', nullable: true)]
private ?float $price = null;
public function getId(): int
{
return $this->id;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): self
{
$this->price = $price;
return $this;
}
}
?>
Пример
Давайте запишем значение в поле price
и выведем его:
<?php
namespace App\Controller;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ProductController extends AbstractController
{
#[Route('/product', name: 'product_create')]
public function create(EntityManagerInterface $em): Response
{
$product = new Product();
$product->setPrice(19.99);
$em->persist($product);
$em->flush();
return new Response((string) $product->getPrice());
}
}
?>
Результат выполнения кода:
"19.99"
Пример
Давайте зададим точность и масштаб через опции поля:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;
#[ORM\Column(
type: 'smallfloat',
precision: 4,
scale: 2,
nullable: true
)]
private ?float $price = null;
public function getId(): int
{
return $this->id;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): self
{
$this->price = $price;
return $this;
}
}
?>
Теперь поле хранит число с четырьмя цифрами всего и двумя после запятой.