Hi,

I have a filefield in one of my content types with up to 3 allowed file uploads, but not required. The filefield is called fattachment and the mysql table is content_field_fattachment

I have noticed that if I upload only one file and not 3, there are still three rows inserted in the mysql table, here is an example:
I uploaded one file, and here is the row in the table:
1114 1114 0 1257 1 a:1:{s:11:"description";s:0:"";}

but, since I have set the filefield to up to 3 files, I have also two other rows created:
1114 1114 1 NULL NULL NULL
1114 1114 2 NULL NULL NULL

Their fid is NULL, since there is no file, but the problem arises when I'm using Views, the filefield widget lists all these files with an icon for each.
Some of my content types allow upload of up to 5 files, isn't it going to make a lot of NULL rows in the table? How can this be fixed? Thanks
Gafir

CommentFileSizeAuthor
#2 filefield_empty_formatter.patch978 bytesquicksketch

Comments

quicksketch’s picture

Priority: Critical » Normal

The mechanism of storing exactly the number of rows as specified is intentional. This functionality is provided by CCK and is the same for all field types.

Check the CCK "content_set_empty()" function in content.module:

* On order to keep marker rows in the database, the function ensures
* that the right number of 'all columns NULL' values is kept.

So the database storing NULL columns is by design.

when I'm using Views, the filefield widget lists all these files with an icon for each.

But this still sounds like a problem. Are you saying that you get 3 files listed within Views for each node, even when the rows are NULL?

quicksketch’s picture

Title: mysql insert for non-used fields » NULL values inserted when using multiple values
StatusFileSize
new978 bytes

I'm pretty sure checking the "Require this relationship" corrects this problem for the most part. This effectively switches the JOIN to an INNER instead of LEFT JOIN, so NULL values are excluded from the list.

However, it's a bit odd that the file icon is printed out even when there isn't a file there at all. To fix this, this patch returns an empty string if the file doesn't even exist.

There's also a request to add a filter rather than needing to add the relationship, but I think the relationship is the better approach honestly. See #372241: Make a "has file" Views Filter.

quicksketch’s picture

Status: Active » Needs review
easp’s picture

I had a similar problem where file icons would appear in views when no file was uploaded. Patch worked fine for me and corrected the problem.

quicksketch’s picture

Status: Needs review » Fixed

Thanks easp. I've committed this patch.

gafir777, to prevent the problem of these rows showing up at all, use a relationship like I described in #2.

gafir777’s picture

Thank you guys for your help, i'll try that!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

giorgosk’s picture

I have the feeling that checking for the existence of a file is needed for the default formatter as well

at least it was needed in my case

function theme_filefield_formatter_default($element) {
  $file = $element['#item'];
  if (empty($element['#item'])) {
    return '';
  }
  $field = content_fields($element['#field_name']);
  $output = theme('filefield_item', $file, $field);
  return $output;
}

giorgosk’s picture

Desregard previous message
the committed patch works fine ...

the problem was created by an overridden theme function that I had forgotten about...