Search code examples
phpdatabaseloopsormredbean

Using a FOR loop with RedBeanPHP


// Includes rb.php here etc.
$mail_type = array('gmail.com', 'yahoo.com', 'yandex.ru', 'hotmail.com', 'live.no');

for ($i = 0; $i < 100; $i++)
{
    $mail = R::dispense('emails');
    $mail->id = $i;

    // Create random email
    $mail->email = strtolower(substr(base64_encode(mt_rand(99, 99999)), 0, -2)).'@'.$mail_type[mt_rand(0, 4)];

    R::store($mail);
    unset($mail); // This doesn't work either
}

Trying to insert 100 different randomly made e-mails with RedBeanPHP. For some reason I can't get this to work since only the last object gets inserted into the database, effectively replacing the last. Can't find any documentation on it either. Any ideas?


Solution

  • RedBeanPHP thinks it's already stored since you set the id. Solution is to remove the $mail->id = $id field.