CakePHP Naming and Combining Conventions

Just a few days ago, I found out about this miracle called CakePHP , so I'm pretty green. I need to create a mail application, so I executed the agreement and created:

Database Description:

User table <user_id (primary key), fname, lname>.

The table of mail messages <mail_id (primary key), from (foreign key to user_id), to (foreign key for user_id), contents, open>.

My questions:

1) According to the agreement, the foreign key should be called a linked table + '_ id'. How can I call columns if there are two foreign keys that belong to the same table. Both from the table and from it.

2) I would like to make an internal JOIN between two tables. Sort of:

SELECT user_id, mail_id 
FROM users
INNER JOIN mails
ON users.user_id =mails.to AND mails.opened=false. 

, .

+3
2

, . 2 . sender_id recipient_id. :

<?php

class Mail extends AppModel {
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'UserSender' => array(
            'className' => 'User',
            'foreignKey' => 'sender_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
            'UserRecipient' => array(
            'className' => 'User',
            'foreignKey' => 'recipient_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
    );
}
?>

, , :

<?php
    $this->Mail->find(array('conditions'=>array('Mail.opened'=>false)));
?>

... :

<?php
    $this->Mail->find(array('conditions'=>array('UserSender.some_field'=>$someValue,
                                                'UserRecipient.some_field'=>$someValue)));
?>
+4

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


All Articles