学说 2 ORM\GeneratedValue(strategy="AUTO") 不起作用
Doctrine 2 ORM\GeneratedValue(strategy="AUTO") not working
我将 doctrine 2 与 symfony3 和 PostgresSQL 数据库一起使用,我在插入值时遇到问题,因为 id 没有递增 anymore.I 已更新数据库:
php app/console doctrine:database:update --dump-sql
php app/console doctrine:schema:update --force
但是:我得到了
Nothing to update - your database is already in sync with the current entity metadata.
这是我的 class 类别:
class Categorie {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="nom", type="string", length=255 , nullable=true)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="niveau", type="integer" , nullable=true)
*/
private $niveau;
/**
* @var int
*
* @ORM\Column(name="ordre", type="integer" , nullable=true)
*/
private $ordre;
/**
* @var string
*
* @ORM\Column(name="etat", type="string", length=255 , nullable=true )
*/
private $etat;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255 , nullable=true )
*/
private $image = null ;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="EK\PlateformeBundle\Entity\Categorie", mappedBy="categorie")
*/
private $categories ;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="EK\PlateformeBundle\Entity\Piece", mappedBy="categorie")
*/
private $pieces ;
/**
* Constructor
*/
public function __construct()
{
$this->categories = new ArrayCollection();
$this->pieces = new ArrayCollection();
}
/**
* @ORM\ManyToOne(targetEntity="EK\PlateformeBundle\Entity\Categorie", inversedBy="categorie")
*/
private $categorie;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*
* @return Categorie
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set niveau
*
* @param string $niveau
*
* @return Categorie
*/
public function setNiveau($niveau)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return string
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set ordre
*
* @param integer $ordre
*
* @return Categorie
*/
public function setOrdre($ordre)
{
$this->ordre = $ordre;
return $this;
}
/**
* Get ordre
*
* @return int
*/
public function getOrdre()
{
return $this->ordre;
}
/**
* Set etat
*
* @param string $etat
*
* @return Categorie
*/
public function setEtat($etat)
{
$this->etat = $etat;
return $this;
}
/**
* Get etat
*
* @return string
*/
public function getEtat()
{
return $this->etat;
}
/**
* Set image
*
* @param string $image
*
* @return Categorie
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAvailableCategories()
{
return $this->categories;
}
/**
* Add categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*
* @return Categorie
*/
public function addCategorie( \EK\PlateformeBundle\Entity\Categorie $categorie)
{
$this->categories[] = $categorie;
return $this;
}
/**
* Remove categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*/
public function removeCategorie(\EK\PlateformeBundle\Entity\Categorie $categorie)
{
$this->categories->removeElement($categorie);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/*
*
*/
public function __toString()
{
return $this->getNom();
}
/**
* Set categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*
* @return Categorie
*/
public function setCategorie(\EK\PlateformeBundle\Entity\Marque $categorie = null)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* @return \EK\PlateformeBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Get pieces
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAvailablePieces()
{
return $this->pieces;
}
/**
* Add piece
*
* @param \EK\PlateformeBundle\Entity\Piece $piece
*
* @return Categorie
*/
public function addPiece( \EK\PlateformeBundle\Entity\Piece $piece)
{
$this->pieces[] = $piece;
return $this;
}
/**
* Remove piece
*
* @param \EK\PlateformeBundle\Entity\Piece $piece
*/
public function removePiece(\EK\PlateformeBundle\Entity\Piece $piece)
{
$this->pieces->removeElement($piece);
}
/**
* Get pieces
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPieces()
{
return $this->pieces;
}
}
当我尝试使用插入 sql 命令将数据插入数据库时:
INSERT INTO public.categorie(nom, niveau, ordre)
VALUES ('test',1,1);
我收到这个错误:
ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « id »
感谢您的帮助
我通过在实体 class
中将 "AUTO" 更改为 "IDENTITY" 解决了我的问题
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
我将 doctrine 2 与 symfony3 和 PostgresSQL 数据库一起使用,我在插入值时遇到问题,因为 id 没有递增 anymore.I 已更新数据库:
php app/console doctrine:database:update --dump-sql
php app/console doctrine:schema:update --force
但是:我得到了
Nothing to update - your database is already in sync with the current entity metadata.
这是我的 class 类别:
class Categorie {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @ORM\Column(name="nom", type="string", length=255 , nullable=true)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="niveau", type="integer" , nullable=true)
*/
private $niveau;
/**
* @var int
*
* @ORM\Column(name="ordre", type="integer" , nullable=true)
*/
private $ordre;
/**
* @var string
*
* @ORM\Column(name="etat", type="string", length=255 , nullable=true )
*/
private $etat;
/**
* @var string
*
* @ORM\Column(name="image", type="string", length=255 , nullable=true )
*/
private $image = null ;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="EK\PlateformeBundle\Entity\Categorie", mappedBy="categorie")
*/
private $categories ;
/**
* @var array
*
* @ORM\OneToMany(targetEntity="EK\PlateformeBundle\Entity\Piece", mappedBy="categorie")
*/
private $pieces ;
/**
* Constructor
*/
public function __construct()
{
$this->categories = new ArrayCollection();
$this->pieces = new ArrayCollection();
}
/**
* @ORM\ManyToOne(targetEntity="EK\PlateformeBundle\Entity\Categorie", inversedBy="categorie")
*/
private $categorie;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*
* @return Categorie
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set niveau
*
* @param string $niveau
*
* @return Categorie
*/
public function setNiveau($niveau)
{
$this->niveau = $niveau;
return $this;
}
/**
* Get niveau
*
* @return string
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set ordre
*
* @param integer $ordre
*
* @return Categorie
*/
public function setOrdre($ordre)
{
$this->ordre = $ordre;
return $this;
}
/**
* Get ordre
*
* @return int
*/
public function getOrdre()
{
return $this->ordre;
}
/**
* Set etat
*
* @param string $etat
*
* @return Categorie
*/
public function setEtat($etat)
{
$this->etat = $etat;
return $this;
}
/**
* Get etat
*
* @return string
*/
public function getEtat()
{
return $this->etat;
}
/**
* Set image
*
* @param string $image
*
* @return Categorie
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAvailableCategories()
{
return $this->categories;
}
/**
* Add categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*
* @return Categorie
*/
public function addCategorie( \EK\PlateformeBundle\Entity\Categorie $categorie)
{
$this->categories[] = $categorie;
return $this;
}
/**
* Remove categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*/
public function removeCategorie(\EK\PlateformeBundle\Entity\Categorie $categorie)
{
$this->categories->removeElement($categorie);
}
/**
* Get categories
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/*
*
*/
public function __toString()
{
return $this->getNom();
}
/**
* Set categorie
*
* @param \EK\PlateformeBundle\Entity\Categorie $categorie
*
* @return Categorie
*/
public function setCategorie(\EK\PlateformeBundle\Entity\Marque $categorie = null)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* @return \EK\PlateformeBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Get pieces
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAvailablePieces()
{
return $this->pieces;
}
/**
* Add piece
*
* @param \EK\PlateformeBundle\Entity\Piece $piece
*
* @return Categorie
*/
public function addPiece( \EK\PlateformeBundle\Entity\Piece $piece)
{
$this->pieces[] = $piece;
return $this;
}
/**
* Remove piece
*
* @param \EK\PlateformeBundle\Entity\Piece $piece
*/
public function removePiece(\EK\PlateformeBundle\Entity\Piece $piece)
{
$this->pieces->removeElement($piece);
}
/**
* Get pieces
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPieces()
{
return $this->pieces;
}
}
当我尝试使用插入 sql 命令将数据插入数据库时:
INSERT INTO public.categorie(nom, niveau, ordre)
VALUES ('test',1,1);
我收到这个错误:
ERREUR: une valeur NULL viole la contrainte NOT NULL de la colonne « id »
感谢您的帮助
我通过在实体 class
中将 "AUTO" 更改为 "IDENTITY" 解决了我的问题* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;