marko_b123
marko_b123

Reputation: 330

Cakephp, 1 foreign key on 2 or more fields in table

Hy,

I have 2 tables:

-1. users- (id, name, surname, mail.....)

-2. orders- (id, date, owner, aprowed_by.....)

And problem is: fields owner and aprowed_by need to be foreign keys from table users and named users_id but i cant name both fields users_id. I linked relations in php my admin, and baked CRUD code, but ofcourse it want work. I tried next from google and from cake site:

But it want work. maybe i work something wrong, but if someone could see what kind of relations would be that or give me some tip?

Thanks in advance, Best regards

Upvotes: 1

Views: 404

Answers (1)

AgRizzo
AgRizzo

Reputation: 5271

Using the Multiple relations to the same model knowledge, your models should have code like this:

User

public $hasMany = array(
      'Owned_Order' => array(
          'className' => 'Order',
          'foreignKey' => 'owner'),
      'Aprowed_Order' => array(
          'className' => 'Order',
          'foreignKey' => 'aprowed_by')
);

Order

public $belongsTo = array(
      'Owner' => array(
            'className' => 'User',
            'foreignKey' => 'owner'),
      'Aprowed' => array(
          'className' => 'User',
          'foreignKey' => 'aprowed_by')
);

Upvotes: 3

Related Questions