示例#1
0
 /**
  * @param integer $count
  * @return \Generator
  * @throws Exception
  */
 public function export($count)
 {
     if (!$this->model) {
         throw new Exception("Model name is required");
     }
     foreach ($this->generator->generate() as $item) {
         /** @var \yii\db\ActiveRecord $model */
         $model = new $this->model();
         foreach ($item as $key => $value) {
             // we don't wanna be safe :)
             $model->{$key} = $value;
         }
         try {
             if (!$model->save()) {
                 throw new Exception("Model save error. Validation errors: " . var_export($model->getErrors(), true));
             }
         } catch (\Exception $e) {
             throw new Exception("Model save error. Exception: {$e->getMessage()}");
         }
         if (--$count <= 0) {
             break;
         }
         (yield $count);
     }
 }
 /**
  * @param integer $count
  * @return \Generator
  * @throws Exception
  */
 public function export($count)
 {
     throw new Exception("GeneratorTemplate: you can use this class as template for new dbprovider classes.");
     foreach ($this->generator->generate() as $item) {
         // do
         if (--$count <= 0) {
             break;
         }
         (yield $count);
     }
 }
示例#3
0
 /**
  * @param integer $count
  * @return \Generator
  */
 public function export($count)
 {
     $first = $this->generator->generate()->current();
     $this->out(array_keys($first));
     $this->out(array_values($first));
     foreach ($this->generator->generate() as $data) {
         if (--$count <= 0) {
             break;
         }
         $this->out($data);
         (yield $count);
     }
 }
示例#4
0
 /**
  * @param integer $count
  * @return \Generator
  * @throws Exception
  */
 public function export($count)
 {
     if (!$this->table) {
         throw new Exception("table name is required");
     }
     if (is_null($this->db)) {
         $this->db = \Yii::$app->get($this->db_component);
     }
     $this->table_quoted = $this->db->quoteTableName($this->table);
     $this->truncate();
     $first_row = $this->generator->generate()->current();
     // prepare quoted fileds name list
     $fields = array_map(function ($i) {
         return $this->db->quoteColumnName($i);
     }, array_keys($first_row));
     $fields_str = implode(",", $fields);
     // prepare values section, repeat values block $rows_per_request number
     $rows_per_request = 1;
     if ($this->multirow) {
         $rows_per_request = (int) max(floor($this->placeholder_limit / count($fields)), 1);
     }
     $placeholders = "(" . implode(",", array_fill(1, count($fields), '?')) . ")";
     $value_placeholders = implode(",", array_fill(1, $rows_per_request, $placeholders));
     // finally sql request
     $prepare = $this->db->createCommand("INSERT INTO {$this->table_quoted}({$fields_str}) VALUES {$value_placeholders}");
     // first row of data, lets save it
     $insert_values = $this->array1based($first_row);
     $prepared_rows = 1;
     foreach ($this->generator->generate() as $item) {
         if ($prepared_rows === $rows_per_request) {
             unset($insert_values[0]);
             $prepare->bindValues($insert_values);
             $prepare->execute();
             $count = $count - $rows_per_request;
             if ($count <= 0) {
                 break;
             }
             (yield $count);
             $insert_values = $this->array1based($item);
             $prepared_rows = 1;
         } else {
             $insert_values = array_merge($insert_values, array_values($item));
             $prepared_rows++;
         }
     }
 }
示例#5
0
 private function confirmGeneration()
 {
     $this->stdout("Example of fake data (1 row):\n", Console::FG_YELLOW);
     $fake_data = $this->generator_obj->generate()->current();
     foreach ($fake_data as $field => $data) {
         $this->stdout("\t{$field}: {$data}\n");
     }
     return $this->confirm("Generate {$this->count} rows?", Console::FG_YELLOW);
 }