So I needed to be able to prefiliter the results displayed in the GCRUD system, and tried to do so by creating a custom GCRUD model, as follows:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //Modification of grocery_CRUD_model to allow adding join and where clauses class gcrud_query_model extends grocery_CRUD_model { private $join_tbl = null; private $join_str = null; private $where_array = null; function __construct() { parent::__construct(); } function get_list() { $select="`{$this->table_name}`.*"; //set_relation special queries if(!empty($this->relation)) { foreach($this->relation as $relation) { list($field_name , $related_table , $related_field_title) = $relation; $unique_join_name = $this->_unique_join_name($field_name); $unique_field_name = $this->_unique_field_name($field_name); if(strstr($related_field_title,'{')) { $related_field_title = str_replace(" "," ",$related_field_title); $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name"; } else { $select .= ", $unique_join_name.$related_field_title AS $unique_field_name"; } if($this->field_exists($related_field_title)) $select .= ", `{$this->table_name}`.$related_field_title AS '{$this->table_name}.$related_field_title'"; } } //set_relation_n_n special queries. We prefer sub queries from a simple join for the relation_n_n as it is faster and more stable on big tables. if(!empty($this->relation_n_n)) { $select = $this->relation_n_n_queries($select); } $this->db->select($select, false); $this->db->from($this->table_name); //if there is data for a join, add a join clause if ((!is_null($this->join_tbl)) && (!is_null($this->join_str))) { $this->db->distinct(); $this->db->join($this->join_tbl,$this->join_str,'left outer'); } //if there is data for a where, add a where clause if (!is_null($this->where_array)) { $this->db->where($this->where_array); } $results = $this->db->get()->result(); //var_dump($this->db->last_query()); //For debugging echo "<br>"; return $results; } public function set_join_str($join_tbl, $join_str) { $this->join_tbl = $join_tbl; $this->join_str = $join_str; } public function set_where_str($array) { $this->where_array = $array; } }
The idea was to make it work exactly like the basic gcrud, but to allow me to do more advanced filtering - specifically, I wanted to be able to filter based on a joined table.
As far as filtering, everyting works fine. But I get weird behavoir when updating.
$where_array = array("complete" => 0); $crud->basic_model->set_where_str($where_array); $crud->fields('AssignedHerald'); //edit view //foreign key settings $crud->set_relation_n_n('AssignedHerald', 'tblConsultAssign', 'tblHeralds', 'consultId', 'heraldId', 'SCA_Name');
As I understand it, when opening with these settings, clicking on "edit" should only let me see this one many-many field, and it does. But the update doesn't happen. The system just sits there, and FireBug reveals the following: "JSON.parse: unexpected character Line 4"
Note that the following works just fine:
$crud->fields('legalname', 'email', 'phonenumber', 'personalname', 'householdname', 'device', 'personalbadge', 'housebadge', 'complete', 'text', 'AssignedHerald','CreatedAt'); //edit view $crud->required_fields('legalname', 'email', 'phonenumber', 'text', 'CreatedAt'); //validation rules //foreign key settings $crud->set_relation_n_n('AssignedHerald', 'tblConsultAssign', 'tblHeralds', 'consultId', 'heraldId', 'SCA_Name');
Here, many more fields are open, but no pre-filtering is done. I can modify AssignedHeralds, no problems.
The following also works:
$crud->basic_model->set_join_str("tblConsultAssign", "tblConsults.id=tblConsultAssign.consultId"); $where_array = array("tblConsultAssign.heraldId" => $this->ion_auth->user()->row()->ID, "complete" => 0); $crud->basic_model->set_where_str($where_array); $crud->fields('legalname', 'email', 'phonenumber', 'personalname', 'householdname', 'device', 'personalbadge', 'housebadge', 'complete', 'text'); //edit view $crud->required_fields('legalname', 'email', 'phonenumber', 'text'); //validation rules
This view only shows those items assigned to the current user, and the ability to edit works fine, though in this case the ability to change AssignedHeralds is intentionally disabled.
I'm not sure if the problem is with my custom model, or something else, but I'd appreciate help. I'd be happy to post more of the code if needed.