CakePHP Headaches

So I’ve started working on a little content app for the sites here and thought it would be a good sized project for a trial run of a few things including the CakePHP rails-like MVC framework written in PHP.

A few places I’ve stumbled and broken my toe so far…

Queries are cached by default

This was my first noob stumbling block. You do a findByName on an object and see it isn’t found so you add it. Then later on in the loop you happen to look up that same item but the result is still that it doesn’t exist. Did your save fail without warning? Nope, the findByName results were cached and didn’t get updated after the insert. The quick and dirty solution – adding var $cacheQueries = false; to appropriate models.

Order of array data matters in hasAndBelongsToMany relationships

This one I would have never figured out had it not been for stumbling over it while looking up some other problems I was having with my HABTM cases. Bug, intentional, I don’t know, but it certainly wasn’t something I was expecting.

Creating new associated objects

This is more of a gripe then a problem, but if as a framework you’re going to provide lots of core tools to manage relations like hasOne or HABTM then why not provide some utilities to deal with both new primary objects and new associated objects during a save of the primary object? Seems to me (and to some others judging by the topic of questions I have come across) that the need to generate related objects on the fly is quite common—especially when you move away from doing all of your content management via scaffold based web forms.

Hard to grok errors from the framework

… like Notice: Undefined index: id in /ServerRoot/wowt/public_html/cake/libs/model/model_php5.php on line 914

Sure, I was able to trace it back and figure out where things were going wrong after upping the debug level on my app and adding a few other print_f calls, but something a bit more informative might be helpful and save some time.

I certainly realize that the framework is still in active development, that and I’m coming at things head one and coding without much background with the existing docs and other resources (so far the mailing list seems to be the best source of information). Many of these headaches would go away with time (either as a result of work of core devs or my own familiarity), but I thought I needed to make a record of them for future reference.

As for the app I’m building—you’ll know more soon-ish.

P.S. RSS is giving me as many if not more headaches. Maybe I’ll cover that later.

This entry was posted in web. Bookmark the permalink.

6 Responses to CakePHP Headaches

  1. Othy says:

    Queries are cached by default
    =========================
    Yes. good call,
    You will find var $cacheQueries = false;
    in the latest trunk code.
    Tho, In production you would want to change it to true.

    Order of array data matters in hasAndBelongsToMany relationships
    ==========================================
    Conventions.

    Creating new associated objects
    ============================
    You missed Model::bindModel and Model::unbindModel.

    Hard to grok errors from the framework
    ==================================
    This is hardly something the Framework can control, Cake does a good job at giving you instructive messages about what you are doing wrong when it comes to the framework elements: You request localhost/posts/
    if you don’t have PostsController cake tells you you need to create the controller and even gives you the minimal code to put. You miss an action, you miss a view, you use renderElement wrong,..etc
    but the error you’re having above is related to the language.

    If you can step by the IRC channel, we’ll be glad to remove your frustration.

    Best regards.

  2. chris says:

    Thanks for stoping by Othy.

    As I mentioned at the end of my post I do realize that many of the places I got tripped up will be resolved as I spend more time working with the framework—either by finding alternatives (or intended methods), learning to live with, or changes to the framework itself.

    But none the less your reply was helpful. I will cetrainly have to check oun bind and unbindModel more as it may resolve a few other issues (like the cacing issues in my particular case)

  3. max says:

    the mentioned warning with “id” field was probably the same nature I’ve met when first working with cakePHP – it’s about not following conventions: if your primary key is not ‘id’ and you’ve forgoto to put var $primaryKey=’myId’ into model definition, the framework messages will be cryptic like that. And they CAN be less cryptic in that special case, that’s not a language issue anyway.

  4. asterisk_man says:

    RE: Queries are cached by default
    I found that setting cacheQueries to false will fix all the query functions EXCEPT query(). Apparently you have to add a false to that call for now. query(queryString,false). I found this here https://trac.cakephp.org/ticket/1915 but I found this blog much more quickly so I figured I’d post it here too.

  5. sshah254 says:

    Hi,

    I am getting the same error “Undefined index” … can you give me some pointers on how to troubleshoot that?

    Thanks

  6. prakash1708 says:

    sir,

    this has reference to the undefined index error.
    i am having a table with ‘rec_id’ as the primary key (auto increment variable).
    i started out with cakephp (using scaffolding), and initially i had not added the ‘primaryKey’ variable in the model definition. but after going through the above messages i added the ‘primaryKey’ variable. now i am getting a different error as under :

    Undefined index: rec_id [CORE\cake\libs\view\scaffolds\index.ctp, line 65

    please help

    regards

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>