2016-07-05 19 views
0

我已經檢查,它有價值,但當沖洗(),它是與級別爲null。我在database.Message中創建了fk:在執行'INSERT INTO用戶(用戶名,密碼,實名,級別)VALUES(?,?,?,?)'時發生異常,並帶有參數{「1」:「abcdef」,「 2 「:」 21232f297a57a5a743894a0e4a801fc3" , 「3」: 「ABCDEF」, 「4」:空}:SQLSTATE [23000]:完整性約束違規:1048列 '等級' 不能爲空 我的代碼在控制器原則有價值堅持,但在沖洗時爲空

public function testinsertAction() 
{ 
    $user = new \Entities\Usernew(); 
    $user->setUsername('abcdef'); 
    $user->setPassword(MD5('admin')); 
    $user->setReal_name('abcdef'); 
    $user->setLevel(1); 
    $this->_em->persist($user); 
    \Zend_Debug::dump($user);die(); 
    $this->_em->flush(); 
} 

實體

namespace Entities; 
/** 
* @Entity 
* @Table(name="user") 
*/ 
class Usernew{ 
/** 
* @id 
* @Column(type="integer",length=11) 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 


/** 
* @Column(type="string",length=50) 
*/ 
protected $username; 
/** 
* @Column(type="string",length=32) 
*/ 
protected $password; 
/** 
* @Column(type="string",length=150,nullable=true) 
*/ 
protected $real_name; 
/** 
* @Column(type="integer",length=1) 
* 
*/ 
protected $level; 
/** 
* @ManyToOne(targetEntity="Level", inversedBy="users") 
* @JoinColumn(name="level",referencedColumnName="id") 
*/ 
protected $group; 
/** 
* @return the $group 
*/ 
public function getGroup() 
{ 
    return $this->group; 
} 

/** 
* @param field_type $group 
*/ 
public function setGroup($group) 
{ 
    $this->group = $group; 
} 
/** 
* @return the $password 
*/ 
public function getPassword() 
{ 
    return $this->password; 
} 
/** 
* @param field_type $password 
*/ 
public function setPassword($password) 
{ 
    $this->password = $password; 
} 
/** 
* @return the $id 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* @return the $username 
*/ 
public function getUsername() 
{ 
    return $this->username; 
} 

/** 
* @return the $real_name 
*/ 
public function getReal_name() 
{ 
    return $this->real_name; 
} 

/** 
* @return the $level 
*/ 
public function getLevel() 
{ 
    return $this->level; 
} 

/** 
* @param field_type $id 
*/ 
public function setId($id) 
{ 
    $this->id = $id; 
} 

/** 
* @param field_type $username 
*/ 
public function setUsername($username) 
{ 
    $this->username = $username; 
} 

/** 
* @param field_type $real_name 
*/ 
public function setReal_name($real_name) 
{ 
    $this->real_name = $real_name; 
} 

/** 
* @param field_type $level 
*/ 
public function setLevel($level) 
{ 
    $this->level = $level; 
} 

我的實體\級別

namespace Entities; 
use \Doctrine\Common\Collections\ArrayCollection; 
/** 
* @Entity 
* @Table(name="level") 
*/ 
class Level{ 
/** 
* @id 
* @Column(type="integer",length=11) 
* @GeneratedValue(strategy="AUTO") 
* 
*/ 
protected $id; 
/** 
* @Column(type="string",length=150,nullable=true) 
*/ 
protected $name; 
/** 
* @OnetoMany(targetEntity="Usernew", mappedBy="group") 
*/ 
protected $users; 
/** 
* @return the $id 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* @return the $name 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* @return the $users 
*/ 
public function getUsers() 
{ 
    return $this->users; 
} 

/** 
* @param field_type $id 
*/ 
public function setId($id) 
{ 
    $this->id = $id; 
} 

/** 
* @param field_type $name 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 
} 

/** 
* @param \Doctrine\Common\Collections\ArrayCollection $users 
*/ 
public function setUsers($users) 
{ 
    $this->users = $users; 
} 
public function __construct(){ 
    $this->users= new ArrayCollection(); 
} 

}

回答

0

外鍵level不能爲空,或者檢查phpMyAdmin領域在表結構允許空值或在您的情況,因爲它是FK所以你要插入一些值,外交部鍵不能爲空