Thursday, February 24, 2011

Re: Easiest way to get data from a model (better way than $model->data['modelname']['field'])?

Great, so now when I need to build my view that displays data from
four different models, I can call set a bunch of times in the
controller for each model. come up with a whole new form helper to
deal with data that does not have a model key. And deal with duplicate
fields labeled 'id' in my html form. Then when I post my data from a
form back to the controller I can put a bunch more code in place to
remap all the view data and try to figure out what fields belong to
which models so I can save them correctly.

Or I could use this new convention in the models only (which would be
ok) and then the cake array convention in the views so I can
constantly second guess what format of data i'm working with.

Sarcasm was intended.

Seriously now. Once you get past the simple one model form app and
really start to use cake, your going to find your new simplified
function is going to get in your way or its going to be used so little
you will question why you still have it. There are many reasons the
cake data arrays are created the way they are. And its NOT because
thats the way its always been, or some ridged developer wouldn't
change. Its because it makes for very consistent and portable data
from the Model to the Controller to the View and back. Its not just
data its also a consistent structured container for the data so all
the cake 'magic' from the model, controller and view can make use of
it.

If you find you really just need a single field for a specific record
you can call the model::field() function which will go all the way to
the db to get the data. (slower but more precise in a few situations)
as in:
echo $this->Post->field('name');

I give you credit on at least one point. You put the code in the model
instead of the controller. So your a bit ahead of a lot of the new
developers to a MVC framework. ;)

One point you didn't mention is multiple records from the likes of
find('all') or find('list'). Or dealing with data that is many models
deep (recursive).

There are many, many, many reasons to use the cake array structure.
I would encourage you to re-read chapter 3.7.3

I assumed quite a bit about your project/problem as you never stated
you had one other than the new shoes feel different than the old ones
your used to wearing. Try them out for a week or so, maybe its just
the change you needed.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: