skip to Main Content

I have been checking the documentation and got confused, can someone advise me how to add one-to-many relationships for existing tables? Adequately userid is supposed to refer to the user id.

Entity for a user that may have multiple doors

#[ORMEntity(repositoryClass: UserRepository::class)]
#[ORMTable(name: '`user`')]
#[UniqueEntity(fields: ['username'], message: '321')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;


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

Entity for the doors

#[ORMTable(name: '`doors`')]
#[UniqueEntity(fields: ['serialNumber'], message: '312132')]
#[ORMEntity(repositoryClass: DoorsRepository::class)]
class Doors
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn]
    private ?int $id = null;

    #[ORMColumn]
    private ?int $userid = null;

    private ?string $purchaseFilename = null;

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

    public function setId(int $id): static
    {
        $this->id = $id;

        return $this;
    }

    public function getUserid(): ?int
    {
        return $this->userid;
    }

}

2

Answers


  1. as your properties already exist, you have to update their annotations like based on Symfony Associations Doc and the type used.

        class Doors {
        #[ORMManyToOne(targetEntity: User::class, inversedBy: 'doors')]
        private User $user;
        
        public function getUser(): ?User 
        {
            return $this->user;
        }
        
        public function setUser(?User $user): self
        {
            $this->user = $user;
            
            return $this;
        }
    }
    
    class User {
        #[ORMOneToMany(targetEntity: Door::class, mappedBy: 'user')]
        private Collection $doors;
        
        public function __construct()
        {
            $this->doors = new ArrayCollection();
        }
        
        public function getDoors():Collection
        {
            return $this->doors;
        }
    }
    

    Then you will have to run make a new migration and run it using:

    php bin/console make:migration 
    php bin/console doctrine:migrations:migrate
    

    In addition, you may take a look at How to embed a collection of form to handle your forms.

    If it’s was helpull, make sure to accept the answer !

    Login or Signup to reply.
  2. You have an easy way to add relations in Symfony.

    You can use the command php bin/console make:entity.

    Then call your entity (in your example Doors), add a field user, declare type OneToMany (or see more relation types with the relation key), related the entity to User, and voila.

    Then php bin/console make:migration : it will write the change in migrations folder ; you’ll see it automatically add a field user_id in Door table, related to your user.id (as id is the identifier of your User entity).
    And php bin/console doctrine:migrations:migrate : queries will be executed in your db.

    I hope it will help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search