⚠ 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

pass variable into add form and insert in a different table



molay
  • profile picture
  • Member

Posted 06 January 2019 - 00: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

 


NadimD
  • profile picture
  • Member

Posted 18 January 2019 - 23: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.grocerycrud.com/documentation/options_functions/callback_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

janeflowers
  • profile picture
  • Member

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.grocerycrud.com/documentation/options_functions/callback_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.