Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert multi rows in one query #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ To update the database, change one or more of the properties of the object, then
// Syncronise the object with the database
$person->save();

### Creating new records ###

### Creating new record ###
#### Method 1 - single record ####
To add a new record, you need to first create an "empty" object instance. You then set values on the object as normal, and save it.

$person = ORM::for_table('person')->create();
Expand All @@ -356,6 +356,17 @@ To add a new record, you need to first create an "empty" object instance. You th
$person->save();

After the object has been saved, you can call its `id()` method to find the autogenerated primary key value that the database assigned to it.
#### Method 2 - mass insert by one query####

$data[]=array('col1'=>'1','col2'=>'2');
$data[]=array('col2'=>'4','col1'=>'3');
$data[]=array('col1'=>'4','col2'=>'3');
$row=ORM::for_table('aaa')->create();
$row->save_multi($data);

$data are hash table.
Result query:
INSERT INTO `aaa` (`one`, `two`) VALUES (?, ?), (?, ?)

### Checking whether a property has been modified ###

Expand Down
66 changes: 64 additions & 2 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,52 @@ public function save() {
$this->_dirty_fields = array();
return $success;
}
private function _recursive_keys($input, $unique=true){
if(!$unique) {
$output=array_keys($input);
}else{
$output=array();
};
foreach($input as $sub){
if(is_array($sub)){
$output = array_merge($output, $this->_recursive_keys($sub,false)) ;
}
}
$output=array_unique($output);
return $output;
}
private function _recursive_value($a,$f=array()){
$keys=$this->_recursive_keys($a);
foreach($a as $value){
foreach($keys as $value2){
$return[]=$value[$value2];
};
};
return $return;
}
/*
* Build an INSERT query mulit rows in one query
*/
public function save_multi($data) {
$query = array();
$query = $this->_build_insert_many($data);
$values = $this->_recursive_value($data);
var_dump($query,$values);
self::_log_query($query, $values);
$statement = self::$_db->prepare($query);
$success = $statement->execute($values);

// If we've just inserted a new record, set the ID of this object
if ($this->_is_new) {
$this->_is_new = false;
if (is_null($this->id())) {
$this->_data[$this->_get_id_column_name()] = self::$_db->lastInsertId();
}
}

$this->_dirty_fields = array();
return $success;
}
/**
* Build an UPDATE query
*/
Expand All @@ -1092,10 +1137,28 @@ protected function _build_update() {
return join(" ", $query);
}

/**
* Build an many INSERT query
*/
protected function _build_insert_many($data) {
$query[] = "INSERT INTO";
$query[] = $this->_quote_identifier($this->_table_name);
$field_list = $this->_recursive_keys($data);
$field_list = array_map(array($this, '_quote_identifier'),
$field_list);
$query[] = "(" . join(", ", $field_list) . ")";
$query[] = "VALUES";
foreach($data as $value){
$placeholders=$this->_create_placeholders(count($field_list));
$rows[]="({$placeholders})";
};
$query[] = join(', ',$rows);
return join(" ", $query);
}
/**
* Build an INSERT query
*/
protected function _build_insert() {
protected function _build_insert($data) {
$query[] = "INSERT INTO";
$query[] = $this->_quote_identifier($this->_table_name);
$field_list = array_map(array($this, '_quote_identifier'), array_keys($this->_dirty_fields));
Expand All @@ -1106,7 +1169,6 @@ protected function _build_insert() {
$query[] = "({$placeholders})";
return join(" ", $query);
}

/**
* Delete this record from the database
*/
Expand Down