6

I have a table that consists of a few fields with different data types. My task is to create a PHP script that fills this table with a random data. My script will consist of looped MySQL queries. I know how to create a query for generating random date, integer and string.

I have a column that uses ENUM data type that has 4 values ('accepted', 'with errors', 'done', 'failed').

I need to know how to generate a query that allows me to fill in the ENUM field with a random value.

If anyone helps, I would greatly appreciate.

p.s. If my question is too dumb or not clear enough, I apologize for this.

Thanks

3 Answers 3

5

Shorter way using Enum:


enum Status: string
{
    case ACCEPTED = "accepted";
    case WITH_ERRORS = "with errors";
    case DONE = "done";
    case FAILED ='failed';

    public static function randomValue(): string
    {
        $arr = array_column(self::cases(), 'value');

        return $arr[array_rand($arr)];
    }

    public static function randomName(): string
    {
        $arr = array_column(self::cases(), 'name');

        return $arr[array_rand($arr)];
    }
}

Take in mind that only PHP 8.1 or later versions support Enum

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

1 Comment

I went along @nuriddin lines, but implemented the method on a base "enum trait", along other utilities. I also preferred going with the enum instance instead of its attributes $cases[array_rand($cases)].
3

Put all enum values as constants in a class like this:

<?php
class Status{
    const ACCEPTED= "accepted";
    const WITH_ERRORS= "with errore";
    const DONE= "done";
    const FAILED='failed';
}


$ref = new ReflectionClass('Status');
$statuses = $ref->getConstants();
print_r(statuses );

This should output:

Array
(
    ['ACCEPTED'] => accepted
    ['WITH_ERRORS'] => with errors
    ['DONE'] => done
    ['FAILED']=> failed
)

Then to choose a random element from the $statuses array use array_rand()

echo $statuses[array_rand($statuses)];

Of course you could have just added the statuses to an array directly and used array_rand() on it instead of defining a whole class with constant, but IMHO this isn't a good approach on the long term.

Comments

2

Try:

<?php
    namespace App\Enums;
    
    /**
     * PHP 8.1 Enum DataType
     *
     * @author Toni Zeidler
     */
    enum DataType: string {
    
        case ACCEPTED = "accepted";
        case WITH_ERRORS = "with errore";
        case DONE = "done";
        case FAILED ='failed';
    
        public static function getRandomDataTypeName(): string {
          $arr =  array();
          $arrDT = DataType::cases();
    
          for($i = 0; $i < DataType::count(); $i++)
            $arr[$i] = $arrDT[$i]->name;
    
          $i = array_rand($arr, 1);
    
          return $arrDT[$i]->name;
        }
    
        public static function getRandomDataTypeValue(): string {
          $arr =  array();
          $arrDT = DataType::cases();
    
          for($i = 0; $i < DataType::count(); $i++)
            $arr[$i] = $arrDT[$i]->value;
    
          $i = array_rand($arr, 1);
    
          return $arrDT[$i]->value;
        }
        
        public static function count(): int {
          return count(DataType::cases());
        }

        public function toString(): string {
          return match($this){
            self::ACCEPTED => 'Accepted',
            self::WITH_ERRORS => 'With Errors',
            self::DONE  => 'Done',
            self::FAILED  => 'Failed',
            default => '',
          };
        }
    }  


    print(DataType::getRandomDataTypeName());
    
    print(DataType::getRandomDataTypeValue());
    
    ?>

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.