Jump to content


pass variable into add form and insert in a different table


  • Please log in to reply
2 replies to this topic

#1 molay

molay

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 06 January 2019 - 12:49 AM

Hi Experts,

i'm new in GC.

i have a view that gives me the follwing info from table (A) :

id , customer , amount , balance 

i have added an add_action to perform a transaction for a selected customer. 

example for customer with id=1 :

when clicking on the "perform transaction" button i do see the action /xxx/transaction/add/?id=1

 

the action should insert data in a different table (B) 

So i get a new form to add a transaction. however  i'm not able to assign the id=1 to that transaction ..

in other terms i'm unable to pass the variable id with value 1 to the new form and insert a new transaction for customer id =1 

 

Any help will be appreciated.

Thanks

 



#2 NadimD

NadimD

    Member

  • Members
  • PipPip
  • 13 posts

Posted 18 January 2019 - 11:37 PM

Hello there !

 

Did you try to add a callback to your CRUD, to force the id ?

 

Example below (for Free Edition) : https://www.groceryc...k_before_insert

$crud->callback_before_insert(array($this,'encrypt_password_callback'));

// ...

function set_id_before_insert($post_array) {
	$post_array['id'] = $your_id; 
	return $post_array;
}

Example below (for Entreprise Edition) :

$crud->callbackBeforeInsert(function ($stateParameters) {
	$stateParameters->data['id'] = $your_id; // Here you put whatever variable you use to get the id
	return $stateParameters;
});

Replace $your_id with your actual desired id. If you can't pass the variable into the function, set it in the class as a private variable like :

private $insert_id;

In your controller, get the id from the URL and set the variable like this :

$this->insert_id = $_GET['id']; // Or your actual variable

And get it in the function with :

$this->insert_id

// OR a create a getter method if you need to process the id before getting it


#3 janeflowers

janeflowers

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 25 September 2019 - 03:18 AM



 

Hello there !

 

Did you try to add a callback to your CRUD, to force the id ?

 

Example below (for Free Edition) : https://www.groceryc...k_before_insert

$crud->callback_before_insert(array($this,'encrypt_password_callback'));

// ...

function set_id_before_insert($post_array) {
	$post_array['id'] = $your_id; 
	return $post_array;
}

Example below (for Entreprise Edition) :

$crud->callbackBeforeInsert(function ($stateParameters) {
	$stateParameters->data['id'] = $your_id; // Here you put whatever variable you use to get the id
	return $stateParameters;
});

Replace $your_id with your actual desired id. If you can't pass the variable into the function, set it in the class as a private variable like : google street view

private $insert_id;

In your controller, get the id from the URL and set the variable like this :

$this->insert_id = $_GET['id']; // Or your actual variable

And get it in the function with :

$this->insert_id

// OR a create a getter method if you need to process the id before getting it

 

Thank you. Now I understand more.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users