Cakephp - 一个表的多个模型 [英] Cakephp - Multiple Models for one Table

查看:168
本文介绍了Cakephp - 一个表的多个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于存储牙科检查系统用户数据的用户模型。

I have a User model that is used to store data on users of a dental examination system.

通常,存在三种类型的用户: Admininistrator 位置管理员审查者

Typically, three types of users will exist: Admininistrator, Location Manager and Examiner.

似乎已经有必要对待这三个角色作为我的应用程序的独立模型(想象如何我有不同的意见,每个角色与不同的选项等...这是一个噩梦)。

It seems to have become necessary to treat these three roles as seperate models in my application (imagine how I'd have a different view for each role with different options etc... It's a nightmare).

我将如何去关于在每个模型中设置关系。

How would I go about setting up the relationships in each Model.

我的第一个想法是:

//Administrator Model
class Administrator extends User {
    $name = 'Administrator';
    $table = 'User';
    $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'conditions' => array('User.role' => 'administrator'), 
        )
    );
}

然后用户模型将使用hasMany引用这一个?在CakePHP协议方面,如何准确地建模这个模型?

And then the User model will reference this one using a hasMany? In terms of CakePHP convention, how would one actually model this accurately?

此外,这个模型是否扩展了User模型或AppModel?

Also, would this model extend the User model or the AppModel?

推荐答案

当然,您可以使用相同的表名创建不同的模型。为此,请使用$ useTable属性(而不是$ table)将每个模型与特定表格相关联:

Of course you can create different models using the same table name. To do so, link each model with specific table with $useTable property (not $table):

class Administrator extends AppModel {
    public $useTable = 'User';
}

但我不知道CakePHP模型属性,获取结果时返回的数据...您只能在将模型与另一个模型链接时编写自己的条件,例如:

But I don't know any CakePHP model property which will allow you to filter data returned when fetching results... You can only write your own conditions when linking model with another one, for example:

class SomeOtherModel extends AppModel {
    public $hasMany = array(
        'Administrator' => array(
            'className' => 'Administrator',
            'conditions' => array('Administrator.role' => 'administrator')
        )
    );
}

这不是一个好的解决方案,因为它只会在执行查询SomeOtherModel模型。

Which is not a good solution, because it will work only when executing queries for SomeOtherModel model.

您也可以尝试将afterFind()回调应用于您的管理员/审查者/ ...模型,这将删除不同于所需角色的用户,不是一个有效的解决方案。

You can also try applying an afterFind() callback to your Administrator / Examiner / ... models which will delete users of another role than needed, but of course it is not an efficient solution.

另一个解决方案是创建不同的数据库视图,其中只包含所选角色的用户(更多视图 - 这里)。然后,您可以将每个视图指向您的CakePHP模型,就像它是一个普通的数据库表。
但是这个解决方案也不完美。如果您将来必须添加新的用户角色或更改表模式,您还必须修改数据库视图。

Another solution is just creating different database views which will contain only users of selected role (more on views - here). Then you can point each view to your CakePHP model, just as it was a normal database table. But this solution is also not perfect. If you will have to add a new user role or change your table schema in the future, you will also have to modify your database views.

这篇关于Cakephp - 一个表的多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