CakePHP/Croogo:一个极其复杂的关联
当我从事当前的项目时,我遇到了一个相当复杂的问题。我现在将更详细地指出我的问题:
共有三个模型:User、UsersExtendedField 和 UsersExtended。
UsersExtendedField 包含可以手动管理的自定义字段。所有自定义字段也可以显示在用户视图中,并且当然可以填写。然后这些值存储在 UsersExtended 中,它有两个外键:user_id 和 field_id。
关系看起来像这样:用户有许多用户扩展字段,用户扩展字段有许多用户扩展,用户扩展属于用户,用户扩展字段。
问题:访问用户视图时,会显示一个包含用户信息输入的表单。任何 UsersExtendedFields 也都可用,并且由于这些 hasMany UsersExtended,因此它们具有大量的 UsersExtended 值。但我想将这些值减少到仅属于用户的值,此时会显示用户的视图。这是我的(所需的)关系:
Croogo::hookBehavior('User', 'Crooboard.ExtendedUser', array(
'relationship' => array(
'hasMany' => array(
'UsersExtendedField' => array(
'className' => 'Crooboard.UsersExtendedField',
'foreignKey' => '',
'conditions' => array('status' => 1)
),
),
),
));
class UsersExtendedField extends AppModel {
var $name = 'UsersExtendedField';
var $displayField = 'fieldname';
var $hasMany = array(
'UsersExtended' => array(
'className' => 'Crooboard.UsersExtended',
'foreignKey' => 'field_id',
'conditions' => array(
'UsersExtended.user_id = User.id'
)
),
);
}
这不是完整的代码,这些是重要的部分。问题就从我写“UsersExtended.user_id = User.id”的地方开始。显然,这是行不通的。但我不知道如何访问这里的 User.id 。我也无法想象一个 HABTM 结构来解决这个任务。您知道如何让“UsersExtended.user_id = User.id”的语义发挥作用吗?
非常感谢您花时间阅读本文并为我提供帮助!
When I was working on my current project, I ran into a rather complex issue. I'll point out my problem much more detailed right now:
There are three Models: User, UsersExtendedField and UsersExtended.
UsersExtendedField contains custom fields that can be managed manually. All custom fields can be shown in the Users view as well, and be filled out of course. The values are then stored in UsersExtended, which has got two foreignKeys: user_id and field_id.
The relations look like this: User hasMany UsersExtendedField, UsersExtendedField hasMany UsersExtended, UsersExtended belongsTo User, UsersExtendedField.
The problem: When accessing the Users view, a form with user information input is shown. Any UsersExtendedFields are available as well, and since these hasMany UsersExtended, they've got plenty of UsersExtended values. But I want to reduce those to only the value(s) that belong to the User, whose view is shown at the moment. Here are my (desired) relations:
Croogo::hookBehavior('User', 'Crooboard.ExtendedUser', array(
'relationship' => array(
'hasMany' => array(
'UsersExtendedField' => array(
'className' => 'Crooboard.UsersExtendedField',
'foreignKey' => '',
'conditions' => array('status' => 1)
),
),
),
));
class UsersExtendedField extends AppModel {
var $name = 'UsersExtendedField';
var $displayField = 'fieldname';
var $hasMany = array(
'UsersExtended' => array(
'className' => 'Crooboard.UsersExtended',
'foreignKey' => 'field_id',
'conditions' => array(
'UsersExtended.user_id = User.id'
)
),
);
}
This is not the full code, these are the important parts. The problem starts right where I wrote 'UsersExtended.user_id = User.id'. Obviously, this won't work. But I do not have any idea how to access the User.id here. I also could not imagine a HABTM structure to solve this task. Do you have any idea how to get the semantics of this 'UsersExtended.user_id = User.id' to work?
Thank your very much for taking the time to read through this and helping me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要正确设置您的 HABTM 关系。
您已经拥有连接表 UsersExtended,其中包含您的外键。
删除所有以前的关系并在每个 User 和 UserExtendedField 模型中设置 HABTM。
用户模型中的关系代码如下所示:
有关更多信息,请查看页面在cakephp书中
此外,这篇博文帮助我掌握了我学习cakephp时的关系概念。
It sounds like you need to set up your HABTM relationship properly.
You already have the join table, UsersExtended, which contains your foreign keys.
Remove all previous relationships and set up HABTM in each of your User and UserExtendedField models.
The relationship code in your User model would look like this:
For more information check out the page in the cakephp book
In addition, this blog post helped me grasp the relationship concepts when I was learning cakephp.