当我想要通过终端创建新用户时,我得到了这个错误:
Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation:
1048 Column 'created_at' cannot be null
我使用的是MySQL数据库和Doctrin2.5。下面是表格的设置:
Column Type Comment
id int(11) Auto Increment
name varchar(255)
created_at datetime
last_login datetime NULL
下面是create_user.php:
<?php
// create_user.php
use Doctrine\ORM\Mapping as ORM;
require_once "bootstrap.php";
require 'vendor/autoload.php';
use Db\User;
$newUsername = $argv[1];
$user = new User();
$user->setName($newUsername);
$entityManager->persist($user);
$entityManager->flush();
echo "Created User with ID " . $user->getId() . "\n";
User.php:
<?php
// src/User.php
namespace Db;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity @ORM\Table(name="user")
**/
class User
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue **/
protected $id;
/** @ORM\Column(type="string") **/
protected $name;
/** @ORM\Column(type="datetime") **/
protected $created_at;
/** @ORM\Column(type="datetime", nullable=true) **/
protected $last_login;
我看不到错误,因为列created_at不是null。
发布于 2018-02-20 04:28:07
您没有将任何值设置为created_at
,因此它会抛出一个错误。将created_at
设置为nullable或在代码中显式设置该值:
$user->setCreatedAt((new \DateTime()));
发布于 2018-02-20 04:27:01
您应该用NOT NULL
来完成created_at
列的定义。在create table语法中如下所示:
`created_at` datetime NOT NULL,
然后,created_at
列的每个缺省值都将设置为0000-00-00 00:00:00
。
https://stackoverflow.com/questions/48873618
复制相似问题