Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] WithFiles inside repeatable throws error on update #5671

Open
eleven59 opened this issue Sep 20, 2024 · 2 comments
Open

[Bug] WithFiles inside repeatable throws error on update #5671

eleven59 opened this issue Sep 20, 2024 · 2 comments
Labels

Comments

@eleven59
Copy link

Bug report

What I did

I keep getting errors when using a subfield that has a "withfiles" configuration. This is the setup:

  1. There is a model with its "pdfs" attribute as a repeatable field in the Crud model
  2. The repeatable field has two subfields: title (text field), and file (upload field, which has the withFiles config)
  3. The model has an additional "links" attribute, which is a repeatable field with only a text and url field
  4. Both the pdfs and links attributes are cast as array on the parent model

When I create a new entry, all is fine, regardless of whether I add entries to the pdf repeatable element.
When I then go to edit that entry, regardless of whether I add, remove, or leave empty the pdf repeatable field, on save, I get the following error:

array_map(): Argument #2 ($array) must be of type array, string given

I also noticed that after the first save, if I leave both repeatable fields empty, the "pdfs" field is saved as "[]" in the database, whereas the links field is NULL, which already seemed like a weird difference.

Now, in src/app/Library/Uploaders/Support/Traits/HandleRepeatableUploads.php on line 153, I found the following code which causes the error:

$values = $entry->{$this->getRepeatableContainerName()};
$values = is_string($values) ? json_decode($values, true) : $values;
$values = array_map(function ($item) use ($repeatableUploaders) {
    foreach ($repeatableUploaders as $upload) {
        $item[$upload->getAttributeName()] = $this->getValuesWithPathStripped($item, $upload);
    }

    return $item;
 }, $values);

If $values is null to begin with, then this throws an error because it never because it never becomes the array that array_map expects it to be. An easy fix is to change line 154 to:

$values = is_string($values) ? json_decode($values, true) : (array)$values;

But this may cause other issues, I'm not sure. I also expected this error would already be impossible because the attribute is cast as an array on the parent model, but apparently not for some reason. Not sure if there is any code related to repeatable fields or uploads within that prevents the model casts to work?

What I expected to happen

Be able to use withfiles inside repeatable element

What happened

I got an error on update

What I've already tried to fix it

I can confirm that when I remove the "withFiles" field from the equation for the PDFs field as well, behavior reverts to be the same as the links field and no more errors are thrown.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
8.3.11

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dba, dom, hash, FFI, fileinfo, filter, ftp, gd, gettext, gmp, json, iconv, SPL, session, standard, mbstring, igbinary, imagick, imap, intl, ldap, exif, mongodb, mysqlnd, mysqli, pcntl, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, random, readline, redis, Reflection, shmop, SimpleXML, soap, sockets, sodium, sysvmsg, sysvsem, sysvshm, tokenizer, xml, xmlreader, xmlwriter, xsl, zip, zstd, Zend OPcache

### LARAVEL VERSION:
11.23.5.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.6
backpack/crud: 6.7.37
backpack/generators: v4.0.6
backpack/pro: 2.2.17
backpack/settings: 3.1.1
backpack/theme-tabler: 1.2.13
@cod3rshotout
Copy link

Hi @eleven59 did you found a solution? I'm facing the same issue. Any update guys? @tabacitu @pxpm

@eleven59
Copy link
Author

Hi @cod3rshotout! Not really... For now I'm just overwriting the HandleRepeatableUploads trait locally. I just copied the whole file and have changed line 154 as described in my original post. This hasn't caused any other issues so far, but as I also said in the original post: it may lead to other issues in other configurations than in my specific use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants