Doctrine 2 ORM \ GeneratedValue(策略=" AUTO")不起作用

时间:2017-07-31 09:22:59

标签: postgresql doctrine-orm symfony-3.3

我正在使用带有symfony3和PostgresSQL数据库的doctrine 2,我在插入值时遇到问题,因为id不再增加。我已用以下内容更新数据库:

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 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;
}  

}

我尝试使用insert 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 »

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我通过改变" AUTO"解决了我的问题。通过" IDENTITY"在实体类

* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;