open-admin
open-admin copied to clipboard
json two-dimensional array not updating previous records
Describe the bug when i go to edit action and updating json field. t saves new record if i add. and does not get my previous record after edited.
below you can see my code.
$form->table('titledata', function ($table) {
$table->text('title');
$table->textarea('description');
});
protected $casts = [
'titledata' => 'json',
];
public function getColumnNameAttribute($value)
{
return array_values(json_decode($value, true) ?: []);
}
public function setColumnNameAttribute($value)
{
$this->attributes['titledata'] = json_encode(array_values($value));
}
}
public function up()
{
Schema::create('name', function (Blueprint $table) {
$table->increments('id');
$table->json('small_description')->nullable();
$table->json('videolink')->nullable();
$table->json('titledata')->nullable();
$table->timestamps();
});
}
System
- Open-admin version 1.0.27
- PHP version 8.1.13
- Laravel Version 9.35.1
- OS: Windows
- Browser chrome
@kanishka55 For reporting, this is indeed a bug. We'll look into it. If you find fix yourself in the, please share with a pull request.
Hi, could you please add some steps on how to replicate this?
I'm still looking into this
I have almost the same problem. I get an exception on submit:
ErrorException In EmbeddedForm.php line 162 :
foreach() argument must be of type array|object, bool given
After some debugging, I found that when the prepare()
method in open-admin-org/open-admin/src/Form/EmbeddedForm.php
checks if it gets an array or not, the problem is fixed.
/**
* Prepare for insert or update.
*
* @param array $input
*
* @return mixed
*/
public function prepare($input)
{
if (is_array($input)) { // this fixes the bug
foreach ($input as $key => $record) {
$this->setFieldOriginalValue($key);
$input[$key] = $this->prepareValue($key, $record);
}
}
return $input;
}