⚠ 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

Problem with a callback function



Jaïs Pingouroux

Jaïs Pingouroux
  • profile picture
  • Member

Posted 19 December 2013 - 11:17 AM

Hi all,
 
I'm facing a problem with a CRUD, and hope someone will be able to help.
I've build a CRUD with several relations, all of them are working properly.
The CRUD list display the first and last name of the user who has added the record through a relation with the users table.
 
        $crud->set_table('vivier_retour_exp');
        $crud->where('id_vivier_prestataire', $this->idPrestataire);
        $crud->set_relation('id_vivier_segment', 'vivier_segment', 'label');
        $crud->set_relation('id_user', 'users', '{prenom_user} {nom_user}');
 
However, upon adding or editing a new record, I want the user field value to be automatically filled with the actual user's ID. I therefore removed the "user" field from add and edit operations : 
 
        $crud->unset_add_fields('id_user');
        $crud->unset_edit_fields('id_user');
 
I though I could use a callback before insert to set this value :   
        $crud->callback_before_insert(array($this,'callback_feedbacks_update'));
        $crud->callback_before_update(array($this,'callback_feedbacks_update'));
 
function callback_feedbacks_update($post_array, $primary_key) {
        $post_array['id_user']=$this->user->id_user;
        return $post_array;
    }
 
However, this is not working and prevent any record from being added or edited.
 
I have also tryied these other options :
- use a callback after and update my DB table using the primary key passed by the callback -> same result
- change the type of 'id_user' field -> not working as the field comes from a relation (known issue)
 
Can anyone see a reason why the callback function before or after update/insert is not working, or suggest an alternative solution?
 
Thank you very much for your assistance!

 


edramirez

edramirez
  • profile picture
  • Member

Posted 19 December 2013 - 13:07 PM

Hi, Jais -

 

You need to delete this line of code:

$crud->set_relation('id_user', 'users', '{prenom_user} {nom_user}');

 

Let us know if this works. If this doesn't, you will need to display the entire code in that particular controller so that we could see what your code is trying to do.

 

Regards,

 

Ed

 

P.S.

 

Don't forget to click the Like button.


Jaïs Pingouroux

Jaïs Pingouroux
  • profile picture
  • Member

Posted 19 December 2013 - 13:28 PM

Dear Ed,

 

Thanks for your reply.

Unfortunately, this did not solve my problem.

Also, I need to keep this relation as I want to display the first and last name of the user in the list view, and not the user id.


Jaïs Pingouroux

Jaïs Pingouroux
  • profile picture
  • Member

Posted 19 December 2013 - 14:16 PM

Ok now I'm feeling really shameful...

 

The problem came from a unique index on the id_user field... Obviously, the callback was prevented by this restriction.

 

SOOOO sorry...