CakePHP 模型别名有什么用?

发布于 2024-10-03 23:59:37 字数 574 浏览 0 评论 0原文

在用户模型中:

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

在照片模型中:

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

这里一个用户有很多照片。 那么我的问题是,这里的别名是“Owner”,这让我清楚地理解“User”的确切含义,但这是否是使用别名的唯一原因?它会影响用户模型中的“照片”吗?或者如何在 cakephp 中/通过 cakephp 使用“所有者”?

我不太明白模型中别名的含义。 感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-10-10 23:59:37

别名的两个有用场景:

1.同一模型的多个外键

例如,您的 photos 表有两个字段:created_user_id & modified_user_id

var $belongsTo = array(
    'CreatedUser' => array(
        'className' => 'User',
        'foreignKey' => 'created_user_id',
        ...
    ),
    'ModifiedUser' => array(
        'className' => 'User',
        'foreignKey' => 'modified_user_id',
        ...
    ),
);

2.创建特定于应用程序域的逻辑字

使用数组中的条件字段,您可以指定不同类型的模型:

var $hasMany = array(
    'ApprovedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array(
            'User.approved' => 1,
            'User.deleted'  => 0
        ),
        ...
    ),
    'UnapprovedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array(
            'User.approved' => 0,
            'User.deleted'  => 0
        ),
        ...
    ),
    'DeletedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array('User.deleted'  => 1),
        ...
    ),
);

在上面的示例中,组模型具有不同类型的用户(已批准、未批准和已删除)。使用别名有助于使您的代码非常优雅。

Two useful scenarios for aliases:

1. Multiple foreign keys to the same model

For example, your photos table has two fields: created_user_id & modified_user_id

var $belongsTo = array(
    'CreatedUser' => array(
        'className' => 'User',
        'foreignKey' => 'created_user_id',
        ...
    ),
    'ModifiedUser' => array(
        'className' => 'User',
        'foreignKey' => 'modified_user_id',
        ...
    ),
);

2. Creating logical words specific to your application's domain

Using the conditions field in the array, you could specify different kinds of models:

var $hasMany = array(
    'ApprovedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array(
            'User.approved' => 1,
            'User.deleted'  => 0
        ),
        ...
    ),
    'UnapprovedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array(
            'User.approved' => 0,
            'User.deleted'  => 0
        ),
        ...
    ),
    'DeletedUser' => array(
        'className' => 'User',
        'foreignKey' => 'group_id',
        'conditions' => array('User.deleted'  => 1),
        ...
    ),
);

In the above example, a Group model has different kinds of users (approved, unapproved and deleted). Using aliases helps make your code very elegant.

硪扪都還晓 2024-10-10 23:59:37

它允许您执行诸如 $this->Owner->read(null,$userId); 之类的操作。您可以拥有一个 OwnersController 和视图/所有者。

这是……一个别名。从某种意义上说,User 是 db 表 users 的别名。

一个更好的例子:我有一个 CMS,我使用表格文章来表示文章、博客项目和新闻。这三个名称是同一个表的别名,允许我设置不同的模型、关系和行为。所以我有一个 BlogItemsController 和一个 NewsController 以及一个 ArticlesController。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文