Friday, August 31, 2012

Re: REST, Xml::fromArray() and find('all')

Thanks for your answer. Yes indeed it doesn't behave the way the manual says it should.

For now I will use an alternative ;-)

Le jeudi 23 août 2012 11:47:02 UTC+1, Alexandre Leprêtre a écrit :
Hi everyone,

I am trying to build a nice REST API for my website and I had never tried it through CakePHP. I started to follow the (short) instructions, however, the way it works looks totally different from what I can read in the Cookbook.

I use CakePHP 2.2.1.

Here's what I've done so far:

app/Controller/OrdersController.php
<?php
class OrdersController extends AppController {
public function fetch($accountId) {
$this->loadModel('Account');
$this->Account->id = $accountId;
$orders = $this->Order->find('all', array(
'conditions' => array('Order.account_id' => $accountId)
));
$response = $orders;
$this->set(compact('response'));
}
}

app/View/Orders/xml/fetch.ctp 
<?php
$xml = Xml::fromArray(array('orders' => $orders));
echo $xml->asXML();

I tried this with the first account and everything was fine

Ouput:
<orders>
<Order>
<id>502cc39f-5b74-4a25-9bb2-670458d0d2b2</id>
<order_ref>96277</order_ref>
<account_id>502cc39d-8640-465a-b0da-670458d0d2b2</account_id>
<created>2012-08-16 09:55:43</created>
<modified>2012-08-16 09:55:43</modified>
</Order>
</orders>

Everything is fine with an account that has one order.

However, with an account that has several orders, this doesn't work anymore.

Expected output:
<orders>
<Order>
<id>502cc39f-66eb-59d2-2337-670458d0d2b2</id>
<order_ref>96255</order_ref>
<account_id>502cc39d-6325-ad25-5b64-670458d0d2b2</account_id>
<created>2012-08-16 09:42:33</created>
<modified>2012-08-16 09:42:33</modified>
</Order>
<Order>
<id>502cc39f-ee21-3c89-8120-670458d0d2b2</id>
<order_ref>96231</order_ref>
<account_id>502cc39d-6325-ad25-5b64-670458d0d2b2</account_id>
<created>2012-08-16 09:21:01</created>
<modified>2012-08-16 09:21:01</modified>
</Order>
</orders>

What I get:

This page contains the following errors:

error on line 1 at column 367: Entity 'nbsp' not defined

Below is a rendering of the page up to the first error.

Warning (2): SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 3: parser error : Extra content at the end of the document [CORE\Cake\Utility\Xml.php, line 197]
Code Context

I assume that Xml::fromArray() doesn't accept multiple numeric keys. Or something like this. I don't mind editing my PHP array to get he XML I expect but I really don't know how I should write my array for this :-(

Any help is welcome.

Thank you!!

Alex

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

No comments: