2

我是教义新手。我有一个带有注释的以下用户类。主键是 $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 仍会插入默认值,即空字符串,从而导致违反完整性约束。有人可以为此提供详细的解决方案吗?

4

1 回答 1

3

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

于 2012-11-07T19:54:30.960 回答