How can I do batch updates in CodeIgniter instead of firing query each and every time in database?
Mysql can do multiple updates or inserts. Usually in an Active Record pattern you do inserts one by one, but for bulk updates or inserts you can do this.
$sql = "INSERT INTO table (id,Col1,Col2) VALUES (1,1,1),(2,2,3),(3,9,3),(4,10,12) ON DUPLICATE KEY UPDATE Col1=VALUES(Col1),Col2=VALUES(Col2);";
$this->db->query($sql);
CodeIgniter's active record class has an insert_batch() method that does just this and takes care of escaping the data.
$data = array(
array('name' => 'John', 'email' => 'john#email.com'),
array('name' => 'Sue', 'email' => 'sue#email.com')
);
$this->db->insert_batch('my_table', $data);
http://codeigniter.com/user_guide/database/active_record.html
For the sake of other old-fashioned Code Igniter users (like myself):
You’re using an old version? insert_batch and update_batch have been around for over a year [Sep/2010]. If you’re not going to upgrade you would have to run an insert query for each row, or manually construct a batch insert statement.
From: http://codeigniter.com/forums/viewthread/188416/#891199
This question was about updates, but the accepted answer is for inserts.
Here is how you do a batch update:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
This example is from the CodeIgniter user guide:
http://ellislab.com/codeigniter/user-guide/database/active_record.html#update
Related
Could someone please provide a simple example of the usage for dealing with Odoo's one2many, many2many and selection fields when using Laradoo (or ripcord)?
Specifically how one would use them with create() and update(). In Python, it seems as if these are dealt with using special tuple commands however for PHP documentation seems very hard to find for these types of things and it would be extremely helpful.
For illustrative purposes in my particular project, I haven't been able to figure out how to relate a CRM lead tag to a lead during the creation process using Laradoo:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => 0, <-- what do we pass here for this selection field?
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => [1], <-- What do we pass here for this one2many field?
]);
In the example above when trying to set the priority selection field to an int other than 0 fails and when trying to pass an array of tag_ids (1 is valid tag id in my project), the lead remains untagged.
First of all selection field values are just string values that need to be part of the field defined selection values.
The values for relational fields like Onetomany and Many2many are ruled by the command formated values that you could read at:
https://github.com/odoo/odoo/blob/11.0/odoo/models.py#L3020-L3055
For the php api usage with ripcord you could set the tag_ids field value like:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => '0',
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => array(array(4,1)),
]);
This translate as that 1 is the id of a known and already existing crm.lead.tag that you could link to the m2m tag_ids field using the command 4. This could also be expressed using command 6 to link multiple ids on the same command value:
'tag_ids' => array(array(6,0,array(1,2,3))),
where using command 4 it will be:
'tag_ids' => array(array(4,1), array(4,2), array(4,3)),
i'm newbie in cakephp and i trying to update multiple rows in one transaction like:
$Model->saveMany($data, array('deep' => true));
... And the structure of the $data array is:
$data = array(
(int) 1 => array( 'Item' => array('id' => 2, 'name' => 'Name 1') ),
(int) 2 => array( 'Item' => array('id' => 3, 'name' => 'Name 2') ),
);
I Already tried with saveAll instruction and without deep parameter but nothing :( .... what's wrong?
Thanks for the help :)
The problem was that it had a required field in the validation that, although it was not compromised in the update, anyway it had to be passed in command
Thank you all!!
You can use this following code to insert data in Cake Php,
$this->request->data = Hash::insert($this->request->data);
I have the following code written in codeigniter.
foreach ($data as $value) {
# item_transactions fields and values
$data = array (
'checklist_item_id' => ($this->set['table'] == 'checklist_item') ? $this->set['id'] : null,
'imp_sequence_no' => $value['Sequence No.'],
'imp_vendor_tin' => $value['Vendor TIN'],
'imp_vendor_name' => $value['Vendor Name'],
'imp_input_vat_per_client' => floatval(str_replace(',', '', trim($value['Input VAT per client']))),
'imp_gsi' => $value['Goods/Services/Importations']
);
# Insert to item_transactions table
$id = parent::insert($data);
$count++;
}
return $count;
What this code do, is to insert a thousands of records to a database table, from the csv file imported.
$data is an associative array of fields and values to be inserted.
The problem
When I tried to import a csv file containing more than 4,000 records, I got a PHP error saying Maximum execution time of 30 seconds exceeded.
I don't want to change the php.ini setting, because mostly you don't have control on the live servers settings.
Does anyone can suggest best way to solve this problem?
Codeigniter has a query builder class that supports batch upload. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
Producess:
INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
Change your php.ini file settings. max_execution_time = 300 will be enough. Restart your server.
I am new to php codeigniter, Please any one tell me how to insert form fields in mysql using codeigniter. Thanks in advance.
Well, study the codeigniter Active Record, you will find all the ways to insert data into the database, including your form fields as well. It is available on codeigniters website as well, codeigniter is a well documented php framework, so you will find lots of help out there.
https://ellislab.com/codeigniter/user-guide/database/active_record.html
Here you will find all the guide.
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
controller
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->modelname->form_insert($data);
model
function form_insert($data){
$this->db->insert('tablename', $data);}
view
<?php echo form_label(' Name :'); ?>
<?php echo form_error('Name'); ?>
<?php echo form_input(array('id' => 'id', 'name' =>'Name')); ?>
How can I update multiple rows using active record?
My array
foreach($services as $service){
$this->service_batch[]=array(
'service_id'=>$service->service_id,
'service_count'=>$service->service_count,
'id_customer'=>$service->id_customer,
'id_section'=>$service->id_section);
}
When I update the table, I need define 2 parameters in the WHERE clause (id_customer AND id_section), but if I use update_batch, I can specify only one parameter.
How can I set a few parameters in the WHERE clause?
something like this from the Code Igniter Documentation - See Update Batch :-
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->where($this->db->where('id', $id);
$this->db->update_batch('mytable', $data, 'title');
in your case data will be replaced by $this->service_batch and title by your desired column.
Not quite sure of catching what you exactly needs, but i would do:
//controller
foreach($services as $service){
$this->model->update(
$service->service_id,
array(
'service_count'=>$service->service_count,
'id_customer'=>$service->id_customer,
'id_section'=>$service->id_section
));
}
//model
function update($id,$data){
$this->db->where('id',$id);
return $this->db->update('my_table',$data);
}