CCK only saving first character

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
Fixdit's picture

I am inserting some data into nodes via a Flash application using SWX PHP (It's very like AMFPHP). All works well apart from the CCK fields. I'm having a massive problem trying to insert more than one character (the first character) into any CCK textfield. I believe this is because I haven't got the right formatting in my Flash AS2 code, but feel like I've tried every combination of Objects and Arrays to get it to work... to no avail :(

What should the format be? All the other normal node fields insert fine...

Example node structure snippet printed from Services module of the 'questionnaire' node:

stdClass Object
(
[nid] => 419
[type] => questionnaire
[field_CCKFIELDNAME] => Array
        (
            [0] => Array
                (
                    [value] => 2
                )

        )
)

AS2 code snippet from Flash file:

      nodeobj = {
            nid:0,
         type:'questionnaire',
            field_CCKFIELDNAME:Array[0] = Array(String(CCKFIELDNAME_INPUT)),
           };

        var callCreateNode:Object = {
          serviceClass:"Drupal",
           method:"callService",
            args:['node.save', [sessionid, nodeobj]],
            result:[this,confirmCreateHandler],
            timeout:[this, timeoutHandler],
            fault:[this, faultHandler]
         };
        
       swx.call(callCreateNode);

Pleeeeeaase heeeelp! Many thanks,

Comments

Instead

waldmanm's picture

Instead of:
field_CCKFIELDNAME:Array[0] = Array(String(CCKFIELDNAME_INPUT)),

Try:
field_CCKFIELDNAME = [{value:String(CCKFIELDNAME_INPUT)}],

You need to set the 'value' property within the field_CCKFIELDNAME array's first item to the actual CCK field value. Lots of complexity for a simple field assignment ... :(

You could be my saviour!

Fixdit's picture

You could be my saviour! However it's not accepting that at the moment.. it shoots back an error. :(

Tried this and got no error

Fixdit's picture

Tried this and got no error but no input turning up either... I feel close to solving it... any thoughts?

nodeobj.field_name = new Array();
nodeobj.field_name = [{value:"HELLO WORLD"}];

Here's some notes on how I

yajnin's picture

Here's some notes on how I got CCK fields working:

http://drupal.org/node/776122

//

The format you have now appears correct.

nodeobj.field_name = [{value:"HELLO WORLD"}];

If this isn't working, you may want to double check the naming convention of the field.

If you haven't already done so, create a reference node Drupal-side, pull it up with Node.Get, inspect the naming convention, and double check you have the right format.

Also, make sure you're trying

waldmanm's picture

Also, make sure you're trying this with a simple CCK field type, one of the core ones such as Text. I've had a problem once with a more sophisticated field type by a contributed module (though not sure if the problem was with me or the module ... :).

One other thing: If the number of values you defined for your CCK field is 'unlimited' (or even more than 1, but not sure about that), there's an odd way values are represented:
- An array with at least one item is always returned from drupal.
- If there are no values, then the first item's value will be null
- Otherwise, there are items in the array as per the number of values

For example, for an unlimited node reference field:
- No references would be represented as: 'node.field_refs = [{nid:null}]; ' (and NOT as an empty array, as you might expect)
- One reference would be: 'node.field_refs = [{nid:"8"}]; '
- Two would be : 'node.field_refs = [{nid:"8"}, {nid:"14"]; '
etc.

Unfortunately, I doubt either of these is your problem ...

I'm going to go away and run

Fixdit's picture

I'm going to go away and run a few tests using all your great help. Will hopefully be back here with a solution :)

The suggested, and seems

Fixdit's picture

The suggested, and seems agreed formatting still isn't working.. no matter what variation I try to achieve it:

nodeobj.field_name = [{value:"HELLO WORLD"}];

or...
var fieldobj:Object = new Object;
fieldobj = {value:"HELLO WORLD"};
var dataArray = [fieldobj];

nodeobj = {
nid:0,
title:"A node title here",
field_name:dataArray
}

I have checked over the formatting of the node:

[field_name] => Array
        (
            [0] => Array
                (
                    [value] =>
                )

        )
)

Could it be an issue to do with how SWX handles the data? This is very frustrating. All suggestions welcome :) I feel soooo close to the solution...

Managed to find this error

Fixdit's picture

Managed to find this error when using this code: nodeobj.field_name = [{value:"HELLO WORLD"}];

