1

I'm having a strange situation and I'm not quite sure where to start looking. I have a knockout.js form on the front end, and CakePHP in the backend.

As of right now, I send some JSON data from a knockout.js form to CakePHP. This PHP does some processing and then echo's the result in JSON format back to the knockout javascript file.

Well, the issue is when I have this PHP setup through CakePHP it seems to only work maybe 1/4 of the time. When I have this PHP file in my webroot (just a standard file, not associated with Cake, because knockout is backend agnostic after all) it works 100% of the time.

So here are the two data flows

Fails frequently: knockout.html -> knockout.js -> CakePHP Controller -> echo in CakePHP view-> knockout.js -> knockout.html

Works 100%: knockout.html -> knockout.js -> PHP in webroot, echo from the same page -> knockout.js -> knockout.html

I would just use the PHP file in my webroot but I have to query the database for a few things and I'd like to keep all the querying up to Cake and not outside sources.

Is the CakePHP method possibly taking too long and failing because it's not getting a result back quickly enough?

edit- I changed my controller so there are no calculations, it simply passes on some JSON encoded text to the view. This produces no errors and has worked 100% of the time. So, is it safe to say that CakePHP is taking too long causing the error? I understand a framework takes longer than just plain PHP, but why would this cause it to fail? I'm not doing any intense calculations.

user1104854
  • 2,137
  • 12
  • 51
  • 74
  • Can you briefly explain what kind of calculation are you doing? does it involve database queries? maybe calls to another action via requestAction? – Headshota Apr 23 '13 at 11:06
  • @Headshota First I grab one item from my database and then I use PHPquery to grab some content (3 instances of this) off of another site, which understandably may take a short while. However, I don't see why this is so drastically different between CakePHP and a standard PHP file. I even tried eliminating the db query by setting that variable equal to the expected value and using that for PHPquery and it still fails occasionally leading me to believe CakePHP doesn't work well with PHPquery. – user1104854 Apr 23 '13 at 11:19
  • @Headshota I should clarify that I only grab content once from my database, and then use PHPquery to grab items from an external source three times. – user1104854 Apr 23 '13 at 11:40
  • This seems to me to be a PHPquery issue. have you tested the same functionality outside the framework, I am pretty sure, you'll get the same results. – Headshota Apr 23 '13 at 13:44
  • @Headshota yes, as I described above (check out the little flow diagram thingy I made for more detail) I used this exact same PHP outside of Cake and it works 100% of the time. Instead of having knockout call to cakephp (say cake/items/123) I called directly to the file located in my webroot(items.php?item=123). – user1104854 Apr 23 '13 at 14:26

1 Answers1

0

I got it to work in a very roundabout way. It's working 100% of the time now, but I'm not satisfied with how I had to do it.

As I explained I'm using PHPQuery in my ItemsController. I took all of this logic and made a separate PHP file completely outside of the cake structure and just in my webroot. So instead of knockout calling my ItemsController function for that respective action, it calls the file in the webroot. From this file I access the database, lookup what I need, then perform the PHPQuery tasks.

Very strange. Works 100% outside of CakePHP. Fails more than 50% of the time when used in a CakePHP function. I don't get it, but at least it's working.

user1104854
  • 2,137
  • 12
  • 51
  • 74
  • Just wondering, what exactly are you using PhpQuery for? Wouldn't it be faster to fetch the external data directly via JavaScript/jQuery? Fetching the external data via PHP for each request seems like a lot of overhead, unless you're caching the results – thaJeztah Apr 23 '13 at 21:50
  • I suppose it could be, however, part of the reason why I'm using PHP is for concealing some of the code and because I have to query the database (using PHP). Javascript would be visible to everyone if I'm not mistaken (at least a .js file) – user1104854 Apr 23 '13 at 21:59
  • Well, I noticed that PhpQuery hasn't received any update since 2010. Caching external request is advisable in most situations (depending of course on how often you expect those external data will change). Also consider the built-in Dom functions of PHP, depending on your requirements this may be enough for your purpose. See the documentation [DomDocument::loadHTML()](http://php.net/manual/en/domdocument.loadhtml.php) and this question [How to parse and process HTML/XML](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-xml) – thaJeztah Apr 23 '13 at 22:07