open-admin icon indicating copy to clipboard operation
open-admin copied to clipboard

json two-dimensional array not updating previous records

Open kanishka55 opened this issue 1 year ago • 4 comments

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();
        });
    }

Screenshot (56)

System

  • Open-admin version 1.0.27
  • PHP version 8.1.13
  • Laravel Version 9.35.1
  • OS: Windows
  • Browser chrome

kanishka55 avatar May 17 '23 10:05 kanishka55

@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.

open-admin-org avatar May 19 '23 09:05 open-admin-org

Hi, could you please add some steps on how to replicate this?

SachinBahukhandi avatar May 23 '23 11:05 SachinBahukhandi

I'm still looking into this

kanishka55 avatar May 26 '23 09:05 kanishka55

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;
    }

bytebrain avatar Aug 03 '23 12:08 bytebrain