doctrine值持续存在但是在flush时为null

时间:2016-07-05 06:38:01

标签: zend-framework doctrine-orm

我已经检查过并且它有值但是当flush()时,它的等级为null。我在database.Message中创建了fk:执行' INSERT INTO用户(用户名,密码,real_name,级别)VALUES(?,?,?,?)'与params {" 1":" abcdef"," 2":" 21232f297a57a5a743894a0e4a801fc3"," 3":&# 34; abcdef"," 4":null}: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();
}

}

1 个答案:

答案 0 :(得分:0)

外键level不能为空,请检查表格结构中的phpMyAdmin字段以允许空值,或者在您的情况下检查 fk 所以你必须插入一些值,外键不能以任何方式为空