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 '
thumb_image).'" />';
} else {
return false;
}
}
public function get_image()
{
if (!empty($this->image)) {
return '
image).'" />';
} else {
return false;
}
}
}
?>
My database structure looks like this:
http://brandonboswellphoto.com/dropbox/screenshot.png
Comments
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.
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>
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()).
Thank you!