Active record update with on_after_post() hook

edited May 2012 in Modules
I've got a photos / gallery model that uses on_after_post() to create thumbnails, and that aspect works fine. I thought I'd add to the functionality by saving the thumb filename to the table from on_after_post().

So I have this:
function on_after_post($values) { $CI =& get_instance(); $CI->load->library('image_lib'); $pk = $values['id']; // create the thumbnail if an image is uploaded if (!empty($CI->upload)) { $data = $CI->upload->data(); if (!empty($data['full_path'])) { // create thumb $config = array(); $config['create_thumb'] = TRUE; $config['source_image'] = $data['full_path']; $config['width'] = 100; $config['height'] = 80; $config['master_dim'] = 'auto'; $config['maintain_ratio'] = TRUE; $config['thumb_marker'] = '_thumb'; $CI->image_lib->clear(); $CI->image_lib->initialize($config); if (!$CI->image_lib->resize()) { $this->add_error($CI->image_lib->display_errors()); } else // store thumb filename for convenience { $thm = $data['raw_name'] . $config['thumb_marker'] . $data['file_ext']; $this->_update_thumb_field($pk, $thm); } } } return $values; } function _update_thumb_field($pk, $thm) { if(is_numeric($pk)) { $this->db->set('thumbnail_file', $thm); $this->db->set('id', $pk); /* without this, id gets set to 0 */ $this->db->where('id', $pk); $this->db->update('photos'); } }

Now here's what I find odd - without the id being explicitly set(), the update leaves id as 0, and yet $pk is always the right index value for the table. In SQL terms that just seems unnecessary (eg UPDATE photos SET id = x, thumbnail_file = 'xyz_thumb.gif' WHERE id = x). Is doing an update from this hook likely to lead to any race conditions? This is more likely a MySQL issue or a Codeigniter one, but maybe I'm misunderstanding what can be done with on_after_post(). Incidentally, if this is the last hook to trigger (is it?) does there need to be a return for $values?

Comments

  • edited 7:38AM
    I worked out a more satisfactory way of getting Active Record to do what I wanted:
    function _update_thumb_field($pk, $thm) { if(is_numeric($pk)) { $where = 'id = '. $pk; $data = array('thumbnail_file' => $thm); $str = $this->db->update_string('photos', $data, $where); $this->db->query($str); $this->db->get('photos'); } }

    So that avoided having to over-write the id using set(), which offended me beyond reasonable measure.

    I'd like to know why the update method seems to railroad me into that situation though.
  • edited 7:38AM
    There doesn't need to be a return of values. Could you do a $this->db->debug_query() right after $this->db->update('photos') to see what it outputs for the query?
  • edited 7:38AM
    Yup I did some debugging and the update() method was insisting on using SET `id` = '', and since there was no id being explicitly set, it found the right record, and overwrote the id with 0. Why it persisted in this I don't understand. I wonder if - ironically - it is because of some safety setting somewhere? Whether from CI or MySQL I'm not sure.

    Still, the alternative is satisfactory.
Sign In or Register to comment.