PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

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

 January 22, 2022     cakephp, php     No comments   

Issue

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:

  • Multiple relations to the same model- http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

  • HABTM- http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

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


Solution

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')
);


Answered By - AgRizzo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing