Issues writing functions when database column ends in _image

I've been trying to write a model that uses a different set of images for the thumbnails and main images and have had some trouble trying to get both image fields to work correctly.

I've tried writing my own get_thumb_image method, but if the database column ends in _image, then it just won't work. Any ideas why this is? I've tried multiple different approaches and this still seems to be the problem. I have tried adding _upload to the field name in the form_fields method but still no luck. Code is below:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once(FUEL_PATH.'models/base_module_model.php');

class Services_model extends Base_module_model {

public $required = array('name');


function __construct()
{
parent::__construct('services'); // table name
}

function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc')
{
$data = parent::list_items($limit, $offset, $col, $order);
return $data;
}


function form_fields($values = array())
{
$fields = parent::form_fields($values);
$fields['thumb_image']['upload_path'] = assets_server_path('services/thumb/', 'images');
$fields['thumb_image']['overwrite'] = TRUE;

return $fields;
}
}

class Service_model extends Data_record {
public function get_block()
{
$output = '<article class="service">';
$output .= $this->get_thumb_image();
$output .= '

'.$this->name.'

';
$output .= '';
return $output;
}

public function get_thumb_image()
{
if(!empty($this->thumb_image)){
return 'imagethumb_image).'" />';
} else {
return false;
}
}

public function get_image()
{
if (!empty($this->image)) {
return 'imageimage).'" />';
} else {
return false;
}
}

}
?>

My database structure looks like this: http://brandonboswellphoto.com/dropbox/screenshot.png

Comments

  • edited 6:06AM
    Sorry about the formatting, is there a way to comment the code so that it doesn't do that?
  • edited 6:06AM
    FUEL will automatically create a "thumb_image_upload" file input field if it sees a field that ends with "_image" or "_img" (this code happens in the parent Base_module_model::form_fields method if you are interested). You then need to add the "upload_path" to that field like so:
    function form_fields($values = array()) { $fields = parent::form_fields($values); $fields['thumb_image_upload']['upload_path'] = assets_server_path('services/thumb/', 'images'); $fields['thumb_image_upload']['overwrite'] = TRUE; return $fields; }
    Use the "code" tag for formatting.
  • edited 6:06AM
    So i've updated my form_fields method and the files are being uploaded in the correct place, however I'm still running into an issue with my get_thumb_image method. It doesn't return the information from the thumb_image column of the data base. Code is below

    Data Record Class that contains the method
    class Service_model extends Data_record { public function get_block() { $output = '<article class="service">'; $output .= $this->get_thumb_image(); $output .= '<h2>'.$this->name.'</h2>'; $output .= '</article>'; return $output; } public function get_thumb_image() { if(!empty($this->thumb_image)){ return '<img src="'.img_path($this->thumb_image).'" />'; } else { return false; } } public function get_image() { if (!empty($this->image)) { return '<img src="'.img_path($this->image).'" />'; } else { return false; } } }

    View File Calling the Method
    <?php $service = $CI->uri->segment(2); $CI->load->model('services_model'); ?> <h1>Services <?php if (!empty($service)) : ?> : <?=$service->name?> <?php endif; ?></h1> <section class="services"> <?php if (!empty($service)): ?> <?php $service = $CI->services_model->find_one_by_id($service); ?> <?=$service->get_block()?> <?php else: ?> <?php $services = $CI->services_model->find_all();?> <?php foreach($services as $service) : ?> <?php echo $service->get_block();?> <?php endforeach; ?> <?php endif ?> </section>
  • edited April 2012
    Try the following change:
    class Service_model extends Data_record { public function get_block() { $output = '<article class="service">'; $output .= $this->get_thumb_image(); $output .= '<h2>'.$this->name.'</h2>'; $output .= '</article>'; return $output; } public function get_thumb_image() { if(!empty($this->_fields['thumb_image'])){ return '<img src="'.img_path($this->_fields['thumb_image']).'" />'; } else { return false; } } public function get_image() { if (!empty($this->_fields['image'])) { return '<img src="'.img_path($this->_fields['image']).'" />'; } else { return false; } } }
    Note the use of $this->_fields['thumb_image'] and $this->_fields['image']. If you are overwriting a property value like that, you need to reference the _fields array because of how the magic method works (otherwise it can go into an infinite loop because $this->image = $this->get_image()).
  • edited 6:06AM
    That solved it! I apologize for my ignorance, didn't know you could call those methods like that!

    Thank you!
Sign In or Register to comment.