Alex Rivera | Logout

what is CakePHP model alias used for?

Asked 2010-11-25T08:49:03.663
11

In user model:

var $hasMany = array(
        'Photo' => array(
            'className' => 'Photo',
            'foreignKey' => 'owner_id',
            ...
        ),
);

In photo model:

var $belongsTo = array(
        'Owner' => array(
            'className' => 'User',
            'foreignKey' => 'owner_id',
            ...
            ),
);

Here one user has many photos. So what my question is that here the alias name is 'Owner', which make me clear to understand the exact meaning of 'User', but is this the only reason to use alias? does it affect 'Photo' in user model? or how to use 'Owner' in/by cakephp?

I don't quite understand the meaning of alias in model. Appreciate your help!

Edit
Report

1 Answer

3

It allows you to do things like $this->Owner->read(null,$userId); You can have an OwnersController and views/owners.

It is ... an alias. In a sense, User is an alias for the db table users.

A better example: I have a CMS where I use the table articles for Article, BlogItem and News. Those three names are aliases for the same table that allow me to set up different models, relationships and behaviour. So I have a BlogItemsController and a NewsController as well as an ArticlesController.

answered 2010-11-25T09:03:36.613

Your Answer