CakePHP: Setting Default Values

If you've got a user seeing a form for the first time, normally the fields are blank. But there are times where you want to prefill fields with data or preselect a certain option. Setting a default value turned out to be really straightforward. In my controller, I have a method called create that sets any default values that I might have.

class ProjectsController extends AppController
  function create()
    $this->data['Project']['name'] = 'Default Project Name';

Just like that, my model now has some default data to use within the form.

I've put in a request ticket to expand the CakePHP functionality to read any default values set at the database level and to use those where possible.

Side note on "data"

On a side note, something I managed to clarify for myself was how to access model data in a consistent fashion. I had seen both $this->data and $this->params['data']. Even more confusing was that some examples showed $this->set('data', $this->Model->read()) and then accessing the model in the view using $data['Model']['field'].

While all of these work, it seems $this->data would or should be the recommended way to access that data. This should work at the controller, view, and helper level without issue.

Note: this is using CakePHP version

Published June 27, 2006 · Updated June 28, 2006
Categorized as CakePHP
Short URL:


10 Comments · RSS feed
Wesley Walser said on June 28, 2006

So Jonathan, with all of these articles are we safe to assume that since you first impressions you are still trying things out with cake, or have you decided that it's the way to go on all or certian applications?

Miles Johnson said on June 28, 2006

Ya thats what I was thinking, because you have posted many CakePHP articles lately.

Jonathan Snook said on June 28, 2006

I would certainly not advocate CakePHP for all applications but I would recommend it for most. It's turning out to be a decent framework to work with and it certainly can do a lot for you for.

My biggest complaint is still documentation. In fact, the main reason I've had so many CakePHP posts is because this stuff isn't really documented (or documented well enough) on the Cake site.

So, I suppose my official response would be: use Cake if you're willing to meet it halfway. That means willing to jump into IRC or the wiki or the Google group to find an answer that may not be clear in the manual.

Miles Johnson said on June 28, 2006

It seems like a very interesting thing framework and something I would consider unlike Rails, since I havent had the time to learn Rails.

Would you suggest starting to learn CakePHP now? or at a later time when its a more stable and documented version?

Emil said on June 28, 2006

There are a lot of different frameworks out there, my friend have almost convinced me to use Zend framework, he says that it's great.

These articles makes me wanna use Cake, and another part of my brain says "you should try to learn RoR". I want to work with stable frameworks as 90% of my programming is for production use.

Please give us some advice, are there any comparing reviews out there? And what do you recommend for production use. Until now I'v only worked with a framework developed by my company and I'm not very satisfied with it, thats why I'm looking around.

sosa said on June 28, 2006

before 1.0 we used $this->params['data'], then the $this->data alias was added. The controller variable $this->data stores the values of a form sent in the 'Model/value' format. But if you need to acces othe values (like when uploading files) $this->params will contain those values.

Max said on July 02, 2006

Emil, I would suggest that you use Cake and here is why:

1. Cake is more stable than most of the other frameworks out there.

2. Cake keeps getting better. (Documentation and features set)

3. Cake is extremely flexible and easy to extend.

Zend is very nice, however, it is still in the early stages of development. Besides, any of the components from Zend can be easily integrated into cake ;)

Rob said on July 04, 2006

Baking with Cake is still an issue, I noticed with older version of bake.php, it creates code to access data using $data, and 1.15?? uses $ $data['Model']['field'] was old bake, new bake is $model['Model']['field'].....

nate said on July 04, 2006

A little clarification:
The Controller::set( ) method is just a way to pass a variable from the controller to the view. The basic syntax is $this->set('varname', $value).

There isn't really a formalized or "right" way to use the method, or how to name your variables. Just code to preference.

However, $this->data and $this->params['data'] (functionally equivalent; the former was added post-1.0 for the sake of less typing) are special variables that get passed to the view automatically. They have special significance for form fields and the like.

z. said on August 25, 2006

This works really well. If you have a logged in user:

$this->data = $this->Session->read("User");

?php echo $html->input("User/name",array()); ?

will be auto populated, and as many other fields you want to add to the form! becareful of overhead though

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.