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);
}
Related
I want to insert cart items into database but when I insert data it will insert just a single product so how to insert multiple products at once
Use $this->db->insert_batch();
Example
Your record data should be array format
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
Now pass the set of data to the following function
$this->db->insert_batch('table_name', $data );
Simple use following function for your case, it will help you. add this function to your model file
public function insertAllQuery($table, $valueArray){
$this->db->insert_batch($table, $valueArray);
return ($this->db->trans_status()) ? true : false;
}
pass the card items in a controller as like below
$this->your_model->insertAllQuery('table_name', $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')); ?>
I want to make an if else in filterArgs (in Model) as follow:
public $filterArgs = array(
array('name' => 'to', 'type' => 'value' => 'Product.regular_price' ),
);
I want the field to change base on the condition.
if sale = 1, field is promo_price, else field is regular_price
I already try below code (but unsuccessful):
'field'=> 'Product.sale' => 1 ? 'Product.promo_price >=' : 'Product.regular_price >='
Can someone please help me. Thanks alot in advance!
I would create a virtualField in my Product Model
$virtualFields = array
(
'my_price' => 'IF(Product.sale = 1, Product.promo_price, Product.regular_price)';
)
public $filterArgs = array
(
'my_price' => array('type' => 'value'),
);
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