Network Traffic: Data In
ae
<br />
<b>Fatal error</b>:  Cannot use object of type stdClass as array in <b>/home/fixddav7/public_html/myovi/sites/all/modules/cck/text.module</b> on line <b>389</b><br />
0
FLERR: Bad Image Data Error!
ExternalAsset loaded: _level0.SWXLoadManagerClip.holder0

This is an issue of the

yajnin's picture

This is an issue of the Services server you're using. JSON Server module used to have the same issue.

I have notes on this here:

http://drupal.org/node/774116

Specifically:

  // Note: Commented Method for Older JSON Server

  // With earlier JSON parsers the node object needed to be an encoded JSON string
  // // This may apply to other JavaScript servers as well

  // // node_object = JSON.encode(node_object); // // Uncomment this line

  // // Note: JSON.encode() is a MooTools method
  // // Your framework has the equivalent. Or GOOG 'JavaScript serialize' etc

  // // If JSON.encode() employed, 'node_object' is now a plain text string

The problem (I surmise) is with the Drupal method "drupal_to_js()". I had found the node object needed to be serialized as a string, rather than an object. Confused the hell out of me too.

JSON Server has since built in their own JSON parser and the problem was solved.

Note in my code JSON.encode(). This is a MooTools class that converts a struct to a string. You'll need to do this in your code. Either with a similiar method, or manually. For example:

var object = {foo:"bar"};

// should now be:

var object_string = "{foo:\"bar\"}";

annoying hey?

Keep in mind my example is

yajnin's picture

Keep in mind my example is using JSON. Depending on server perhaps you'll need a serialized PHP string.

Cool, thanks so much Ninjay.

Fixdit's picture

Cool, thanks so much Ninjay. I'm dealing in Flash, SWX and PHP. So, tell me if I'm getting this wrong, but I need to look at either parsing the string within Flash or the SWX module's PHP? Going to go and try this out, so any further pointers on how to encode will be much appreciated :)

I'm not a big expert on SWX

waldmanm's picture

I'm not a big expert on SWX vs. AMF (or on Flash, for that matter), but have you tried AMFPHP? I'm using it quite successfully using Flex.

Using SWX due to necessity

Fixdit's picture

Using SWX due to necessity because I'm building in Flash Lite (on mobiles) interfacing with Drupal. SWX works great... apart from CCK... which is REALLY important and appearing to be frustrating. Hah.

These days I'm Mr

yajnin's picture

These days I'm Mr JavaScript... and it's been about 8 years since I really worked with Flash. Missed the Flex boat mostly... so off the top of my head, I have no great tips for you.

A quick GOOG returned some stuff, all which seemed to require reaching out to PHP to do the serialization for you. Here's one example:

http://www.sephiroth.it/test/unserializer/

// ##

If it were me, I'd probably avoid that and just produce my own basic string maker.

Here's an example of serialized PHP:

'a:2:{i:0;s:4:"pleh";i:1;a:2:{i:0;s:4:"pleh";i:1;R:3;}}'

To my non-php-expert-eyes it's a little awkward reading this at first... but if you were to build your object in PHP, serialize it there to see the string you eventually need to produce, you could then produce your string with a little bit of simple string wizardry within Flash.

For example, using the random example I showed in the code above:

var_a = "pleh";

var my_string = "a:2{i:0,s:4:\"" + var_a + "\";i:1;a:2:{i:0;s:4:\""+ var_a +"\";i:1;R:3;}}'\"

Voila, you'll have what you need.

Ideally you'd just find a great Flash class to import that gives you full serialization capability.

But either way this will get you closer to goal.

--- I should really take a

yajnin's picture

--- I should really take a minute to say... if this is for mobile... you may want to seriously consider using JavaScript rather than Flash. Quite honestly, I believe anyone working with Flash should consider replacing it with JavaScript.

Libraries like MooTools and jQuery give you open, standardized, plugin-less Rich Internet Application power that blows Flash away any day.

And working with JSON is a dream. So much that I think I'll name my first born son JSON Jr. (My name is Jason). lol.

As you can see in the code similarities, Actionscript is derived of JavaScript, and so making the switch wouldn't be too difficult.

Anyway, just some Big Picture thoughts. My two cents :)

Made a mistake in my string.

yajnin's picture

