Project:Embedded Media Field
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Souvent22
Status:closed (fixed)

Issue Summary

The Fatal error of:
Cannot use string offset as an array
Still persists in ncck. Copying the temporary solution from the video_cck thread:
I'm not sure my method is ok, but the problem was with that line :

$node->$field_name[$delta]['data'] = $data;

I fixed it using an intermediary variable ($nodefield) :
    if ($field['multiple']) {
      foreach ($items as $delta => $item) {
        $data = (array)unserialize($items[$delta]['data']);
        $nodefield = $node->$field_name;
        $items[$delta]['data'] = $data;
        $nodefield[$delta]['data'] = $data;
      }
    }

Comments

#1

It seems that some version of PHP do not decouple well; meaning going to from obj. to array, etc.
Example, the following code set throws the string off set error:

      $data = (array)unserialize($items[0]['data']);
      $items[0]['data'] = $data;
      $node->$field_name[0]['data'] = $data;

However, this code does not:
      $data = (array)unserialize($items[0]['data']);
      $items[0]['data'] = $data;
      $temp = $node->$field_name;
      $temp[0]['data'] = $data;
      $node->$field_name = $temp;

Almost exactly the same except I take out the entire array and then place it back as a whole. I haven't found the exact reason why this is yet; but i've tested it a lot, and this seems to be the cause of it.
For reference, I'm using:

PHP 5.2.3
Windows
Apache 2.2
xDebug enabled

#2

Status:active» needs review

Using $node_field is the right approach, that's the way it's done in CCK core. Need to make the same fix in the non-multiple value code and apply it back to the $node object. A patch is attached that I think will do it.

AttachmentSize
ncck.module.patch 1.09 KB

#3

This patch is working perfectly
Tks a lot

#4

Project:» Embedded Media Field
Version:5.x-1.x-dev» 5.x-1.x-dev

I'll move this to the new project.

#5

Rolled a new patch

AttachmentSize
emfield_fatal_offset_fix.patch 1.07 KB

#6

Maybe the patch was not quite right, i had to add an } on line 161 and remove one on line 177. Then it worked fine. But i was patching manually so maybe i made the mistake.

#7

I was experiencing this issue, and the patch you created seemed to fix it. Strange issue. Anyone know why this was happening? Probably has to do with PHP casting the values incorrectly or something.

#8

My approach took 1 less line but I came to the same result. I don't know if there are any performance implications (I assume it would be negligible).

        $node_field_name = &$node->$field_name;
        $node_field_name[$delta]['data'] = $data;

This is caused by a change in PHP5 http://www.php.net/manual/en/migration5.incompatible.php

According to PHP.net it is a "bad habit" that will no longer be supported.

#9

I misspoke, It is the same amount of code but it seems to me that mine should be correct as the original code would not modify the $node->field_name, but rather, a copy of it.

#10

Same error on PHP5 with this:

$data = (array)unserialize($items[0]['data']);
$items[0]['data'] = $data;
$node->$field_name[0]['data'] = $data;

However, I just wrapped field_name in curly brackets and it works:

$node->{$field_name}[$delta]['data'] = $data;

#11

Boris,
Your fix worked for me.
Thanks!
Heather

#12

it seemed to work fine for me when going into the original emfield.module file and puting "{" before and after $field_name.

#13

Thanks for the work! Sorry I was unavailable the last few weeks -- I was moving out of state. I'll take a look at the patches and roll them in by early next week. (Still unpacking boxes this weekend.)

It must be a PHP5 thing -- I don't experience this in PHP4. I don't have PHP5 installed locally yet, and don't plan to until I start helping with D7 development. From the thread, though, it looks like this is a sounder solution anyway. Meanwhile, once I make sure the patch doesn't break in PHP4, will one of you be willing to test in PHP5 once the patch is in just to make sure?

Thanks,
Aaron

#14

I can confirm that the code Boris provided works with PHP5. Thanks!

#15

thank you for this patch

#16

Status:needs review» fixed

Thanks for the help with this! I don't have php5 installed yet, but this all works w/ php4. I just put the curly brackets around $node->{$field_name} as Boris suggested in the two lines they appeared. After the repository synchs, could someone using php5 confirm this release works? I'll make sure to use this coding standard in the future.

Thanks,
Aaron Winborn

#17

i was having an error that was different. When trying to view a node that had the video field I would get a blank screen.
the log would show this entry:
include_once(./themes/engines/phptemplate/phptemplate.engine) [function.include-once]: failed to open stream: No such file or directory in /Users/ddp/Sites/drupal/includes/theme.inc on line 78.

Boris, this fixed my error too.

Drupal 5.1, PHP 5.22, CCK 1.5

#18

Status:fixed» closed (fixed)
nobody click here