我在Symfony 3.0.6下 我在这个实体中使用了loadFixtures:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Categorie
*
* @ORM\Table(name="categorie")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategorieRepository")
*/
class Categorie
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Libelle", type="string", length=100)
*/
private $libelle;
/**
* @var datetime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* @ORM\ManyToOne(targetEntity="\AppBundle\Entity\User", inversedBy="categories")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $user;
public function __construct()
{
$this->createdAt = new \DateTime('now');
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* @param string $libelle
*
* @return Categorie
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
*
* @return Categorie
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set createdAt
*
* @param datetime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
/**
* Get createdAt
*
* @return datetime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
}
当我尝试用这个插入一些值时:
public function loadCategorie(ObjectManager $manager, $user)
{
$aLib = array('Categ1','Categ2','Categ3','Categ4','Categ5','Categ6','Categ7');
foreach($aLib as $catP)
{
$cat = new Categorie();
$cat->setLibelle("[".$user->getUsername()."] ".$catP);
$cat->setUser($user);
$manager->persist($cat);
}
$manager->flush();
}
我有这个错误:
[学说\ DBAL \异常\ NotNullConstraintViolationException] 执行'INSERT INTO categories(Libelle,created_at,user_id)VALUES(?,?,?)'时发生异常 params [“[cyriladmin] Categ1”,null,1]: SQLSTATE [23000]:完整性约束违规:1048列'created_at'不能为空
任何想法?
答案 0 :(得分:1)
您在实体中遇到错误:private $created_at;
vs $this->createdAt = new \DateTime('now');
&lt; - 不同的变量名称。