简体   繁体   English

mt_rand同一号码,symfony2

[英]mt_rand same number, symfony2

i'm currently working with symfony2. 我目前正在使用symfony2。 I've created my user entity as i want it to be, but i have a massive problem and don't know what it's caused by: 我已经创建了我想要的用户实体,但是我遇到了一个很大的问题,并且不知道它是由什么引起的:

i want to have a random number with the length of 10 characters for the ID. 我想要一个随机数,其ID的长度为10个字符。

ID field: 编号栏位:

/**
     * @ORM\Id
     * @ORM\Column(type="integer", unique=true)
     * 
     */
    protected $id;

setId, so a random number get's generated, before persisting setId,因此在持久化之前会生成一个随机数

/**
     * Set id
     *
     * @param integer $id
     * @return UserReg
     * @ORM\PrePersist
     */
    public function setId($id)
    {
        $number = mt_rand(1000000000, 9999999999);
        $this->id = $number;

        return $this;
    }

If i run the same function on a completely fresh controller that does nothing but generate the number and give it out .... it works. 如果我在完全新鲜的控制器上运行相同的功能,则不执行任何操作,但生成数字并将其发出..它起作用。 Everytime i refresh the page a random number appears. 每次我刷新页面时,都会出现一个随机数。

But somehow it doesn't work with the user registration. 但是以某种方式它不适用于用户注册。 The same number gets generated over and over again. 一遍又一遍地产生相同的数字。 Before i used the Lifecycle Callbacks and tried to generate the number within the register controller, same result ... i'm now trying to generate the number directly withing the entity as you can see and still.... the same number, over and over again. 在我使用Lifecycle Callbacks并尝试在注册控制器中生成数字之前,相同的结果...我现在正尝试直接与实体一起生成数字,如您所见,仍然....一遍又一遍。

What am i missing? 我想念什么?

I hope somebody can help ... 我希望有人可以帮助...

Regards 问候

Edit: 编辑:

SOMETIMES a different random number gets generated, but most of the times it's the same. 有时会生成不同的随机数,但是大多数情况下它是相同的。 Fe: 1. register: 2147483647 2. register: 1749450073 3. register: duplicate entry ( 2147483647 ). Fe:1.寄存器:2147483647 2.寄存器:1749450073 3.寄存器:重复输入(2147483647)。

I just took a good look at the error message: 我只是仔细看了一下错误消息:

An exception occurred while executing 'INSERT INTO UserReg (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, id, firstname, lastname, birthdate, secretq, secreta, registered, banned, frozen, creditpoints, country, verified, referralcode, referree, messages) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["testmail3@test.com", "testmail3@test.com", "testmail3@test.com", "testmail3@test.com", 0, "gqd6kqolwk084gwg4ggwwsc8kokwk0k", "$2y$13$gqd6kqolwk084gwg4ggwwetuZOQlZzCjlPt7HmZ4RMp7LBITBZ8Wy", null, 0, 0, null, "i_cVQFp-cG5KyQ70eqMZH6y32lAZEtEsvWkIG7bbcrQ", null, "a:0:{}", 0, null, **2179120243**, "Max", "Mustermann", "1894-01-01 00:00:00", "What was your childhood nickname", "asd", "2014-02-01 18:30:03", "no", null, "150", "Austria", "no", "c46c6fcaaaae7a606fd25185ec860fd1", null, "0"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '**2147483647**' for key 'PRIMARY'

I don't get what's wrong: the sql insert shows a different ID than the one in the error message. 我没有发现什么问题:sql插入显示的ID与错误消息中的ID不同。

The problem is that you're reaching the max size for an integer, 2^31-1 = 2147483647. Generate the numbers as a string and you'll be fine. 问题是您达到了整数的最大大小2 ^ 31-1 =2147483647。将数字生成为字符串,就可以了。

That said, you should just use MySQL's sequential auto_incremenet for IDs instead. 也就是说,您应该只使用MySQL的顺序auto_incremenet作为ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM