Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

php - Symfony 4.2 - Doctrine / schema:validate => The table with name 'fablab.appartment_user already exists

beginner here i'm trying to do an API with Api-platform and Symfony. Actually i want to create my database schema and start testing it but when using php bin/console doctrine:schema:validate, i got this error and i cant figure out how to fix it : The table with name 'mydb.appartment_user' already exists

Unfortunately i didnt found an answer on google to my problem :'(

Here is my appartement entity :

<?php

namespace AppEntity;

use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryAppartmentRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

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

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

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

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

    /**
     * @ORMManyToMany(targetEntity=User::class, inversedBy="occupied_appartments")
     */
    private $current_tenant;

    /**
     * @ORMManyToMany(targetEntity=User::class, inversedBy="rented_appartments")
     */
    private $landlord;

    /**
     * @ORMOneToOne(targetEntity=Lock::class, cascade={"persist", "remove"})
     * @ORMJoinColumn(nullable=false)
     */
    private $lock_id;

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

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

    public function getAdress(): ?string
    {
        return $this->adress;
    }

    public function setAdress(string $adress): self
    {
        $this->adress = $adress;

        return $this;
    }

    public function getLatitude(): ?string
    {
        return $this->latitude;
    }

    public function setLatitude(string $latitude): self
    {
        $this->latitude = $latitude;

        return $this;
    }

    public function getLongitude(): ?string
    {
        return $this->longitude;
    }

    public function setLongitude(string $longitude): self
    {
        $this->longitude = $longitude;

        return $this;
    }

    /**
     * @return Collection|User[]
     */
    public function getCurrentTenant(): Collection
    {
        return $this->current_tenant;
    }

    public function addCurrentTenant(User $currentTenant): self
    {
        if (!$this->current_tenant->contains($currentTenant)) {
            $this->current_tenant[] = $currentTenant;
        }

        return $this;
    }

    public function removeCurrentTenant(User $currentTenant): self
    {
        $this->current_tenant->removeElement($currentTenant);

        return $this;
    }

    /**
     * @return Collection|User[]
     */
    public function getLandlord(): Collection
    {
        return $this->landlord;
    }

    public function addLandlord(User $landlord): self
    {
        if (!$this->landlord->contains($landlord)) {
            $this->landlord[] = $landlord;
        }

        return $this;
    }

    public function removeLandlord(User $landlord): self
    {
        $this->landlord->removeElement($landlord);

        return $this;
    }

    public function getLockId(): ?Lock
    {
        return $this->lock_id;
    }

    public function setLockId(Lock $lock_id): self
    {
        $this->lock_id = $lock_id;

        return $this;
    }
}

and my user entity :

<?php

namespace AppEntity;

use AppRepositoryUserRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;

/**
 * @ORMEntity(repositoryClass=UserRepository::class)
 * @ORMTable(name="`user`")
 */
class User implements UserInterface
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=180, unique=true)
     */
    private $email;

    /**
     * @ORMColumn(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORMColumn(type="string")
     */
    private $password;

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

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

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

    /**
     * @ORMManyToMany(targetEntity=Appartment::class, mappedBy="current_tenant")
     */
    private $occupied_appartments;

    /**
     * @ORMManyToMany(targetEntity=Appartment::class, mappedBy="landlord")
     */
    private $rented_appartments;

    /**
     * @ORMManyToMany(targetEntity=Rental::class, mappedBy="tenant_id")
     */
    private $tenant_rentals;

    /**
     * @ORMManyToMany(targetEntity=Rental::class, mappedBy="landlord_id")
     */
    private $landlord_rentals;

    public function __construct()
    {
        $this->occupied_appartments = new ArrayCollection();
        $this->rented_appartments = new ArrayCollection();
        $this->tenant_rentals = new ArrayCollection();
        $this->landlord_rentals = new ArrayCollection();
    }

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

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->email;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getFirstname(): ?string
    {
        return $this->firstname;
    }

    public function setFirstname(string $firstname): self
    {
        $this->firstname = $firstname;

        return $this;
    }

    public function getLastname(): ?string
    {
        return $this->lastname;
    }

    public function setLastname(string $lastname): self
    {
        $this->lastname = $lastname;

        return $this;
    }

    public function getProfilePicture(): ?string
    {
        return $this->profile_picture;
    }

    public function setProfilePicture(?string $profile_picture): self
    {
        $this->profile_picture = $profile_picture;

        return $this;
    }

    /**
     * @return Collection|Appartment[]
     */
    public function getOccupiedAppartments(): Collection
    {
        return $this->occupied_appartments;
    }

    public function addOccupiedAppartment(Appartment $occupied_appartments): self
    {
        if (!$this->occupied_appartments->contains($occupied_appartments)) {
            $this->occupied_appartments[] = $occupied_appartments;
            $occupied_appartments->addCurrentTenant($this);
        }

        return $this;
    }

    public function removeOccupiedAppartment(Appartment $occupied_appartments): self
    {
        if ($this->occupied_appartments->removeElement($occupied_appartments)) {
            $occupied_appartments->removeCurrentTenant($this);
        }

        return $this;
    }

    /**
     * @return Collection|Appartment[]
     */
    public function getRentedAppartments(): Collection
    {
        return $this->rented_appartments;
    }

    public function addRentedAppartment(Appartment $rentedAppartment): self
    {
        if (!$this->rented_appartments->contains($rentedAppartment)) {
            $this->rented_appartments[] = $rentedAppartment;
            $rentedAppartment->addLandlord($this);
        }

        return $this;
    }

    public function removeRentedAppartment(Appartment $rentedAppartment): self
    {
        if ($this->rented_appartments->removeElement($rentedAppartment)) {
            $rentedAppartment->removeLandlord($this);
        }

        return $this;
    }

    /**
     * @return Collection|Rental[]
     */
    public function getTenantRentals(): Collection
    {
        return $this->tenant_rentals;
    }

    public function addTenantRental(Rental $tenantRental): self
    {
        if (!$this->tenant_rentals->contains($tenantRental)) {
            $this->tenant_rentals[] = $tenantRental;
            $tenantRental->addTenantId($this);
        }

        return $this;
    }

    public function removeTenantRental(Rental $tenantRental): self
    {
        if ($this->tenant_rentals->removeElement($tenantRental)) {
            $tenantRental->removeTenantId($this);
        }

        return $this;
    }

    /**
     * @return Collection|Rental[]
     */
    public function getLandlordRentals(): Collection
    {
        return $this->landlord_rentals;
    }

    public function addLandlordRental(Rental $landlordRental): self
    {
        if (!$this->landlord_rentals->contains($landlordRental)) {
            $this->landlord_rentals[] = $landlordRental;
            $landlordRental->addLandlordId($this);
        }

        return $this;
    }

    public function removeLandlordRental(Rental $landlordRental): self
    {
        if ($this->landlord_rentals->removeElement($landlordRental)) {
            $landlordRental->removeLandlordId($this);
        }

        return $this;
    }
}

I tried to clear-cache and to force but it didnt work. Thank's in advance for help and ask me if I forget any details !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

add a JoinTable annotation for properties which have ManyToMany relation in Appartment entity

/**
 * @ORMManyToMany(targetEntity=User::class, inversedBy="occupied_appartments")
 * @ORMJoinTable(name="appartment_current_tenant")
 */
private $current_tenant;

/**
 * @ORMManyToMany(targetEntity=User::class, inversedBy="rented_appartments")
 * @ORMJoinTable(name="appartment_landlord")
 */
private $landlord;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...