手动设置ID,学说

时间:2012-11-07 19:51:50

标签: php doctrine doctrine-orm

我是学说的新手。我有一个带注释的以下User类。主键是$ userid,它是字符串。我没有自动设置$ userid,而是想手动设置id。我怎么不知道怎么做?

<?php

namespace models;

/**
 * User
 *
 * @Table(name="user")
 * @Entity
 */
class User
{
/**
 * @var string $userid
 *
 * @Column(name="userid", type="string", length=255, precision=0, scale=0, nullable=false, unique=false)
 * @Id
 * @GeneratedValue(strategy="IDENTITY")
 */
private $userid;

/**
 * @var string $fullname
 *
 * @Column(name="fullname", type="string", length=255, precision=0, scale=0, nullable=false, unique=false)
 */
private $fullname;

/**
 * @var string $password
 *
 * @Column(name="password", type="string", length=255, precision=0, scale=0, nullable=false, unique=false)
 */
private $password;

/**
 * @var string $email
 *
 * @Column(name="email", type="string", length=255, precision=0, scale=0, nullable=false, unique=false)
 */
private $email;

/**
 * Get userid
 *
 * @return string $userid
 */
public function getUserid()
{
    return $this->userid;
}

/**
 * Set fullname
 *
 * @param string $fullname
 */
public function setFullname($fullname)
{
    $this->fullname = $fullname;
}

/**
 * Get fullname
 *
 * @return string $fullname
 */
public function getFullname()
{
    return $this->fullname;
}

/**
 * Set password
 *
 * @param string $password
 */
public function setPassword($password)
{
    $this->password = $password;
}

/**
 * Get password
 *
 * @return string $password
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set email
 *
 * @param string $email
 */
public function setEmail($email)
{
    $this->email = $email;
}

/**
 * Get email
 *
 * @return string $email
 */
public function getEmail()
{
    return $this->email;
}
}

如果我甚至将id公开并自己设置,则学说2仍会插入默认值,即空字符串,从而导致完整性约束违规。有人可以为我提供详细的解决方案。

1 个答案:

答案 0 :(得分:3)

从@Id定义中删除@GeneratedValue行。您要求Doctrine为您生成ID。