Made a mistake in my string. Can't seem to edit this post now (due to a reply I made on it?) so here it is again fixed.

var my_variable = "pleh";

var my_string = "a:2{i:0,s:4:\"" + my_variable + "\";i:1;a:2:{i:0;s:4:\""+ my_variable +"\";i:1;R:3;}}";

I've updated my resources

yajnin's picture

I've updated my resources with more information derived of this thread, should anyone want a more comprehensive overview of the process -- including the serialization issue discussed here.

http://drupal.org/node/774116

Note this is JavaScript oriented. But as mentioned, JS is quite similar to Actionscript and the information linked here should be of use to anyone struggling through.

Great help, if this doesn't

Fixdit's picture

Great help, if this doesn't work I don't know what will... here's hoping :)

Hi Ninjay, I'm just trying to

Fixdit's picture

Hi Ninjay,
I'm just trying to bring all this awesome info together and apply it to my situation.... I'm a little lost at the moment. Just need a little clarification.

Here is what I understand: I embed my variable inside a string formatted to appear as a PHP serialised object. This is then sent through to the PHP and I have to write some PHP code to unserialise it... right?

var my_variable = "pleh";

var my_string = "a:2{i:0,s:4:\"" + my_variable + "\";i:1;a:2:{i:0;s:4:\""+ my_variable +"\";i:1;R:3;}}";

nodeobj = {
nid:0,          
type:'questionnaire',
field_name:[my_string]
}

var callCreateNode:Object = {
   serviceClass:"Drupal",
   method:"callService",
    args:['node.save', [sessionid, nodeobj]],
    result:[this,confirmCreateHandler],
    timeout:[this, timeoutHandler],
    fault:[this, faultHandler]
};
         
swx.call(callCreateNode);

Then find out where the field gets sent and add some unserialising PHP code, or should this just be something sorted on the Flash side? In your example code you are encoding the entire 'node_object'.

node_object = JSON.encode(node_object);

Thanks once again for all the great help,

If this is the problem you

yajnin's picture

If this is the problem you shouldn't need to do anything to unserialize... But that's a tough statement to make as it depends on your server.

In Drupal go to Services / Browse and check out some of the services pages.

You can supply your args using JSON and serialized PHP. I think some of the interfaces allow other formats. I discovered that the interface has a bug, and doesn't take JSON. Thats currently in the issue queue and has been confirmed as a bug. I'm not sure if this also is broken for php, but if not you should be able to paste your string and save. As a test. It should work... And the point is you shouldn't need to do anything more than that.

Theoretically your own app should work the same.

But again -- it is completely up to your chosen server module. It acts as middleman between Services and your app. And they can do anything to your data as it runs through.

Theoretically you shouldn't have to do anything. You should be able to send a full deep object and get the same returned. If (un)serialization is needed, it should happen in the module. You may want to look through the module code and understand what they've done.

As I said, JSON Server once had this issue, but it's since been fixed. If you suspect this is the case with yours I recommend posting questions and your findings in the issue queue of that module.

Good luck! Let me know how it unfolds.

I looked more closely. It's

yajnin's picture

I looked more closely. It's actually your entire node object that you may need to serialize. Not just your cck field.

Also note the "pleh" example was just a random example of a php string.

You'll need to create your node object in the fashion we prescribed in the first few posts, and then serialize that.

With the Drupal module it

Fixdit's picture

With the Drupal module it lists using SWX 1.01, since which time SWX 2 has come out. I wonder if upgrading is worth a try (or will break everything) ... I will upgrade and see what happens.

Didn't work. Just broke

Fixdit's picture

Didn't work. Just broke everything. Haha. Putting it all back where it came from.

When you say "it broke

yajnin's picture

When you say "it broke everything" there may be a reason. And it may be a simple matter of adjusting how you're building everything.

There are so many security considerations in Services, and points of failure, you should take the time to review what the differences are between each module.

Perhaps a couple simple fixes to your code, and everything starts working. Including the stuff that isn't working now.

I have to focus on other things now. But I'll leave you with this:

Upgrade. Find out the handshake in the new module. If you find it requires a text string, where you would expect it requires an array or object, then you need to serialize. You essentially need to ensure your square pegs go in the square hole. And circle pegs in the circle holes.

Assume that your server did not follow the convention. Assume there is no convention.

In all things Services, try

yajnin's picture

