views:

60

answers:

2

I have an application the is loaded with forms in every view. I've become rather familiar with CakePHP's array syntax and I use the $this->data parameter a lot.

I fell in love with skinny controllers and fat models, so I put as much code in my models as is possible. Unless I'm doing something complex, most of my controller methods look like this:

function some_action() {
    $this->set('data', $this->Model->some_action($this->data));
}

Then, in my Model, I have a method that takes one parameter:

function some_action($this_data = NULL) {
    // do some stuff, manipulate the data etc.
    $this->create();
    if ($this->save($this_data)) {
        // return success message here
    }
}

I've oversimplified, but you get the idea.

My question: I always send $this->data to my model and catch it with a variable, $this_data. However, I was wondering if this is one step too many? Does the Model inherit $this->data? If so, could I change the above controller method to this:

function some_action() {
    $this->set('data', $this->Model->some_action());
} 

And then manipulate $this->data in my model instead of the $this_data variable I've been using? What would be the best practice?

+1  A: 

Nice idea,but unfortunately you have to pass $this->data as a parameter because controller and model in cake are extended from diffrent parent-objects.So $this->data has diffrent meanings in them.In controller,$data attribute is for get POST data while it's a container for the model’s fetched data in model.And I suggest you to read the source code in app/cake/lib/controller/controller.php

and

app/cake/lib/model/model.php

That would make you more clear.

SpawnCxy
Excellent, thanks!
Stephen
+1  A: 

What you can do however is :

In your controller

$this->Model->create($this->data);
$this->set('data', $this->Model->some_action());

That way, you can access your model data using $this->data in your Model::some_action();

Pixelastic
That Was Perfecto! I don't wanna take the points away from SpawnCxy Who I marked correct 3 days ago, otherwise this would definitely be my pick. Thanks!
Stephen
wait, would this screw with updates?
Stephen
What do you mean ? It will change your model $id to the $data['Model']['id'] value, if that was your concern.
Pixelastic