Hello;
I've found an issue with using custom models, and I don't know if it is a genuine bug, or I'm lacking an action due to not enough clear documentation.
I'm trying to make a custom way to upload scanned images of documents. I've a table of actors, with the basic info for a person, with an autogenerated table.
Then, I've a table of attachments or uploads. Since thay are big files, using the provided upload system it is not feasible, so I implemented a library to encapsulate storing a file, and generate miniatures to see in the table view. To do so, I needed an autogenerated field calculated from the Id of the row (the primary key) and a file hash, that I would use to rename the file and prevent collisions. Also to store the file outside of the www/ folder, so to view them I must use a controller to show the miniatures, but not before checking if you have permission to view the picture.
But I've been incapable to make it work, failing in the easiest part, so either the doumentation is incomplete, or it could be a bug.
After simplifiying everything as much as possible for testing, I ended just adding the field
`id` AS `itemId`
Since I can't add a field with MySQL functions to the autogenerated queries, I have to use a custom model. Ok, it's fair. Here is the model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once(APPPATH.'models/Grocery_crud_model.php'); class Protected_files_model extends Grocery_CRUD_model { protected $repository_path = 'temp'; protected $repository_table = 'temp'; function __construct() { parent::__construct(); } function _initialize($table, $path) { $this->repository_table = $table; $this->repository_path = $path; } function get_list() { $this->db->select('`'.$this->repository_table.'`.*'); $this->db->select('`'.$this->repository_table.'`.`id` AS `itemId`'); $this->db->from($this->repository_table); $this->db->order_by('`type` ASC, `update_date` ASC'); return $this->db->get()->result(); } function db_insert($post_array) { if ($this->field_exists('update_date')) { $this->load->helper('date'); $post_array['update_date'] = date('Y-m-d H:i:s',now()); } parent::db_insert($post_array); } function db_update($post_array, $primary_key_value) { if ($this->field_exists('update_date')) { $this->load->helper('date'); $post_array['update_date'] = date('Y-m-d H:i:s',now()); } parent::db_update($post_array, $primary_key_value); } }
As you can see, it only adds itemId to the list query, and force the update of timestamps on creation or modification.
The controller is like this:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Documents extends CI_Controller { function __construct() { parent::__construct(); /* Standard Libraries */ $this->repo_table = 'repodocs'; $this->repo_path = '/var/private/documents/actors/'; $this->load->database(); $this->load->model('Protected_files_model', 'actor_files'); $this->actor_files->_initialize($this->repo_table, $this->repo_path); $this->load->library(array('grocery_CRUD')); include(set_realpath(FCPATH.'assets/scriptlets/image_resize.inc.php')); } function miniature($value) { return '<a href="/index.php/Documents/preview/full/'.$value.'" target="_blank">' .'<img src="/index.php/Documents/preview/mini/'.$value.'" width="100" >' .'</a>'; } function preview($size, $id) { /* resizing image to output stream */ resize_image($size,null, $imagefile,'image/png'); } public function listing() { $crud = new grocery_CRUD(); $crud->set_model( 'actor_files' ); $crud->set_table( $this->repo_table ); // Columns to view in table mode $crud->columns('actores_id', 'update_date', 'type','description','expiration', 'itemId'); // Columns to see in edit mode $crud->fields('actores_id','type', 'description', 'expiration', 'url'); // Required fields $crud->required_fields('actores_id','type','url'); // Actions if (!$this->ion_auth->in_group('contracts')) { $crud->unset_clone(); $crud->unset_read(); $crud->unset_add(); $crud->unset_edit(); $crud->unset_delete(); } else { $crud->add_action('documents', '/assets/grocery_crud/themes/flexigrid/css/images/clone.png', 'Documents/listing','ui-icon-document'); } ; // Index fields for options $crud->set_relation('actors_id','actores', '{name} {surname} ({id})'); $crud->change_field_type('type', 'dropdown', array('photo' => 'Photo', 'contract' => 'Contract', 'medical' => 'Medical insurance')); $crud->callback_column('itemId', array($this,'miniatures')); $form = $crud->render(); $this->load->view('view.php', $form); } }
In the controller, it does show the image generated by the callback, replacing the itemId field.. But when entering to edit the row, the fields are loaded empty.
If I remove or comment
//$crud->set_model( 'actor_files' );
the Input dialogs then are filled and work ok, but the callback miniatures fails because the autogenerated query does lack `itemId`.
Whell, what do I need to do? Am I missing some function or value of the model to set up or something? Or it is a bug?