I was getting an error page when my site's users tried to download files from nodes. The watchdog table showed three errors:

PDOException: SQLSTATE23000: Integrity constraint violation: 1048 Column 'id' cannot be null: INSERT INTO {download_count} (fid, uid, type, id, ip_address, referrer, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => 52 [:db_insert_placeholder_1] => 32 [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 192.168.0.1 [:db_insert_placeholder_5] => http://example.com/list [:db_insert_placeholder_6] => 1473690039 ) in download_count_file_download_access_alter() (line 196 of .../download_count/download_count.module).

Notice: Array to string conversion in download_count_file_download_access_alter() (line 191 of .../download_count/download_count.module).

Notice: Undefined property: stdClass::$Array in download_count_file_download_access_alter() (line 191 of .../download_count/download_count.module)

The code causing the problem is in the download_count_file_download_access_alter():

  $id = db_insert('download_count')
  ->fields(array(
    'fid' => $file['fid'],
    'uid' => $user->uid,
    'type' => $entity_type,
    'id' => $entity->$entity_info['entity keys']['id'],
    'ip_address' => $ip,
    'referrer' => $referrer,
    'timestamp' => $time,
  ))
  ->execute();

Line 191 is 'id' => $entity->$entity_info['entity keys']['id'],.

Through debugging, I've found the id is NULL, although my $entity is a proper node object, and $entity_info['entity keys']['id'] returns 'nid'.

Adding curly brackets around the property name in line 191 as 'id' => $entity->{$entity_info['entity keys']['id']}, solves this problem.

I'll create a patch shortly.

CommentFileSizeAuthor
#2 fix-entity-id-property-2798767-2.patch528 byteskylesmith

Comments

kylesmith created an issue. See original summary.

kylesmith’s picture

Status: Active » Needs review
StatusFileSize
new528 bytes

Here is my patch.

erikhopp’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +PHP 7.0 (duplicate)

I just ran into this too and I've applied the patch and can confirm that it fixes the issue. This showed up due to a changes in variable handling in PHP 7. Read more here: https://secure.php.net/manual/en/migration70.incompatible.php#migration7...

woutk’s picture

There's a very similar PHP 7 syntax error in includes/download_count.field.inc, line 31:

      $item['downloads'] = db_query("SELECT COUNT(fid) from {download_count} where fid = :fid AND type = :type AND id = :id", array(':fid' => $item['fid'], ':type' => $entity_type, ':id' => $entity->$entity_info['entity keys']['id']))->fetchField();

After I'd switched my website to PHP 7 last week, this syntax error started generating two watchdog notices whenever someone downloaded a file: "Array to string conversion" and "Undefined property: stdClass::$Array".
So I did not get any PDOException - apparently my website has a different call path for downloads.
Anyway, the fix turned out to be very similar: since I surrounded $entity_info['entity keys']['id'] with braces, I haven't seen any more notices:
$item['downloads'] = db_query("SELECT COUNT(fid) from {download_count} where fid = :fid AND type = :type AND id = :id", array(':fid' => $item['fid'], ':type' => $entity_type, ':id' => $entity->{$entity_info['entity keys']['id']}))->fetchField();
So can someone create a patch for this? I'm new here and don't have the time right now to figure out the exact patch creation process. Thanks in advance.

idefix6’s picture

Patch in #2 works fine.
Any change to get this commited and a new version released?

tomasbedrich’s picture

Just confirming, the patch #2 is working.

bshensky’s picture

Likewise, Patch #2 works here on PHP 7.1

gold’s picture

Bumping this with another +1.

This patch fixes the issue I was seeing too.

gold’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
Related issues: +#2814107: Download count not working with PHP 7

Hmm... This issue was also spotted in #2814107. However, that one also spotted the same sort of issue in another spot. The patch there includes this fix and one other.

Closing this as a duplicate.