Welcome Guest, Not a member yet? Register   Sign In
Data handling, Arrays, IDs and Active Record
#1

[eluser]Unknown[/eluser]
Hello everyone,

I'm new to CodeIgniter and like it very much. It seems to be the perfect lightweight framework to use it as a base.

My question is about the Active Record Pattern. I'm working on a function that reads data from the database und updates the timestamps of the loaded data. My code looks like this:

Code:
// load data from database
$this->db->where($where);
$query = $this->db->get('table');
$data = $query->result();

if(is_array($data)) {
    
    $ids = FALSE;
    
// build new array of ids
    foreach($data as $entry) {
        $ids[] = $entry->id;
    }
    
// update data with array
    $this->db->where_in('id', $ids);
    $this->db->update('table');
    
    return $data;
}

I was wondering if I'm missing something to shorten the 'foreach' part a bit and maybe don't need to set up a new array for this update part. Like a helper-function or something I don't know about the Active Recored Class that makes it easier to gather the IDs of the data for the update and doesn't make it look so awkward.

Thank you in advance.

Oliver
#2

[eluser]Seb[/eluser]
The code you posted doesn't affect the database on update because you need to supply an array as the second parameter to the update() function, with the values to update on the database.

What do you want to do with the extracted ids ?

FYI, you can also rewrite your whole sequence with:

Code:
$this->db->where($where)->update('table', $ARRAY_TO_PUT_HERE);




Theme © iAndrew 2016 - Forum software by © MyBB