Symfony private message system with unlimited participants

Currently I have a message system between two users (like ebay), and you can make soft delete from the current conversation (the person who deleted it cannot see the message but the other participant yes)

Here you have my entities:

Message.php

<?php

namespace AppEntity;

use AppRepositoryMessageRepository;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=MessageRepository::class)
 */
class Message
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=User::class)
     * @ORMJoinColumn(nullable=false)
     */
    private $creator;

    /**
     * @ORMManyToOne(targetEntity=User::class)
     * @ORMJoinColumn(nullable=false)
     */
    private $receiver;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $title;

    /**
     * @ORMColumn(type="text")
     */
    private $content;

    /**
     * @ORMColumn(type="integer")
     */
    private $readed;

    /**
     * @ORMColumn(type="integer")
     */
    private $delete_creator;

    /**
     * @ORMColumn(type="integer")
     */
    private $delete_receiver;

    /**
     * @ORMColumn(type="datetime")
     */
    private $created;

    /**
     * @ORMManyToOne(targetEntity=Chat::class, inversedBy="messages")
     */
    private $chat;

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

    public function getCreator(): ?User
    {
        return $this->creator;
    }

    public function setCreator(?User $creator): self
    {
        $this->creator = $creator;

        return $this;
    }

    public function getReceiver(): ?User
    {
        return $this->receiver;
    }

    public function setReceiver(?User $receiver): self
    {
        $this->receiver = $receiver;

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getReaded(): ?int
    {
        return $this->readed;
    }

    public function setReaded(int $readed): self
    {
        $this->readed = $readed;

        return $this;
    }

    public function getDeleteCreator(): ?int
    {
        return $this->delete_creator;
    }

    public function setDeleteCreator(int $delete_creator): self
    {
        $this->delete_creator = $delete_creator;

        return $this;
    }

    public function getDeleteReceiver(): ?int
    {
        return $this->delete_receiver;
    }

    public function setDeleteReceiver(int $delete_receiver): self
    {
        $this->delete_receiver = $delete_receiver;

        return $this;
    }

    public function getCreated(): ?DateTimeInterface
    {
        return $this->created;
    }

    public function setCreated(DateTimeInterface $created): self
    {
        $this->created = $created;

        return $this;
    }

    public function getChat(): ?Chat
    {
        return $this->chat;
    }

    public function setChat(?Chat $chat): self
    {
        $this->chat = $chat;

        return $this;
    }
}

Chat.php

<?php

namespace AppEntity;

use AppRepositoryChatRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass=ChatRepository::class)
 */
class Chat
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMOneToMany(targetEntity=Message::class, mappedBy="chat")
     */
    private $messages;

    /**
     * @ORMManyToOne(targetEntity=User::class)
     * @ORMJoinColumn(nullable=false)
     */
    private $creator;

    public function __construct()
    {
        $this->messages = new ArrayCollection();
    }

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

    /**
     * @return Collection|Message[]
     */
    public function getMessages(): Collection
    {
        return $this->messages;
    }

    public function addMessage(Message $message): self
    {
        if (!$this->messages->contains($message)) {
            $this->messages[] = $message;
            $message->setChat($this);
        }

        return $this;
    }

    public function removeMessage(Message $message): self
    {
        if ($this->messages->contains($message)) {
            $this->messages->removeElement($message);
            // set the owning side to null (unless already changed)
            if ($message->getChat() === $this) {
                $message->setChat(null);
            }
        }

        return $this;
    }

    public function getCreator(): ?User
    {
        return $this->creator;
    }

    public function setCreator(?User $creator): self
    {
        $this->creator = $creator;

        return $this;
    }
}

Features I need to implement:

  1. Each participant from the conversation has the ability to invite more new members who will be able to read previous message history
  2. I need to show a list of received/invited messages and participants (like you have one conversation where you created it and you are talking with member A and member B, last message received is "I am fine")
  3. Each participant can exit the conversation which will make to stop him receiving new messages but remaining participants can read past messages.

I am not asking for a full solution, I am just stuck at the definition of my entities and I do not want to make some spaghetti code with a bad performance and extra tables in the database. So I need a bit help to redefine my database definitions.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *