CakePHP如何通过另一个连接表与hasMany通过定义两个相同的模型
我想使用 hasMany through
关系通过模型 invitations
连接两个 用户
我目前有以下控制器:
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array(
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'sender_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'receiver_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
)
}
但是,当我收到错误时:
Error: Database table sender_users for model SenderUser was not found.
这有点烦人,因为我已经认为我指定要使用Invitation
。
这是什么问题,我应该更改什么代码?
I would like to use hasMany through
relationship to connect two users
through a model invitations
I currently have the following controller:
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array(
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'sender_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'receiver_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
)
}
However, when i get error:
Error: Database table sender_users for model SenderUser was not found.
Which is a bit annoying, because I already thought I specified that I want to use Invitation
.
What is the issue, what code do I change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一:您不能两次创建相同的关联。即使从 PHP 语法的角度来看,这也是不可能的。您必须为每个关联指定一个唯一的名称:
其次,问题可能是您没有邀请模型。 Cake 根据命名约定即时创建模型。由于您有一个
sender_user_id
列,根据命名约定,您还应该有一个sender_users
表。创建邀请模型并在没有该关联的情况下设置它,那么 Cake 不应该寻找它。First: you can't create the same association twice. Even from a PHP syntax perspective that's not possible. You'll have to give each association a unique name:
Second, the problem is probably that you don't have an Invitation model. Cake creates a model on-the-fly according to naming conventions. Since you have a column
sender_user_id
, according to naming conventions you should therefore also have asender_users
table. Create the Invitation model and set it up without that association, then Cake shouldn't be looking for it.