dsaob80228 2012-11-07 19:51
浏览 24
已采纳

手动设置ID,学说

I am new to doctrine. I have a following User class with annotations. The primary key is $userid, which is string. Instead of setting the $userid automatically, I want to set the id manually. How ever I am not sure how to do that?

<?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;
}
}

If I even make the id public and set it myself, the doctrine 2 still inserts the default value, which is empty string, leading to Integrity constraint violation. Can someone provide me detailed solution for this.

  • 写回答

1条回答 默认 最新

  • duangua5742 2012-11-07 19:54
    关注

    Remove the @GeneratedValue line from the @Id definition. You're asking Doctrine to generate the ID for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测