⚠ 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

workaround conflict hidden field and set_relation()



semsik
  • profile picture
  • Member

Posted 08 November 2016 - 20:06 PM

Well I'm still kinda new to GC as this is my second post.

I've been developping at a speed I have never developped before. (I'm just a hobbyist programmer)

 

After getting to know the API i found that there where things I could not do with the API, luckely there is the forum and I found solutions/workarounds/hacks for most of my issues.

 

There was one thing I saw asked a lot in the forum that got answers but no solution/workaround (I have not read the entire forum (yet!!) so excuse me if the workaround was already demonstrated)

 

Maybe the solution is obvious to most but for me it took a while to figure it out.

 

The Issue:

You have a table that has a user_added field and in this field you store the id of the user who added the record.

In some cases you want to display the name of the user who added the record in the list view.

 

Well then you need to use set_relation function to get the name of the user who added the record from the users table for displaying in the list view.

$crud->set_relation('user_added','users','username');

You also want to hide the field user_added on the add form because you want to use session data to set it to the logged in user.

$crud->field_type('user_added','hidden',$this->session->id_user_added);

The above conflicts in GC, resulting in showing a textbox for the user_added field on the add form

 

To get around this limitation I just added this in the controller.

if($this->uri->segment(3)!='add'){
    $crud->set_relation('user_added','users','username');
}

No idea why it works, but I'm guessing that this way the set_relation function only gets executed when not in add view, meaning no conflict with the hidden field on the add form.

 

(Possibly u need to adjust the segment number, for the edit form I exclude the user_added field with edit_fields function)

 

using:

codeigniter 3.+

GC 1.5.7

Bootstrap theme v3 (totaly worth buying)

 

 


semsik
  • profile picture
  • Member

Posted 03 December 2016 - 14:19 PM

i actually changed my approach a bit on this one

 

after class before first function i now declare a property

public $uries;

in __construct function i now have

//convert uri to segment with codeigniters segment_array() function ->store it in $uries property
$this->uries = $this->uri->segment_array();

in the crud code i now have

if(!in_array('add',$this->uries)){
    $crud->set_relation('id','table','field');
}

this way no conflict with hidden field on add form and list view set relation

 

greetz

 

Semsik

using:

codeigniter 3.+

GC 1.5.7

Bootstrap theme v3 (totaly worth buying)