Tschallacka
Tschallacka

Say you have a database table that has a different primary key than the standard id.

Let's say it's named customer_number

To make sure everything works as usual for this you have 3 locations to keep in mind for the primary key to function properly:

The model The config_form.yaml The config_list.yaml

In the model you need to add the variable $primaryKey

/**
  * Customer Model
  */
class Customer extends Model {
    /**
    * @var string The database table used by the model.
    */
    public $table = 'customers';
    public $primaryKey = 'customer_number';// Note the uppercase K
    .........

In config_list.yaml you have the variable recordUrl: that needs changing. Default this has :id appended behind it. You need to change :id to your primary key name

recordUrl: author_name/client/customeredit/update/:customer_number

in config_form.yaml you have the variable redirect: under create: There too you need to change out :id for your primary key name

redirect: author_name/client/customeredit/update/:customer_number

And now you should be good to go with your different primary key.

Last updated

jfo
jfo

@Tschallacka Thanks a lot dude! Saved my day! ;-)

Vijay Wilson
Vijay Wilson

Thanks a lot..!

reallifedigital
reallifedigital

tl;dr;

Are you using a non-numeric primary key and having issues with redirect failing when saving a new record? Then simply add this line to your model:

public $incrementing = false;


Thanks @Tschallacka for the pointer for setting the $primaryKey and yaml file updates.

For anyone else having an issue with using a non-numeric ID (in my case a 'type' string as a primary key) when a record is created for the first time the redirect URL will fail. This is because the Illuminate\Database\Eloquent\Model class (which is inherited by October\Rain\Database\Model) sets $incrementing to true. This will break during the Eloquent class's call to performInsert() (and in turn insertAndSetId()) which is expecting an incrementing column in the database. This causes the primary key field to be set to 0 which then causes an issue when the path is looked up for the form redirect on save.

If you add the following to your Model class:

public $incrementing = false;

then you'll be able to get the $model object correctly returned (as provided in the form, and no longer being borked by the auto-incrementing expectation of the primary key) ready for when Backend\Behaviors\FormController::create_onSave() fires off its redirect.

Last updated

bonniss26
bonniss26

reallifedigital said:

tl;dr;

Are you using a non-numeric primary key and having issues with redirect failing when saving a new record? Then simply add this line to your model:

public $incrementing = false;


Thanks @Tschallacka for the pointer for setting the $primaryKey and yaml file updates.

For anyone else having an issue with using a non-numeric ID (in my case a 'type' string as a primary key) when a record is created for the first time the redirect URL will fail. This is because the Illuminate\Database\Eloquent\Model class (which is inherited by October\Rain\Database\Model) sets $incrementing to true. This will break during the Eloquent class's call to performInsert() (and in turn insertAndSetId()) which is expecting an incrementing column in the database. This causes the primary key field to be set to 0 which then causes an issue when the path is looked up for the form redirect on save.

If you add the following to your Model class:

public $incrementing = false;

then you'll be able to get the $model object correctly returned (as provided in the form, and no longer being borked by the auto-incrementing expectation of the primary key) ready for when Backend\Behaviors\FormController::create_onSave() fires off its redirect.

This fix my issue of attaching an image to a model that has non-auto-incremented PK. Thank you.

1-5 of 5