Detailed explanation of Yii framework's simple extension class for batch inserting data

*文
Release: 2023-03-19 07:30:01
Original
1440 people have browsed it

This article mainly introduces the simple implementation method of batch inserting data extension classes in Yii framework, involving Yii extension classes and database related operation skills. Friends in need can refer to it. I hope to be helpful.

The MySQL INSERT statement allows the insertion of multiple rows of data, as shown below:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
Copy after login

Then to implement batch insertion, the main task is to assemble the data into the above format in column order, you can Use sprintf and vsprintf functions to achieve this.

The following is a simple example of a Yii extension class that implements batch insertion (supports VARCHAR type data):

<?php
/**
 * class for sql batch insert
 */
class CDbBICommand extends CDbCommand{
  /** @var CActiveRecord $class */
  private $class;
  /** @var string $insert_tpl */
  private $insert_tpl = "insert into %s(%s) ";
  /** @var string $value_tpl */
  private $value_tpl = "(%s)";
  /** @var string $query */
  public $query;
  /** @var CDbColumnSchema[] $columns */
  private $columns;
  /** @var boolean $fresh */
  private $fresh;
  /** @param CActiveRecord $class
   * @param CDbConnection $db
   */
  public function __construct($class,$db){
   $this->class = $class;
   $this->createtpl();
   parent::_construct($db);
  }
  private function createtpl(){
   $this->fresh = true;
   $value_tpl = "";
   $columns_string = "";
   $this->columns = $this->class->getMetaData()->tableSchema->columns;
   $counter = 0;
   foreach($this->columns as $column){
    /** @var CDbColumnSchema $column */
    if($column->autoIncrement){
     $value_tpl .= "0";
    }else{
     $value_tpl .= "\"%s\"";
    }
    $columns_string .= $column->name;
    $counter ++;
    if($counter != sizeof($this->columns)){
     $columns_string .= ", ";
     $value_tpl .= ", ";
    }
   }
   $this->insert_tpl = sprintf($this->insert_tpl, $this->class->tableName(), $columns_string);
   $this->value_tpl = sprintf($this->value_tpl, $value_tpl);
  }
  /**
   * @param CActiveRecord $record
   */
  public function add($record){
   $values = array();
   $i = 0;
   foreach($this->columns as $column){
    if($column->autoIncrement){
     continue;
    }
    $values[$i] = $this->class->{$column->name};
    $i ++;
   }
   if(!$this->fresh){
    $this->query .= ",";
   }else{
    $this->query = "values";
   }
   $this->fresh = false;
   $this->query .= vsprintf($this->value_tpl, $values);
   return true;
  }
  public function execute(){
   $this->setText($this->insert_tpl." ".$this->query);
   return parent::execute();
  }
}
Copy after login

The method of use is to add data one by one through the add method, and then call execute.

Related recommendations:

How Yii filters bad code

Yii solves DeleteAll connection table deletion error problem

Yii2 integrates Xunsou to achieve efficient Chinese word segmentation retrieval

The above is the detailed content of Detailed explanation of Yii framework's simple extension class for batch inserting data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!