Multiple Database Relationships in a Non-Primary Key Field in CakePHP

I have a project that necessarily covers several databases.

There are tables in one database:

CREATE TABLE device {
  device_uid integer unsigned not null primary key auto_increment,
  os varchar(50),
  name varchar(50)
}

CREATE TABLE platform {
  platform_id integer unsigned not null primary key auto_increment,
  name varchar(50)
}

Another database has a table:

CREATE TABLE oses_platforms {
  platform_id integer unsigned not null primary key auto_increment,
  os varchar(50),
  platform_id integer unsigned
}

I created Models for tables and relationships:

<?php
class Platform extends AppModel {

    var $name = 'Platform';
    var $useTable = 'platform';
    var $primaryKey = 'platform_id';
    var $useDbConfig = 'otherdb';

    var $hasOne = array(
        'OsesPlatform' => array(
            'className' => 'OsesPlatform',
            'foreignKey' => 'platform_id'
        )
    );
}

class Device extends AppModel {

    var $name = 'Device';
    var $primaryKey = 'device_uid';
    var $useDbConfig = 'otherdb';        

}

class OsesPlatform extends AppModel {

    var $useDbConfig = 'default';
    var $name = 'OsesPlatform';
    var $primaryKey = 'os';


        var $belongsTo = array(
                'Platform' => array(
                        'className' => 'Platform',
                        'foreignKey' => 'platform_id',                            
                ),
        );

        var $hasMany = array(
                'Device' => array(
                        'className' => 'Device',
                        'foreignKey' => 'os',
                        'dependent' => false,                            
                )
        );
}
?>

If all three tables are in "default" or "otherdb", I can do this in the "condition" argument for hasOne or belong to a device-to-OsesPlatform relationship, and in fact the hasOne relationship between Platform and OsesPlatform works fine, However, this relationship is between device and platform that I need to simulate.

+3
source share
3 answers

, HABTM Cake ( markstory #cakephp). , ( ), , . -.

+1

cakeapp.com. http://cakeapp.com/sqldesigners/sql/oses

: http://oses.cakeapp.com/cake/oses/devices/add, .

: "oses". cakeapp.com , # 3: " CakeApp SQL"

Maybe this will help you?

0
source

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


All Articles