开发者

CakePHP how to define two of the same models through another join table with hasMany through

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