Thursday, February 24, 2011

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

One slight change: you're $model param is superfluous. There's no need
to pass the model as this is the AppModel. Just use $this when
referencing it.

I'd also check if $this->data exists before doing anything.

OK, one other possibility:

public function data()
{
if (empty($this->data)) return null;

$res = Set::extract(
'/' . implode('/', func_get_args()),
$this->data
);

// ...

return $res;
}

I didn't return immediately because I know that, in some cases, $res
is going to be formatted in unhelpful ways. So some further work would
be needed.

On Thu, Feb 24, 2011 at 11:30 AM, Joshua Muheim <psybear83@gmail.com> wrote:
> 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
>

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