Finding your ingredients in CakePHP

Posted on 10/21/09 by Rob Conner
Ever get stuck trying to write code and don't know where to look? If you know the right way to find information within the cake community, you can save yourself time. Not only do you not have to wait for someone to help you, usually you find information you were not looking for.

Some folks just hop right on IRC and start asking. Others post to groups, without even searching anywhere. There is a chain of command for these things. Many times people have already written articles on what you are looking for. You just have to know how to find what they have written.

Here are my 5 easy steps to finding out what you want to know about CakePHP.

Step 1. Search the Bakery - A lot of people have put a lot of time into putting a lot of good code onto the Bakery. It goes through an approval proccess by the cake developers. 80% of the time you'll find a cure for what ails you right here.

Step 2. Search Groups - Chances are if you are having a problem, someone else has had this same problem before you. Most times a solution is also posted

Step 3. Search the Blogs - There is a CakePHP custom search maintained by Ritesh that only searches a list of known Blogs. The many smart people within the Cake community discover new features and ways of doing things They post their findings on their blogs. Using this modern technology called the internet, you are able to go read what they have posted.

Step 4. Ask for help - Depending on your problem it may be easier to ask on Groups or on IRC. I tend to ask smaller simpler questions on IRC. Things that are a bit more complex and take more to explain, I like to ask on Groups. If you are asking questions on IRC, try not to be a drain. People who help on there are not paid and only help because of their love for CakePHP. Technically it is your problem and it is really on you to solve it, not them.

Step 5. Take the Cake apart - Sometimes even asking for help does not work. This is when you really have to start getting your hands dirty. You have to dive into Cake and find out what is going on for yourself. Add print statements, modify the cake core to see how things change. I have a sandbox set separate from my production sites. I use this whenever I need to experiment with Cake. This is far and away the best way to learn about Cake anyways.

There you have it. Happy Baking.


Model Field Names

Posted on 10/21/09 by Rob Conner
Sometimes I've needed to get a list of fields that a Model's table has. This is a little function that I have in my AppModel just to make getting the list of fields easy.

Dont forget, if you just want to know about one field, you can use the already builtin Model->hasField($name)
Code:
<?php class AppModel extends Model { /** * Get a list of all fields of a the table this model is associated with * @param bool, prefix all values with the name of this model eg: Model.fieldname * @return array non-associative array with field names as values * @author rtconner */ function fieldNames($prefix=false) { if (empty($this->_tableInfo)) { $this->loadInfo(); } $arr = array_keys($this->_tableInfo->combine('{n}.name')); if($prefix) { $func = create_function('&$a, $key, &$obj', '$a = $obj->escapeField($a);'); array_walk($arr, $func, $this); } return $arr; } }?>


Rendering and not rendering

Posted on 10/21/09 by Rob Conner
One of the most common questions I see on IRC is "How do I stop the view from displaying?".

The controller has a handy little attribute <em>autoRender</em>, setting it to false will stop the view from being displayed.

Also using the render(...) method automatically sets autoRender to false. This is handy if you want to another view aside from the default one for an action.

Some example code:
Code:
<?php // in a controllers action $this->autoRender = false; header('Content-type: application/pdf'); readfile('/path/tp/pdf_file'); // or say you want to render an element as a view. // maybe you are using ajax if($this->RequestHandler->isAjax()) { // is a cake component $this->viewPath = 'elements'.DS.'products'; $this->render('product_table', 'ajax'); } ?>

Additional links:
Controller::render [api.cakephp.org]
RequestHandler [api.cakephp.org]

-rtconner