How to Export Codeigniter 3 Database with bulk insert - php

I am trying to export the database from Codeigniter 3 the insert query is not bulk.
it crease each row as a new query using $this->load->dbutil(); and The format agr is
$format = array(
'ignore' => array($this->ignore_directories),
'format' => 'zip',
'filename' => 'db_backup_' . $date . '.sql',
'add_insert' => TRUE,
'newline' => "\n"
);
backup Statement
$backup = $this->dbutil->backup($format);
The problem is that exported data is not in the bulk import format. it simply creates each import as a query which is slow and time-consuming also sometimes it failed to import because PHP time limit.

Try this
$this->load->dbutil();
$db_format=array('format'=>'zip','filename'=>'YOUR DB NAME.sql');
$backup=& $this->dbutil->backup($db_format);
$dbname='backup-on-'.date('Y-m-d').'.zip';
$save='YOUR FOLDER PATH'.$dbname;
write_file($save,$backup);
force_download($dbname,$backup);
This will help you to export the db in zip file.When you import the extracted file,the db imported fully or bulky.Hope it will helps!!

Related

How can i backup my database with lashes?

Hello i wish someone can help me it's my presentation tomorrow i would like to ask why does my values does not have lashes? ``` when i backup my database in php? i'm using codeigniter can someone please help me whenever i back up it it turns
INSERT INTO `discount` (`id`, `name`, `discount_percent`, `active`) VALUES (3, Student, 10, 1);
and it's giving me an error i think he want the value have lashes like this
INSERT INTO `discount` (`id`, `name`, `discount_percent`, `active`) VALUES ('3', 'Student', '10', '1');`
i tried the first one in my localhost but failed and the second one with lashes is successful so how can i backup with lashes?
my code is:
public function create()
{
// Load the DB utility class
$this->load->dbutil();
$test = date('YmdS-His');
// Backup your entire database and assign it to a variable
$config = array (
'format' => 'zip', // gzip, zip, txt
'filename' => 'bubblebee_'.$test.'_db.sql', // File name - NEEDED ONLY WITH ZIP FILES
'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
'add_insert' => TRUE, // Whether to add INSERT data to backup file
'newline' => "\n", // Newline character used in backup file
'foreign_key_checks' => FALSE,
);
$backup =& $this->dbutil->backup($config);
$db_name ='bubblebee_'.$test.'.zip';
$this->load->helper('download');
force_download($db_name, $backup);
}

How to export child array element to excel in Laravel

I'm building an application in Laravel 5.5, with matwebsite/excel plugin where I'm having an array element to be exported into csv file. I'm able to get the values when I assign single string to key inside an array, but currently I have child array element which is present into the array, which is giving me blank excel sheet once included the same.
Without array elements:
public function investor(Request $request)
{
$record_set = [];
$tables = json_decode($request->investorTable);
foreach($tables as $table)
{
$record_set[] = array(
'schedule' => $table->schedule,
'Event type' => $table->event_type,
'Venue' => $table->venue,
'Nature' => $table->nature,
);
}
return Excel::create('investor', function($excel) use ($record_set) {
$excel->sheet('mySheet', function($sheet) use ($record_set) {
$sheet->fromArray($record_set);
});
})->download('csv');
}
this is working perfect but I want to place an array element whose name key I want to export, I don't know how to implement when I implement it it gives me blank sheet,
$record_set[] = array(
'schedule' => $table->schedule,
'Company Name' => $table->companyName,
'Contact Person' => $table->contact_participants,
'Event type' => $table->event_type,
'Venue' => $table->venue,
'Nature' => $table->nature,
);
my complete array element is something like this:
and the table looks into my html something like this:
I want exactly same output in excel, help me out with this.
Thanks.
You can use
Export to Excel5 (xls)
Excel::create('Filename', function($excel) {
})->export('xls');
// or
Export to Excel2007 (xlsx)
->download('xls');
Export to Excel2007 (xlsx)
->export('xlsx');
// or
->download('xlsx');
Export to CSV
Export to CSV
->export('csv');
// or
->download('csv');
Please refer that documentation
http://www.maatwebsite.nl/laravel-excel/docs/export

Codigniter backup library creating wrong insert query

i am creating backup using codigniter library "dbutil" and i have fields in my mysql database which having datatype bit when using below given code
public function run_backup()
{
$filename="MembersPro_database_$this->curr_date.sql";
$filepath="application/upload/DatabaseBackup/$filename";
$dbfilepath="MembersPro/application/upload/DatabaseBackup/$filename";
$prefs = array(
'ignore' => array(), // List of tables to omit from the backup
'format' => 'sql', // gzip, zip, txt
'filename' =>$filepath, // File name - NEEDED ONLY WITH ZIP FILES
'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
'add_insert' => TRUE,
"foreign_key_checks" =>FALSE
/* 'newline' => "\n",*/
// Newline character used in backup file
);
$backup="CREATE DATABASE IF NOT EXISTS `MembersManagmentSystem`; USE `MembersManagmentSystem` ";
$backup .= $this->dbutil->backup($prefs);
if(!write_file($filepath, $backup))
{
echo "Error";die;
}
else
{
$_SESSION['sucessmsgbackup']="true";
}
$this->insert_into_datbase($filename,$dbfilepath);
redirect("ViewDatabaseBackup");
}
getting wrong output insert query
INSERT INTO `User` (`userId`, `userRoleId`,`userActive`, `userIsDelete`) VALUES ('20000046', '20001','1', '0');
in above given query "userActive" and "userIsDelete" fields having datatype "bit" and the query create by DbUtil library is treating it as string so i am getting warning error in mysql
"out of range column value"
Read Here Why you should not use BIT columns in MySQL
Issue reported Here in Github
Look into folder and modify core classes so that it will not escape, for example for mysqli driver
system/database/drivers/mysqli
https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/drivers/mysqli/mysqli_utility.php#L159
and find file mysqli_utility.php
locate line
$is_int[$i] = in_array(strtolower($field->type),
array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'),
TRUE);
Type numbers
numerics
-------------
BIT: 16
TINYINT: 1
BOOL: 1
SMALLINT: 2
MEDIUMINT: 9
INTEGER: 3
BIGINT: 8
SERIAL: 8
FLOAT: 4
DOUBLE: 5
DECIMAL: 246
NUMERIC: 246
FIXED: 246
and add your datatype to above array like below
$is_int[$i] = in_array($field->type,
array(16, 1, 2, 9, 3, 8),
TRUE);

Data not saved in database cakephp

I have an event table with this fields:
I use this code to insert data in this table :
$oneEvent = array(
'trainer_id' => $event['trainer_id'],
'formation_id' => $event['formation_id'],
'title' => $event['title'],
'start' => $event['start'][$i],
'end' => $event['end'][$i],
);
$success = $this->Event->save($oneEvent);
$events_id= $this->Event->inserted_ids;
when I run this code I get true and ID of insert element (showed using debug)
but in database i can't see this field never !!!!.
and when I insert data in phpmyadmin when this request INSERT INTO events(title,start, end, trainer_id, formation_id) VALUES ('Départ B','2016-11-18 10:00:00','2016-11-18 11:00:00','13','1') it worked
I didn't know what happen here !!??
I solved the problem by using $dataSource->commit(); after calling save() function

How to get SQL Query on model->save() in CakePHP 3?

How can I view the SQL Query on model->save() in CakePHP 3? Is there any way to do this? I want to get the specific sql query e.g when I save new entity.
I need it because I want to save that to a log file in some cases.
My bootstrap.php log config:
Log::config('current', [
'className' => 'File',
'path' => LOGS.DS.date('Y-m').DS,
'scopes' => ['daily','queriesLog'],
'file' => date('Y-m-d'),
]);
What i want to get:
e.g when i save entity:
$this->Clients->save($client);
i want to log something and i want to save INSERT sql query with this log
Log::warning('test\n', ['scope' => ['daily']]);
A solution could be using query logging
http://book.cakephp.org/3.0/en/orm/database-basics.html#query-logging
you can turn query logging on just when you save your model and then turn it off
For example I have a Comments model
In my bootstrap.php I did
Log::config('current',
[
'className' => 'File',
'path' => LOGS.date('Y-m').DS, // you don't need a DS between LOGS and date()
'scopes' => ['daily','queriesLog'],
'file' => date('Y-m-d'),
]);
and in my controller I did
$conn = \Cake\Datasource\ConnectionManager::get('default');
$comment = $this->Comments->get(5620); // 5620 is the id of one comments of mine
$conn->logQueries(true);
$comment = $this->Comments->get(5619); // 5619 is onother id of one comments of mine
$comment->text = 'Test';
$this->Comments->save($comment);
$conn->logQueries(false);
In this way a file is created in the logs folder and the file contains the following
2015-12-16 13:38:35 Debug: SELECT ... WHERE Comments.id = 5619 LIMIT 1
2015-12-16 13:38:35 Debug: BEGIN
2015-12-16 13:38:35 Debug: UPDATE comments SET text = 'Test' WHERE id = 5619
2015-12-16 13:38:35 Debug: COMMIT
note that the query used to get comment #5620 has not been logged
Also note that this works if you don't have debugkit enabled
Put this into model
function getLastQuery() {
Configure::write(‘debug’,false);
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
$latQuery = $lastLog['query'];
echo "<pre>";
print_r($latQuery);
}
after $this->save($data);
call that function
$this->getLastQuery();
and echo it.
Try it.

Categories