⚠ 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

Set fields in the junction table.



dlaynes
  • profile picture
  • Member

Posted 02 May 2018 - 03:51 AM

Hi, I recently bought the Enterprise version of GroceryCrud.
I was wondering if it is possible to set extra fields in the junction table of ->setRelationNtoN() when creating or updating related records.

 

For example:
Laravel stores the model class name in the intermediary table when using Polymorphic relations (for example "App/User").

This could be solved by setting a default value in the database field. However, if the programmer wants to use multiple models, then setting a default value is not a viable option.


Thanks.


dlaynes
  • profile picture
  • Member

Posted 03 May 2018 - 14:51 PM

Hi, perhaps I could solve this issue creating custom ModelState classes, in order to modify the insert and update actions, in order to add the desired manipulation of the $relationNtoNfields variable through the $stateParameter variable. Any tips on how to do this? Thank you.
 

$operationResponse = $this->stateOperationWithCallbacks($stateParameters, 'Insert', function ($stateParameters) use ($relationNtoNData) {
$model = $this->gCrud->getModel();
$insertResult = $model->insert($stateParameters->data);
$stateParameters->insertId = $insertResult->insertId;

$relationNtoNfields = $this->gCrud->getRelationNtoN();

foreach ($relationNtoNData as $fieldName => $relationData) {
$model->insertRelationManytoMany($relationNtoNfields[$fieldName], $relationData, $stateParameters->insertId);
}

return $stateParameters;
});