CakePHP如何通过另一个连接表与hasMany通过定义两个相同的模型

发布于 2024-11-27 08:20:21 字数 1221 浏览 1 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(1

梦醒灬来后我 2024-12-04 08:20:21

第一:您不能两次创建相同的关联。即使从 PHP 语法的角度来看,这也是不可能的。您必须为每个关联指定一个唯一的名称:

public $hasMany = array(
    'SentInvitation'     => array(...),
    'ReceivedInvitation' => array(...)
);

其次,问题可能是您没有邀请模型。 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:

public $hasMany = array(
    'SentInvitation'     => array(...),
    'ReceivedInvitation' => array(...)
);

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 a sender_users table. Create the Invitation model and set it up without that association, then Cake shouldn't be looking for it.

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