<?php
namespace App\Entity;
use App\Repository\CarRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=CarRepository::class)
*/
class Car
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
public function getId(): ?int
{
return $this->id;
}
}
以及Suggest:
<?php
namespace App\Entity;
use App\Repository\SuggestRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=SuggestRepository::class)
*/
class Suggest
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity=Car::class)
*/
private $cars;
public function __construct()
{
$this->cars = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection<int, Car>
*/
public function getCars(): Collection
{
return $this->cars;
}
public function addCar(Car $car): self
{
if (!$this->cars->contains($car)) {
$this->cars[] = $car;
}
return $this;
}
public function removeCar(Car $car): self
{
$this->cars->removeElement($car);
return $this;
}
}
1条答案
按热度按时间ttcibm8c1#
假设您使用的是
make:entity
命令:如果您将cars
属性添加到您的Suggest
实体,并使其成为Car
的ManyToMany
关系,它不会将额外的属性添加到Car
类(您可以对该类说no
):Car
:以及
Suggest
: