php - 手动设置ID、 Doctrine

标签 php doctrine doctrine-orm

我对 Doctrine 很陌生。我有一个带有注释的以下 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 公开并自己设置,则 Doctrine 2 仍然会插入默认值(空字符串),从而导致违反完整性约束。有人可以为我提供详细的解决方案吗?

最佳答案

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

关于php - 手动设置ID、 Doctrine ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13277117/

相关文章:

php - 如何找出5天前的日期?

php - 如何编写具有多个条件的 Doctrine 查询?

validation - 验证实体中的值 - 通过 setter 或断言验证?

php - Symfony 从实体标题创建 slug

php - 我们可以在 symfony2 的多个包中使用通用( Doctrine )orm 文件吗?

php - Symfony 2 从实体管理器获取实体的原始数据

php - Mysql 查询从没有记录的连接计数为零

php - 德鲁帕尔 8 : Notify message is not translated

php - 优化mysql查询-foreach循环

doctrine-orm - Doctrine 2 控制台工具显示 : You are missing a "cli-config.php" or "config/cli-config.php"