open-admin-org / open-admin

open-admin forked from z-song/laravel-ladmin. Removing jquery, now based on Bootstrap5, vanilla JS
https://open-admin.org
MIT License
244 stars 71 forks source link

json two-dimensional array not updating previous records #66

Open kanishka55 opened 1 year ago

kanishka55 commented 1 year ago

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-org commented 1 year ago

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

SachinBahukhandi commented 1 year ago

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

kanishka55 commented 1 year ago

I'm still looking into this

bytebrain commented 1 year ago

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