How to use multiple foreign keys in one table, referencing another table in cakephp

Using cakephp, I have a common address table that I want to associate with customers, sellers, contacts. most tables only have 1 to 1 relationships, but I want my clients table to have 2

possible to clarify: I have a customer table

id, int
mailing_address_id, int
billing_address_id, int

and address table

id,int
addr, varchar
city, varchar
etc....

Now I know what I can put customer_idin the address table. But I do not want to do this, because I have a table of suppliers, a table of contacts and other tables that everyone will use in the table of addresses. customer_idactually will not match other tables.

I would like the Customer model to automatically bind in two addresses

+3
4

, , .

Addresses, table_id. table_id "", "", "" , .

entity_id. , .

, , , $conditions:

'Address.entity_id'=>'123456'
'Address.table_id'=>'vendor'
'Address.type'=>'billing'

, , Addresses.

+1

, .

, , , . , .

<?php

class Customer extends AppModel {
    var $name = 'Customer';                
    var $belongsTo = array(
        'BillingAddress' => array(
            'className'    => 'Address',
            'foreignKey'    => 'billing_address_id'
        ),
        'MailingAddress' => array(
            'className'    => 'Address',
            'foreignKey'    => 'mailing_address_id'
        )
    );  
}
?>

, . - , customer_id, company_id, employee_id .. , , .

, . , . " " .

+1

, , , .

, , , , ( ). $hasMany . (, ), $conditions, 'Address.type' = > 'billing'.

, Travis

0

HASMANY, has-many:

http://book.cakephp.org/view/82/hasMany

HASONE ( ) , has-one:

http://book.cakephp.org/view/80/hasOne

If clients can have the same address (the same entry), you will need to use the HABTM with the connection table that you referenced.

More information about the Cakes Association: http://book.cakephp.org/view/78/Associations-Linking-Models-Together

0
source

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


All Articles