That's because, in the context of the view, $athlete is an array, not an object.

A better (I think) approach to this would be to use
Athlete::afterFind() to create the full name. Then, you could access
it in the view with $athlete['Athlete['full_name'] (or whatever you
make the key).

Have a look at AutoFieldBehavior[1], which takes care of the
afterFind() work for you. You would use it like so:

public $actsAs = array(
        'AutoField' => array(
                'full_name' => array(
                        'fields' => array('first_name', 'last_name'),
                        'mask' => '%s %s'
                )
        )
);

[1] 
http://cakeexplorer.wordpress.com/2007/09/10/autofield-behavior-or-how-to-add-additional-columns-to-your-models/

On Mon, Sep 21, 2009 at 9:21 AM, DK <[email protected]> wrote:
>
> My model page - athlete.php - has a method getName():
>        function getName() {
>                return $this->data['Athlete']['first_name'] . " " . $this->data
> ['Athlete']['last_name'];
>        }
>
> I can call this in my controller and debug the name to the debug
> output (last line in the method):
>        function view($id = null) {
>                $this->Athlete->id = $id;
>                $this->set('athlete', $this->Athlete->read());
>                debug($this->Athlete->getName());
>        }
>
>
> I am hoping someone can tell me the correct syntax on how to access
> this method directly from a view:
> <?php debug($athlete->getName()); ?> returns the following error:
> Fatal error: Call to a member function getName() on a non-object in C:
> \wamp\www\cake\app\views\athletes\view.ctp on line 2
>
> <?php debug($athlete['Athlete']['name']); ?> returns the error I was
> expecting, but I thought there was a slight chance the automajic would
> kick in:
> Notice (8): Undefined index: name [APP\views\athletes\view.ctp, line
> 2]
>
> So, can someone show me an example of how to access object methods
> directly from w/in a view or let me know if I am approaching this the
> wrong way?
> Thanks in advance,
> Dave
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to