我正在填充数据库表。该表有一些字段,其中一些是枚举。
考虑具有字段status
的用户,其值可以是active
,inactive
等。假设我们可以修改配置值并运行脚本,可以相应地填充数据
让我们代表user
字段为
status
表
'status' => array(
'active' => 3,
'inactive',
'deleted',
),
在这种情况下,我们假设我们需要使用status
,active
创建3个用户。 1位状态为inactive
且1位为deleted
的用户。
该表可能包含更多枚举字段。所以配置可以扩展。根据配置和字段,值将是倍数。
考虑下面的例子。
例如:
$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',
),
)
);
在这种情况下,需要将table1
的字段field1
与active
,inactive
,deleted
和roles
填充为{{1} }},admin
,user
等。(例如,提供有效,无效等等。它可以只是值。)
这个想法是根据计数生成更多用户(如果有的话)。
例如:
editor
这样就会有
10 * 4 =>活跃用户(10 * 2活动管理员/ 10活跃用户,10活动编辑器)+ 2 * 4 =>非活动用户(2个非活动管理员,1个用户,1个编辑者)+ 3 * 4 =>总共删除了用户。
我正在努力为此构建算法。
'status' => array(
'active' => 10,
'inactive' => 2,
'deleted' => 3,
),
'roles' => array(
'admin' => 2,
'user',
'editor'
)
....,
'more-fields' => array(
'more-values',
)
更新:
根据@ the-fourth-bird answer https://stackoverflow.com/a/33354032/487878
进行的更改问题是它只查找2个字段,字段可以是1或n。
答案 0 :(得分:1)
你在寻找这样的设置吗? (不确定用户的字段是什么,我在本例中使用'role'和'admin'。)
$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;
}
}
}
//使用动态属性进行更新
<?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();
}
}