1

I am working on populating the database tables. The table have fields which some of them are enum.

Consider a user which have a field status , whose values can be active, inactive etc. Assume we can modify the configuration values and running the script the data can be populated accordingly.

Let us represent the user table whose status field as

'status' => array(
   'active' => 3, 
   'inactive', 
   'deleted',
),

In this case assume we need to create 3 users with status , active. 1 user with status inactive and 1 with deleted.

The table may be having more enum fields. So the config can expand. Depending on the configuration and fields the values will be multiples.

Consider the below example.

Eg :

$config = array(
    'table1name' => array(
        'field1' => array(
            'active' => 3, 
            'inactive', 
            'deleted',
        ),
        'field2' => array(
            'admin', 
            'user', 
            'editor'
        ),
        ....,
        'more-fields' => array(
            'more-values', 
        )
    ),
    'table2name' => array(
        'field1' => array(
            'active', 
            'inactive', 
            'deleted',
        ),
    )
);

In this case there need to populate table1 whose field field1 with active, inactive, deleted and roles with admin, user, editor etc. ( The active, inactive etc are provided just for example. It can be just values. )

The idea is to generate more users depending on the count if any provided.

Eg :

'status' => array(
    'active' => 10, 
    'inactive' => 2, 
    'deleted' => 3,
),
'roles' => array(
    'admin' => 2, 
    'user', 
    'editor'
)
....,
'more-fields' => array(
    'more-values', 
)

So that there will be

10 * 4 => active users (10 * 2 active admin / 10 active user, 10 active editor ) + 2 * 4 => inactive users ( 2 inactive admin , 1 user, 1 editor ) + 3 * 4 => deleted users in total.

I am struggling to build the algorithm for the same.

array(
    'status' => array(
        'active' => 10, 
        'inactive' => 2, 
        'deleted' => 3,
    ),
    'roles' => array(
        'admin' => 2, 
        'user', 
        'editor'
    ),
    ....,
    'more-fields' => array(
        'more-values', 
    )
)

// In this example you can see we have not covered the fields of the table when they are more than 1 on save.It looks we need to build the array with values first.

foreach ($config as $table => $fields) {
    foreach ($fields as $field => $values ) {
        foreach ($values as $key => $statusCount) {
            if (is_string($key)) {
                $model = new User();
                $model->$field = $key;
                $model->another = 'value';
                $model->save();
            } else {            
                for ($i = 0; $i< $statusCount; $i++) {
                    $model = new User();
                    $model->$field = $key;
                    $model->another = 'value';
                    $model->save();
                }
            }
        }
    }
}

UPDATE :

Changes made according to @the-fourth-bird answer https://stackoverflow.com/a/33354032/487878

Problem is it only look for 2 fields, the fields can be 1 or n.

Community
  • 1
  • 1
Hari K T
  • 4,174
  • 3
  • 32
  • 51

1 Answers1

1

Are you looking for a setup like this? (Not sure what the fields for the User can be, I used 'role' and 'admin' in this example.)

$fields = array(
    'status' => array(
        'active' => 10,
        'inactive' => 2,
        'deleted' => 3,
    ),
    'roles' => array(
        'admin',
        'user',
        'editor'
    )
);
$roles = $fields['roles'];
$statuses = $fields['status'];

foreach ($roles as $role) {
    foreach ($statuses as $status => $statusCount) {
        for ($i = 0; $i< $statusCount; $i++) {
            $model = new User();
            $model->role = $role;
            $model->status = $status;
        }
    }
}

// Update with dynamic properties

<?php
class table1name {
    public function save() {}
}
class table2name {
    public function save() {}
}
$config = array(
    'table1name' => array(
        'field1' => array(
            'active' => 3,
            'inactive',
            'deleted',
        ),
        'field2' => array(
            'admin',
            'user' => 2,
            'editor'
        ),
        'more-fields' => array(
            'more-values' => 2,
        ),
        'color' => array(
            'blue' => 2,
            'red'
        ),

    ),
    'table2name' => array(
        'field1' => array(
            'active',
            'inactive',
            'deleted',
        ),
    )
);

// Adjust data structure
// If the key is a string, turn the key into values for the given multiplier in the same array.
// Then unset the key.
foreach ($config as $table => $fields) {
    foreach ($fields as $field => $values ) {
        foreach ($values as $key => $statusCount) {
            if (is_string($key)) {
                for ($i = 0; $i< $statusCount; $i++) {
                    $config[$table][$field][] = $key;
                }
                unset($config[$table][$field][(string)$key]);
            }
        }
    }
}

$cartesians = [];

// If you want all the possible combinations for for example the 'table1name', you need a cartesian product. Used the function from this page:
//http://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays
function cartesian($input) {
    $input = array_filter($input);
    $result = array(array());

    foreach ($input as $key => $values) {
        $append = array();
        foreach($result as $product) {
            foreach($values as $item) {
                $product[$key] = $item;
                $append[] = $product;
            }
        }
        $result = $append;
    }
    return $result;
}

// Create the cartesian products for all the keys in the $config array.
foreach ($config as $key => $tables) {
    $cartesians[$key] = cartesian($tables);
}

// Loop all the objects created by the cartesian function.
foreach ($cartesians as $objectName => $cartesian) {
    foreach($cartesian as $key => $value) {
        $model = new $objectName();
        $model->$key = $value;
        $model->save();
    }
}
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Your example is nice, the problem is the fields are dynamic configuration based (it can be one to n). So we could not restrict it . – Hari K T Oct 27 '15 at 03:29
  • I've updated the code with another example to clarify the expected results. Is this example the dynamic configuration you are looking for? – The fourth bird Oct 27 '15 at 13:26
  • Thank you, as I mentioned you cannot expect the roles / status are the fields. I was just giving a rough example. Consider the name of the fields change according to different table. – Hari K T Oct 28 '15 at 14:23
  • have modified the example to make it more clearer. Does this helps? Thank you. – Hari K T Oct 28 '15 at 14:44
  • I've updated my code example. The properties are now dynamic according to the table in the config array. Perhaps this idea can serve as a basis for your algorithm. – The fourth bird Oct 29 '15 at 23:22