⚠ 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

beginner question



richb201
  • profile picture
  • Member

Posted 06 December 2017 - 23:01 PM

I have a function where I am allowing a user to enter a business component. Here is the code:

$crud = new grocery_CRUD();

$crud->set_table('business_components');
$crud->set_subject('Business Component');
$crud->set_relation('email','admin','email');
$crud->fields('bus_comp','email');
$crud->change_field_type('email','invisible');

$crud->columns('bus_comp');

$crud->order_by('bus_comp','asc');
// $crud->add_fields(array('bus_comp'));
// $crud->edit_fields(array('bus_comp'));

$crud->callback_before_insert(array($this,'add_email'));
$output = $crud->render();

$this->_configure_output($output);

}catch(Exception $e){
show_error($e->getMessage().' --- '.$e->getTraceAsString());
}
}

function add_email($post_array){
// $Data = base64_decode( urldecode( $this->session->userdata('userid') ) );
$post_array['email']=base64_decode( urldecode( $this->session->userdata('userid') ) );
return $post_array;
}

 

I am having a user enter a business component but I am also filling in their email address in that same record. The callback is to allow me to set their email address, prior to writing to the database. 

 

My problem is that I added add_fields and edit_fields to make sure that only the business component field displays in the add screen. But when I do this, the email address in the database record is blank. When I uncomment add_fields and edit_fields, the email address gets properly added, but the view shows a place for email address. I have attached a screen shot of the two ways the view appears.  I don't want to show an input field for the email address because I am hard coding that in my callback. 

 

Any help on this newby problem is appreciated. 

 

 


Amit Shah
  • profile picture
  • Member

Posted 07 December 2017 - 04:04 AM

Well that is a simple reason is that when u set the add / edit fields, you specify which fields you want to play around with. In such scenario, based on the add / edit fields, it creates the insert statement. If you don't specify the add / edit fields, it will pick up the fields on its own. 

So if u want the field (email) to be available during insert, u need to set it in the add_fields for sure. What you can do is set the type of field to be hidden and that should solve your purpose.

 

Happy GCing :)


richb201
  • profile picture
  • Member

Posted 07 December 2017 - 09:13 AM

Thanks. Please notice that I have 

 

$crud->change_field_type('email','invisible');

 

I thought that would hide the field. Apparently it does not!  Is there a different function I should be using?

 

Also, I want to limit the display to only records where email=$myEmailAddress. How would I do this within GCrud?

 

thx


Amit Shah
  • profile picture
  • Member

Posted 07 December 2017 - 12:51 PM

GC have a where clause - its similar to the ActiveRecord where clause of Codeigniter. There you can specify your criteria

 

Well - u can set it to be hidden - that will be fine to do with. But one thing, you need to put it in add / edit fields list .. else it will not recognize it as a contender while building the insert / update statement.

 

Happy GCing :)


richb201
  • profile picture
  • Member

Posted 07 December 2017 - 16:52 PM

Thanks. I think the issue is that although I am using $crud->callback_before_insert(array($this,'add_email')) , only the business component (first field)is being inserted, not both fields in the $post_array.

 

function add_email($post_array){

// $Data = base64_decode( urldecode( $this->session->userdata('userid') ) );
$post_array['email']=base64_decode( urldecode( $this->session->userdata('userid') ) );
return $post_array;
}

 

I am returning the $post_array correctly to have both the buisness component and the email. But Grocery_CRUD.php line 962 is only adding one field, not both fields. 

 

962   $insert_data[$field->field_name] = $post_data[$field->field_name];

 

Any idea how to get it to add both fields in the $post_array? Or perhaps i shouldn't be using post_array?


richb201
  • profile picture
  • Member

Posted 07 December 2017 - 21:51 PM

Well here is what I have discovered. I am thinking this is a bug, unless someone can tell me that GC is meant to work this way. If I have add_fields() in my code then the additional field will not be written. Why? I have no idea? Is this an application programming error? I don't know. edit_fields() works fine and doesn't seem to affect the write.  


Amit Shah
  • profile picture
  • Member

Posted 08 December 2017 - 04:13 AM

Well .. when u set the add_fields, GC being generic library set, it will pickup the fields it is set to play around with. When u specify add_fields, it tells the library, you want to play around with those fields only. Hence, it will not only show those limited set of fields, but also build the insert statement around the same. 

 

If you don't set anything with add / edit fields, it will pickup every field in the table. That is how the library works. There are high possibilities, that in post array in callback, you may add additional fields that might not exists and it can create un-necessary breakdown.

 

Edit - anyways, callback before insert will never be called. I don't see it as a bug. It is rather a functional set of generic library and not personal library.


richb201
  • profile picture
  • Member

Posted 08 December 2017 - 13:59 PM

Thank you for the response. Now it seems clear, GC will only add the fields it displays. So if I only have one field displayed, it will only add that one field. So what do I do? Can I get the record number back for the newly added record and in post array callback do a separate, codeigniter-based write to that record? Does anyone have any example code of this type of thing? Basically I need to modify a field that GC is not displaying. 

 

OR

 

can I display the other field (email address in this case) but prepopulate it and not allow the user to change it? 


Amit Shah
  • profile picture
  • Member

Posted 09 December 2017 - 16:05 PM

Well, think you missed the same shared above, you can add the field in add_field / edit_field .. and change the type to be hidden. That way, the field will be available during add / edit but wont be touched by the user filling in the form. And you can manipulate the same with the email add in callback_before_insert ... / update .. that should do the trick for you.

 

As if you want to understand the id of the newly pushed record - you can get the same in callback_after_insert ... where you can play around with the same as you like.

 

Happy GCIng :)