2015-10-26 24 views
1

我正在填充数据库表。表中有一些是enum字段。使用多个值填充数据库字段

考虑其中有一个字段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其字段field1activeinactivedeletedrolesadminusereditor等(只是对于所提供的有效,无效等例如,它可以只是数值)

这个想法是根据计数产生更多的用户,如果提供的话。

例如:

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

UPDATE:

根据@了四届鸟答案https://stackoverflow.com/a/33354032/487878

问题是,它只能看2场中所做的更改,该字段可以是1或n。

回答

1

您是否在寻找这样的设置? (不知道用户的字段可以是什么,我用在这个例子中“角色”和“管理员”。)

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

你举的例子是不错,问题是这些字段是基于动态配置的(它可以是n之一)。所以我们不能限制它。 –

+0

我已经使用另一个示例更新了代码以阐明预期结果。这个例子是你正在寻找的动态配置吗? –

+0

谢谢,正如我所提到的,你不能指望角色/状态是领域。我只是举了一个粗略的例子。考虑根据不同的表格来改变字段的名称。 –