I am developing a web application that should handle XML data, and I'm facing some problems when I try to edit data that I have previously saved.
This is What I'm doing from the beginning: The user has to fill a form with several inputs, I grab that data in my controller, convert it into an XML string with Xml::fromArray and store it in a single database field. This works perfectly and the generated XML string is exactly what I need. This is (part of) the form.
echo $this->Form->input('data.FT.Header.Transmission.Number');
echo $this->Form->input('data.FT.Header.Transmission.Format');
echo $this->Form->input('data.FT.Header.Transmission.Destination');
Now I am trying to make add an edit function for that data. From my controller I grab that data, convert it back to an object with Xml::build and... this is where the problem starts.
If I do this
debug($invoice->data->FT->Header->Transmission);
everything looks fine as it tells me that I have an object containing this:
object(SimpleXMLElement) {
Number => 'Npslu'
Format => 'Rgytz'
Destination => 'Uheac'
}
But, if I try to see what $invoice->data->FT->Header->Transmission->Number contains, instead of a string I get an empty result. Which is why (I think) all the form inputs are empty.
I had a look to the official documentation, tried to convert it to arrays (which should be the old way cakephp handled data) and everything that came to my mind... still no results.
What am I doing wrong?
UPDATE: I managed to create a working object this (dirty) way:
$array = Xml::toArray( Xml::build( $xmlString ) );
$object = json_decode(json_encode($array), FALSE);
Now, if I debug $invoice->data->FT->Header->Transmission I have the correct value, which is a huge step forward in my situation :)
The problem is that the Form helper still don't recognize that value. If I have this in my edit template
echo $this->Form->input('data.FT.Header.Transmission.Number');
then the field is empty. If I specify its value this way
echo $this->Form->input('data.FT.Header.Transmission.Number', array('value' => $invoice->data->FT->Header->Transmission->Number);
it works correctly as it should. I know I could specify every value but I'd like to have it automatically, as it does with other fields (non XML related) that I have in that page, like $invoice->sent which is correctly recognized by $this->Form->input('sent')...
UPDATE 2: this is the XML generated code:
<?xml version="1.0" encoding="UTF-8"?>
<FT>
<Header>
<Transmission>
<Number>1234</Number>
<Format>qwer</Format>
<Destination>asdf</Destination>
</Transmission>
</Header>
</FT>
And these are the functions I'm using to convert the input field's values to an XML and back:
// Values to XML, called from the add function this way:
// $this->request->data['data'] = $this->_arrayToXml($this->request->data['data']);
protected function _arrayToXml($array) {
$xmlObject = Xml::fromArray($array);
$xmlString = $xmlObject->asXML();
return $xmlString;
}
// XML to Values, called from the edit function this way:
// $invoice->data = $this->_xmlToObject($invoice->data);
protected function _xmlToObject($xmlString) {
$array = Xml::toArray( Xml::build( $xmlString ) );
$object = json_decode(json_encode($array), FALSE);
return $object;
}
p.s. please forgive me if you see some minor incongruences, I'm trying to strip all non related code to avoid posting tons of code...
debug()call to work,FTwould need to be wrapped into adatanode and root node? Is the mistake in the XML data or in the debug call? ps. please always put code in your question so that it is self-contained. - ndm$invoiceis an entity? I thought it's the XML object. - ndm