gpt4 book ai didi

php - 使用多个值填充数据库字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:58 24 4
gpt4 key购买 nike

我正在填充数据库表。该表有一些字段,其中一些是枚举。

考虑一个用户有一个字段status,它的值可以是activeinactive等。假设我们可以修改配置值和运行脚本可以相应地填充数据。

让我们将 user 表的 status 字段表示为

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

在这种情况下,假设我们需要创建 3 个具有 statusactive 的用户。 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,其字段 field1activeinactive删除角色adminusereditor等(active,inactive等是仅作为示例提供。它可以只是值。)

我们的想法是根据提供的数量生成更多用户。

例如:

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

这样就会有

10 * 4 => 活跃用户(10 * 2 活跃管理员/10 活跃用户,10 活跃编辑)+2 * 4 => 非活跃用户(2 位非活跃管理员,1 位用户,1 位编辑)+3 * 4 => 总共删除了用户。

我正在努力构建相同的算法。

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();
}
}
}
}
}

更新:

根据@the-fourth-bird 的回答所做的更改 https://stackoverflow.com/a/33354032/487878

问题是它只查找 2 个字段,字段可以是 1 或 n。

最佳答案

您正在寻找这样的设置吗? (不确定用户的字段可以是什么,我在此示例中使用了“角色”和“管理员”。)

$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();
}
}

关于php - 使用多个值填充数据库字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33350217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com