0

I have entity with uniq field, inviteCode. And when I create new entity I want set automatic some random code, but this code must be different from exist in db, what do you thing, what practices about that you know ?

/**
 * @ORM\Table(name="users")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @AssertBridge\UniqueEntity(
 *     groups={"registration"},
 *     fields="inviteCode",
  *     errorPath="not valid",
 *     message="This inviteCode is already in use."
 * )
 */
 class User extends AbstractUser implements UserInterface
 {
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=500, unique=true)
 * @Annotation\SerializedName("_invite_code")
 */
private $inviteCode;

I found something like that

    private function calculateReference($number)
{
    $multipliers = array(7,3,1);
    $length = strlen($number);
    $numberArr = str_split($number);

    $sum = 0;

    for ($i = $length - 1; $i >= 0; --$i)
    {
        $sum += $numberArr[$i] * $multipliers[($length - 1 - $i) % 3];
    }

    return $number.(10 - $sum % 10) % 10;
} 

first get max id from table then call function calculateReference with id and then setInviteCode.

But I believe doctrine have something exist for this issue or maybe somebody have good example for this

1
  • 1
    In which way will you use your inviteCode ? Please give us some context of the needed feature to drive you to the best solution :) Commented Nov 8, 2017 at 7:59

2 Answers 2

3

Someone provided a great answer here https://stackoverflow.com/a/13917309/4173130.

But like he said at the end, you don't need doctrine for such a simple feature. Generating the code in the constructor is an efficient, simple and clear solution.

You can use a UUID library like ramsey/uuid. Then you would be able to generate any random code with Uuid::uuid4();.

Another solution is to use random_bytes() with base64_encode : base64_encode(random_bytes(32)).

Please don't try to create a new function to generate random values. Most of time it is not secure, see https://www.owasp.org/index.php/Insecure_Randomness.

Sign up to request clarification or add additional context in comments.

2 Comments

What about uniq data from ramsey/uuid ? After generate data by uuid need to check inviteCode in data base ? And how get and set data, like this $uuid = Uuid::uuid4(); $user->setInviteCode($uuid->jsonSerialize()); ?
Like I said, just generate the code in the constructor. Then you can use a Doctrine Type or a string to store the data.
1

Why not using a uuid? It is included in php as a core function and i believe it suits your needs.

Check in the official documentation here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.