⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Edit fileds not displaying correctly with Custom Query



toxide
  • profile picture
  • Member

Posted 09 September 2015 - 11:22 AM

Hi ,

 

I've just impletmented a new Custom Query in one of my Grocery crud projects, which works great with the pagination functions as well. But i've noticed that when i select to edit the information I'm missing one of the fields. This fields is the one from the join.

 

I know with the standard query it would normally add a drop the data as a drop down menu, but in the Custom query this is not the case. It displays a blank input filed.

 

Here is an example of the custom query.

 

SELECT
tbl1.column1,
tbl2.column1,
tbl2.column2
FROM
tbl2
INNER JOIN tbl1 ON tbl2.column1 = tb1.column1

 

I have tried a set relation but for some reason half the data dont match up and the other work correctly. (which i thought was quite weird).

 

Any help would be great.

 

Thanks


marlaaragao
  • profile picture
  • Member

Posted 09 September 2015 - 11:50 AM

I think in this case the only way is to use the callback_edit_field function to populate it with the data you want. Then use the callback_before_update to take off the field from the post_array so you won't get an error (column does not exist) when saving on your principal table, and then, with the callback_after_update, you can save the value on your joined table. You can save the data that will be saved on the callback_after_update on a session variable. I do it like this.

 

=D


toxide
  • profile picture
  • Member

Posted 09 September 2015 - 13:13 PM

Hi marlaaragao

 

ok that's great thanks for the feedback. Would you be able to supply any examples. Been looking but getting a bit stuck.

 

Im new to customising CI :)

 

Thanks


marlaaragao
  • profile picture
  • Member

Posted 09 September 2015 - 13:52 PM



   $this->grocery_crud->set_table('tb2');

   [...]

   $this->grocery_crud->fields('tbl1.column1, tbl2.column1, tbl2.column2'); //Put the names of the fields
   $this->grocery_crud->callback_edit_field('tbl1.column1', array($this, '_callback_edit_tb1_column1'))   ;

   $this->grocery_crud->callback_before_update(array($this, '_callback_before_update'));
   $this->grocery_crud->callback_after_update(array($this, '_callback_after_update'));


   [...]

   $this->grocery_crud->render();  

   [...]

}

//The callback edit function will return your input
//I made an example with a select, but you could just return an input
function _callback_edit_tb1_column1($value, $primary_key) {
   $input = '<select id="tb1.column" name="tb1.column">' //(the name attribute must be the same of your field)
   
   $results = $this->TB1_model->findAll(); //find the data you want
   
   if ($results) {
        foreach ($results as $data) {
             $input .= '<option value="' . $data->id . '">' . $data->description . '</option>'
        }
   }
   
   $input .= '</select>';
   return $input;
}

//callback_before_update
_callback_before_update($post_array, $primary_key) {

   $this->session->set_userdata('your_TB1_field_name', $post_array['your_TB1_field_name']);
   unset($post_array['your_TB1_field_name']);
   return $post_array;
}

//callback_after_update
_callback_after_update($post_array, $primary_key) {
   $field_value = $this->session->userdata('your_field_name');
   if ($field_value) {
      $this->db->where('tb2_column1', $primary_key);
      $this->db->update('tb1', array('tb1.column' => $field_value));
      $this->session->unset_userdata('your_field_name');

   }
} 

Hi! I did something like that in my case.. That's just a small example for you to have an idea of the thing..

 

I don't know if that's your case, but I hope it helps.

PS.: I'm not on my own computer, so I did not test this, but I think you'll be able to start from there..

 

=]


toxide
  • profile picture
  • Member

Posted 09 September 2015 - 14:38 PM

Hi marlaaragao

 

Thats great, thanks alot for the example. I also need this as a drop down menu as the field im getting stuck with is a list of car models.

 

I have a Submodel table which should retrieve the model from the model table (This is set as an ModelID in my Submodel table).

 

is this a model which i need create $this->TB1_model->findAll();.

 

Sorry if this sounds a bit stupid of me but this is first time i have to use these sort of actions in my CI.


marlaaragao
  • profile picture
  • Member

Posted 09 September 2015 - 22:18 PM

 

Hello!

 

I not sure I understand your question...

 

In my example, I return an dropdown field in the callback_edit_tb1...

If I undertood correctly your question, yes, you should replace the $this->TB1_model->findAll() for the call to the function that will return the car models you want. So you can replace the options on '<option value="' . $data->id . '">' . $data->description . '</option>' by your results.​

 

 â€‹Is that what you asked? :)​

 

 

 


toxide
  • profile picture
  • Member

Posted 10 September 2015 - 06:58 AM

Hi ,

 

Yeah that's what i was unsure of thanks for your help its been great.

 

Much appreciated :).