How to define two foreign keys in cakePHP

I have a table with multiple columns.
The table has a primary key (response) and two foreign keys (userid and postid).

Here is the table structure: {answerid, content, userid, postid}

The response table received two foreign keys.
I am not sure if the foreign keys are set correctly as follows:

class Post extends AppModel{   
    var $name = 'Post';
    var $useTable = 'post';
    var $primaryKey = 'postid';
    var $belongsTo = 'User';
    var $hasMany = array(
        'Reply' => array(
            'className'     => 'Reply',
            'foreignKey'    => 'postid',          
            'foreignKey'    => 'userid'     
                        )
                        );  

                            }

?>

Could you help me?

+3
source share
3 answers

Two foreign keys are fine, but you have to define relationships in the right models.

In your message model

var $hasMany = array( 
    'Reply' => array( 
        'className'     => 'Reply', 
        'foreignKey'    => 'postid',           
        //'foreignKey'    => 'userid'
                    ) 
                    );   

In your user model

var $hasMany = array( 
    'Reply' => array( 
        'className'     => 'Reply', 
        //'foreignKey'    => 'postid',            
        'foreignKey'    => 'userid'      
                    ) 
                    );   
+1
source

, CakePHP . CakePHP, , :

CREATE TABLE posts_tags (
  id INT(10) NOT NULL AUTO_INCREMENT,
  post_id INT(10) NOT NULL,
  tag_id INT(10) NOT NULL,
  PRIMARY KEY(id)); 

... , , . char (36). CakePHP 36- uuid (String:: uuid) , , Model:: save. ( CakePHP)

+1

cake bake - , Cake . db foreign_id. .

+1

Source: https://habr.com/ru/post/1752899/


All Articles