SQLSTATE [23000]:完整性约束违规:1048列“用户名”不能为空
我检查并确认所有3个输入参数都不为空!
function insert_user($username,$email,$password)
{
$user = new User();
$user->setUsername = $username;
$user->setEmail = $email;
$user->setPassword = $password;
try {
//save to database
$this->em->persist($user);
$this->em->flush();
}
catch(Exception $err){
die($err->getMessage());
}
return true;
}
数据库架构
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(300) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
实体类
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @Table(name="user")
* @Entity
*/
class User
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string $username
*
* @Column(name="username", type="string", length=300, nullable=false)
*/
private $username;
/**
* @var string $email
*
* @Column(name="email", type="string", length=300, nullable=false)
*/
private $email;
/**
* @var string $password
*
* @Column(name="password", type="string", length=300, nullable=false)
*/
private $password;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set email
*
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
}
答案 0 :(得分:1)
你真的拥有名称如setUsername
的属性(公共)吗?或者你的意思是调用方法setUsername($username)
?
$user->setUsername($username);
$user->setEmail($email);
$user->setPassword($password);
请记住,获得Doctrine 2帮助的最佳方法是发布实体类定义