Thursday, February 24, 2011

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

Just in case somebody is interested... I have added a small method
data() that works a little nicer in my opinion than the standard way
of accessing the data array's values is...

So instead of $this->Model->data['User']['name'] you can just call
$this->Model->data('name')... saves you a few keystrokes. Most of you
guys will say this is not necessary... ;-) And maybe you're right. But
here it goes...

// Put this into your AppModel
/**
* Makes returning model data convenient.
*
* Examples:
* - data('name') returns $model->data[<ModelName>]['name']
* - data('Group') returns $model->data['Group']
* - data('Groups') returns $model->data['Groups']
* - data('Group', 'id') returns $model->data['Group']['id']
*
* @param $model Model
* @return mixed
*/
function data(&$model) {
$association = null;
$field = null;

$params = func_get_args();
array_shift($params); // Remove the pointer to the model (first argument)

$paramsCount = count($params);
$association = null;
$field = null;
if($paramsCount == 2) {
$association = $params[0];
$field = $params[1];
} elseif($paramsCount == 1) {
if(ctype_upper($params[0][0])) {
$association = $params[0];
} else {
$association = $model->name;
$field = $params[0];
}
}

if(isset($model->data[$association])) {
if(!empty($field)) {
if(isset($model->data[$association][$field])) {
return $model->data[$association][$field];
}
} else {
if(isset($model->data[$association])) {
return $model->data[$association];
}
}
}

return false;
}

On Wed, Oct 20, 2010 at 1:14 AM, cricket <zijn.digital@gmail.com> wrote:
> On Tue, Oct 19, 2010 at 7:03 PM, cricket <zijn.digital@gmail.com> wrote:
>> On Tue, Oct 19, 2010 at 5:53 AM, psybear83 <psybear83@gmail.com> wrote:
>>> Hi everybody
>>>
>>> Sorry for this newbish question, but I don't seem to find much about
>>> this (although I should, I guess).
>>>
>>> $m = $this->Model->find(1);
>>
>> Since when does find() work like that? What version are you using?
>>
>>> echo $m->data['Model']['something];
>>
>> How are you getting an object returned from find()? Did I miss something?
>>
>
> Whoops! Please disregard. I didn't understand that you were
> *suggesting* it be done this way.
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
>
> You received this message because you are subscribed to the Google Groups "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> 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?hl=en
>

--
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: