Skip to content

LocationModel behavior incompatible with $guarded fields #52

@voveson

Description

@voveson

After a debugging session, I figured out why my model was not being updated correctly. In my model, I had not defined a $fillable array. I generally opt for setting $guarded fields in stead:

protected $guarded = [];

It turns out that when your model implements the LocationModel behavior, that sets an array of fillable properties, which is not compatible with the above. I'd propose the following fix in the LocationModel constructor:

/**
 * Constructor
 */
public function __construct($model)
{
    parent::__construct($model);

    if (!empty($model->getFillable())) {
        $model->addFillable([
            'country',
            'country_id',
            'country_code',
            'state',
            'state_id',
            'state_code'
        ]);
    }

    $model->belongsTo['country'] = ['RainLab\Location\Models\Country'];
    $model->belongsTo['state']   = ['RainLab\Location\Models\State'];
}

This way, if the model does define $fillable properties, the location properties are added as fillable. If not, they would be fillable by default unless the developer explicitly blacklists them using the $guarded array.

I have created a PR (#53) to implement the above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions