I have a database called cardb
which is populated by a SQL file which made some cars. They have ID's starting from 1 up to 30. When I try to create new Car it works the first time and creates a car with id 0 then when it tries to do it again it again tries to make an entry with 0.
Here is the error
An exception occurred while executing 'INSERT INTO cars (Make, Model, TravelledDistance) VALUES (?, ?, ?)' with params ["fock", "fock", 320]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY'
Here is my entity
/**
* Cars
*
* @ORM\Table(name="cars")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CarsRepository")
*/
class Cars
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Make", type="string", length=255)
*/
private $make;
/**
* @var string
*
* @ORM\Column(name="Model", type="string", length=255)
*/
private $model;
/**
* @var int
*
* @ORM\Column(name="TravelledDistance", type="bigint")
*/
private $travelledDistance;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Parts", inversedBy="cars")
* @ORM\JoinTable(
* name="PartCars",
* joinColumns={
* @ORM\JoinColumn(name="Part_Id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="Car_Id", referencedColumnName="id")
* })
*/
private $parts;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set make
*
* @param string $make
*
* @return Cars
*/
public function setMake($make)
{
$this->make = $make;
return $this;
}
/**
* Get make
*
* @return string
*/
public function getMake()
{
return $this->make;
}
/**
* Set model
*
* @param string $model
*
* @return Cars
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* Get model
*
* @return string
*/
public function getModel()
{
return $this->model;
}
/**
* Set travelledDistance
*
* @param integer $travelledDistance
*
* @return Cars
*/
public function setTravelledDistance($travelledDistance)
{
$this->travelledDistance = $travelledDistance;
return $this;
}
/**
* Get travelledDistance
*
* @return int
*/
public function getTravelledDistance()
{
return $this->travelledDistance;
}
/**
* @return mixed
*/
public function getParts()
{
return $this->parts;
}
}
It is auto generated Entity which has auto increment annotation.
What could be the problem?
The annotation
* @ORM\GeneratedValue(strategy="AUTO")
won't make PHP create a unique ID. You can see from the insert statement that PHP doesn't provide an ID value at all.
That annotation tells the ORM what kind of column it is, but it depends on the database to generate the value. You can check the documentation for more details.
Based on the way your code looks, it appears you used the console to generate the entity, but I assume the fact that the database isn't doing the id properly means that you created the cars
table by hand rather than using doctrine:schema:update
, which is fine if that's the way you need/want to do it, but you'll have to alter your table to make the id column an autoincrement.