In all things Services, try keeping abreast of the newest releases. This issue may have been resolved.

It should also be noted,

yajnin's picture

It should also be noted, based in my comments re middleman, they could really be doing ANYTHING in the go between.

If you upgrade and still have trouble, you'll need to ask the module queue for tips.

If you find they went with a proprietary handling of data you might want to speak up... Or at least ensure it's documented.

Already contacted the

Fixdit's picture

Already contacted the previous maintainers who sent me over some info on SWX, but no longer have anything to do with the module. If I can get a grip on this I'll be sure to share it with the world ;)

That serialised string you gave an example of, theoretically that would be as good as importing a whole Serializer Class? In other words I could just try out your bespoke variable in my own code pretty much as is and it should do?

Okay I think I am seeing

Fixdit's picture

Okay I think I am seeing something interesting here. SWX handles all the variables sent from the flash in a big old url and then decodes it. Here is what I have found from doing some debugging work:

GET /services/swx?url=file%3A%2F%2F%2FFixdit%2520Ltd%2FWork%2FNokia%2FFlash%2520App%2FFinal%2520App%2Fmomw%2Fmain%2Eswf&debug=true&serviceClass=Drupal&method=callService&args=%5B%22node%2Esave%22field%5Fname%22%3A%5B%22H%3A2%7Bi%3A0%2Cs%3A4%3A%5C%22pleh%5C%22%3Bi%3A1%3Ba%3A2%3A%7Bi%3A0%3Bs%3A4%3A%5C%22pleh%5C%22%3Bi%3A1%3BR%3A3%3B%7D%7D%22%5D%2C+%22body%5Fvalue%22%3A%22%3Cp%3E%3Cb%3ECustomer+name%3A%3C%2Fb%3E+gcvghchgchgchgc%3Cbr%3E+%3Cb%3ECustomer+email%3A%3C%2Fb%3E+dfgdfgdfgdfgdfgdfg%3C%2Fp%3E%3Cp%3E%3Cb%3ECustomer%5C%27s+recommendations%3A%3C%2Fb%3E%3Cbr%3ENokia+N97+device%3Cbr%3EOvi+Maps+Service+%3Cbr%3EFacebook+and+Myspace+application%28s%29%3C%2Fp%3E%22%7D%5D%5D

Cutting a long story short, here is the part where data for a CCK field (called 'field_name') is getting pushed. At the moment it's just the letter 'a', the first character.
%22field%5Fname%22%3A%5B%22H

I then did a test where I just used a string as the value of 'field_name' and saw this in the URL:

field%5Fname%22%3A%22%255B%257BHELLO%2BWORLD%257D%255D%22%2C

... the full string.

This isn't going to fly with drupal and CCK and indeed no node is created. I can't supply data in the form of an array because it just sends the first character and I can't send a string as is because CCK wants it in a certain way... hmmmm

This is what I'm saying...

yajnin's picture

This is what I'm saying... "hello world" won't work because it has to be in a format that Services expects. That's the first couple posts about how to format the CCK field.

You have to serialize your actual data, if you want the test to work. Otherwise, there's no difference from using any old text string, ie: "hello world", leaving out all the strange looking serialization. SERIALIZED DATA IS JUST PLAIN TEXT. (Put together a certain way).

It may not throw an error because you're sending a text string, as it expects, but if you're not getting a confirmation returned, an error is what you have. (Just a different error. IE: Your call is formulated wrong.)

You need to replace "hello world" with the right handshake. And if that is serialization, or JSON, you need to build it in that fashion. Serialization would be the first place to start... As you'd expect the module creators to follow basic conventions. But who knows..

// Beyond that

If it isn't serialization, it could be anything honestly. The real job of each Services Server is to parse and direct data, and who knows how they've chosen to do it. You'll have to look through their code to discover the handshake.

Not much else any of us can do for you if you insist on needing that module.... good luck!

I've decided the fix needs to

Fixdit's picture

I've decided the fix needs to be on the PHP code side rather than the Flash after testing. The server just wasn't build to cope with objects inside arrays inside an objects (in it's current shape). So I need to improve this using the existing long string URL method SWX currently employs.

Things are becoming slowly clearer. Your great help and patients is much appreciated :)

Right. So the server needs a

yajnin's picture

Right. So the server needs a better parsing method. Or simply requires serialization :)

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: