Image created with Canva

What Are the Best 3 PHP Traits for Symfony?

Serghei Pogor
6 min readApr 2, 2024

--

As an experienced PHP Symfony developer, I’ve been working on lots of different projects for many years. For the last 4 years, I’ve been focusing mainly on Symfony because it fits perfectly with how I structure my projects.

Throughout my career, I’ve mostly been involved in big projects, and I’ve learned that even small problems at the start can turn into big headaches later on.

🚀 Let’s talk about some PHP magic in Symfony!

One thing many PHP developers find tricky is managing entities in Symfony projects.

Today, I want to share 3 PHP Symfony Traits I always use to make managing entities easier and make projects grow smoothly.

Image created with Canva

Instead of using traditional integer IDs for identifying entities, using Universally Unique Identifiers (Uuids) has become increasingly popular in modern web development. Here’s why it’s a great choice:

  1. Uniqueness: Uuids are designed to be globally unique, meaning the chance of a collision (two entities having the same ID) is astronomically low. This ensures data integrity, especially in distributed systems where generating unique IDs can be challenging.
  2. Security: Unlike sequential integer IDs, which can expose the number of records and potentially lead to security vulnerabilities, Uuids are randomly generated and don’t reveal any patterns about the data.
  3. Scalability: As projects grow and scale, managing integer IDs can become cumbersome, especially when dealing with sharded databases or distributed systems. Uuids alleviate this burden by removing the need for centralized ID generation and coordination.
  4. Flexibility: Uuids are not tied to a specific database or system, making them ideal for interoperability across different platforms and technologies. This flexibility future-proofs your application architecture and allows for seamless integration with various services.

Overall, using Uuids as identifiers for entities in all projects offers numerous benefits, including enhanced uniqueness, security, scalability, and flexibility. It’s a modern approach that aligns well with the demands of contemporary web development.

<?php

declare(strict_types=1);

namespace App\Doctrine\Traits;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
use Symfony\Component\Uid\Uuid;

trait IdentifierTrait
{
#[ORM\Id]
#[ORM\Column(type: "uuid", unique: true)]
#[ORM\GeneratedValue(strategy: "CUSTOM")]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?Uuid $id = null;

/**
* @return Uuid|null
*/
final public function getId(): ?Uuid
{
return $this->id;
}
}

As a personal tip, let me share a valuable insight.

Some time ago, I worked as a Website Scraper, developing bots to gather data from various websites. Essentially, this involved crawling sites to collect data. Believe it or not, when websites utilized integer IDs instead of Uuids, the entire scraping process could be completed in less than 5 minutes.

So using traditional integer IDs instead of Uuids can pose security risks. Integer IDs can inadvertently expose patterns in your data, making it easier for attackers to predict and manipulate. On the other hand, Uuids offer a higher level of security by being globally unique and randomly generated, thus reducing the risk of data breaches and unauthorized access. So, from a security standpoint, opting for Uuids over integer IDs is definitely the way to go.

Image created with Canva

Alright, let’s break down the Status Trait.

You’ve got a bunch of entities in your Symfony application, and each one has its own state or status. Maybe it’s active, inactive, pending, or something else entirely. That’s where the Status Trait comes in handy.

  1. Keep Things Organized: With the “Status” trait, you can easily sort and filter your entities based on their status. It’s like having built-in folders to keep things tidy in your application’s database.
  2. Control Visibility: You can control who sees what by setting different statuses for your entities. For example, you might want to hide inactive items from public view while still keeping them around for reference behind the scenes.
  3. Manage Workflows: Ever had entities go through different stages or approvals? The “Status” trait helps you manage that. Think of it as setting up a checklist for your entities to follow as they move through your application.
  4. Interact with Users: Changing statuses can trigger all sorts of actions or notifications in your application. It’s a great way to keep users in the loop and make sure everyone’s on the same page.

So, whether you’re keeping track of orders, users, or anything else in your Symfony project, the Status Trait is like your trusty sidekick, helping you stay organized and in control.

<?php

namespace App\Doctrine\Traits;

use App\Enum\StatusType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\Choice;

trait StatusTrait
{

const array STATUS_TYPES = [
StatusType::ACTIVE->value,
StatusType::INACTIVE->value,
StatusType::BLOCKED->value,
StatusType::DELETED->value,
StatusType::PENDING->value,
];

#[ORM\Column(length: 50)]
#[Choice(
choices: self::STATUS_TYPES,
)]
protected string $status = StatusType::ACTIVE->value;

final public function getStatus(): string
{
return $this->status;
}

final public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
}
Image created with Canva

It’s like a smart tool for your Symfony entities that deals with dates automatically.

Here’s the deal:

  1. Time Tracker: This trait makes sure each entity knows when it was born (createdAt) and when it last got updated (updatedAt).
  2. Automatic Handling: Just attach this trait to your entity, and it takes care of all the date stuff. You don’t have to worry about updating dates manually — it’s all done for you.
  3. Organized Records: By setting timestamps automatically, this trait helps keep your data organized and easy to understand. You can trust that the dates are always correct.
  4. Background Magic: Using special tricks called prePersist and preUpdate, the TimestampTrait ensures dates are set correctly whenever you save or update an entity. It's like having a date expert working quietly in the background.

In simple terms, the TimestampTrait simplifies date management in your Symfony project. It’s perfect for developers who want their code to be efficient and their dates to be accurate without any extra hassle.

<?php

namespace App\Doctrine\Traits;

use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;

#[ORM\HasLifecycleCallbacks]
trait TimestampTrait
{
#[ORM\Column(nullable: false)]
protected DateTimeImmutable $createdAt;

#[ORM\Column(nullable: true)]
protected ?DateTimeImmutable $updatedAt;

public function __construct()
{
$this->createdAt = new DateTimeImmutable();
$this->updatedAt = new DateTimeImmutable();
}

final public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}

final public function setCreatedAt(DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}

final public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}

final public function setUpdatedAt(DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}

#[ORM\PrePersist]
final public function prePersist(): void
{
$this->createdAt = new DateTimeImmutable();
}

#[ORM\PreUpdate]
final public function preUpdate(): void
{
$this->updatedAt = new DateTimeImmutable();
}
}

Using the three traits in your Symfony project is straightforward.

Here’s a simplified example of how you might use these traits in an entity class:

<?php

namespace App\Entity;

use App\Doctrine\Traits\IdentifierTrait;
use App\Doctrine\Traits\StatusTrait;
use App\Doctrine\Traits\TimestampTrait;
use Doctrine\ORM\Mapping as ORM;

#[ORM\HasLifecycleCallbacks]
class YourEntity
{
use IdentifierTrait;
use StatusTrait;
use TimestampTrait;
}

By including these traits in your entity classes, you can leverage their functionality to enhance the management and organization of your Symfony project’s entities.

In conclusion, integrating the UuidTrait, StatusTrait, and TimestampTrait into your Symfony project is more than just a technical choice; it’s a strategic decision that enhances both the functionality and robustness of your application.

By incorporating these traits into your entity classes, you’re not just adding code snippets — you’re imbuing your project with powerful tools that streamline entity management, bolster data integrity, and elevate user experience.

🔔 Click Subscribe to catch more coding fun.
👏🏻 Love it? Give a big clap.
💬 Got a cool idea or funny coding joke? Drop it in the comments.

Share these tips with your fellow developers to help each other succeed together.

Thanks for hanging out and reading. You rock! 🚀

Hold on a sec!!! Want more of my fun stuff in your inbox? Sign up here! 📩

--

--

Serghei Pogor
Serghei Pogor

Written by Serghei Pogor

Good code is its own best documentation

Responses (1)