Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
a3963851
Commit
a3963851
authored
Feb 01, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Command::batchInsert()
parent
761a9f44
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
89 additions
and
2 deletions
+89
-2
Command.php
framework/db/Command.php
+27
-2
QueryBuilder.php
framework/db/QueryBuilder.php
+27
-0
QueryBuilder.php
framework/db/mysql/QueryBuilder.php
+35
-0
No files found.
framework/db/Command.php
View file @
a3963851
...
...
@@ -485,16 +485,41 @@ class Command extends \yii\base\Component
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column data (name=>value) to be inserted into the table.
* @param array $params the parameters to be bound to the command
* @return Command the command object itself
*/
public
function
insert
(
$table
,
$columns
,
$params
=
array
()
)
public
function
insert
(
$table
,
$columns
)
{
$params
=
array
();
$sql
=
$this
->
db
->
getQueryBuilder
()
->
insert
(
$table
,
$columns
,
$params
);
return
$this
->
setSql
(
$sql
)
->
bindValues
(
$params
);
}
/**
* Creates a batch INSERT command.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array(
* array('Tom', 30),
* array('Jane', 20),
* array('Linda', 25),
* ))->execute();
* ~~~
*
* Not that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return Command the command object itself
*/
public
function
batchInsert
(
$table
,
$columns
,
$rows
)
{
$sql
=
$this
->
db
->
getQueryBuilder
()
->
batchInsert
(
$table
,
$columns
,
$rows
);
return
$this
->
setSql
(
$sql
);
}
/**
* Creates an UPDATE command.
* For example,
*
...
...
framework/db/QueryBuilder.php
View file @
a3963851
...
...
@@ -115,6 +115,33 @@ class QueryBuilder extends \yii\base\Object
}
/**
* Generates a batch INSERT SQL statement.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array(
* array('Tom', 30),
* array('Jane', 20),
* array('Linda', 25),
* ))->execute();
* ~~~
*
* Not that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @param array $params the parameters to be bound to the command
* @return string the batch INSERT SQL statement
* @throws NotSupportedException if this is not supported by the underlying DBMS
*/
public
function
batchInsert
(
$table
,
$columns
,
$rows
,
$params
=
array
())
{
throw
new
NotSupportedException
(
$this
->
db
->
getDriverName
()
.
' does not support batch insert.'
);
}
/**
* Creates an UPDATE SQL statement.
* For example,
*
...
...
framework/db/mysql/QueryBuilder.php
View file @
a3963851
...
...
@@ -129,4 +129,39 @@ class QueryBuilder extends \yii\db\QueryBuilder
{
return
'SET FOREIGN_KEY_CHECKS='
.
(
$check
?
1
:
0
);
}
/**
* Generates a batch INSERT SQL statement.
* For example,
*
* ~~~
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array(
* array('Tom', 30),
* array('Jane', 20),
* array('Linda', 25),
* ))->execute();
* ~~~
*
* Not that the values in each row must match the corresponding column names.
*
* @param string $table the table that new rows will be inserted into.
* @param array $columns the column names
* @param array $rows the rows to be batch inserted into the table
* @return string the batch INSERT SQL statement
*/
public
function
batchInsert
(
$table
,
$columns
,
$rows
)
{
$values
=
array
();
foreach
(
$rows
as
$row
)
{
$vs
=
array
();
foreach
(
$row
as
$value
)
{
$vs
[]
=
is_string
(
$value
)
?
$this
->
db
->
quoteValue
(
$value
)
:
$value
;
}
$values
[]
=
$vs
;
}
return
'INSERT INTO '
.
$this
->
db
->
quoteTableName
(
$table
)
.
' ('
.
implode
(
', '
,
$columns
)
.
') VALUES ('
.
implode
(
', '
,
$values
)
.
')'
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment