简体   繁体   中英

CakePHP - HABTM and different databases for models

Lets say I have two different databases with the following tables:

Auth database:

users

App database:

fanpages
fanpages_users

Obviously my User model uses the Auth database, but how do I force HABTM relation to use the App database instead of the Auth database?

The error I am getting states:

Table fanpages_users for model FanpagesUser was not found in datasource Auth.

You need to create another model called FanspagesUser and define $hasMany relation on User model and on Fanpage model.

FanpagesUser.php

    <?php
    class FanpagesUser extends AppModel{
        public  $useDbConfig = 'your database configuration on databases.php';
        public $belongsTo = array('User','Fanpage');

    }

Fanpage.php

    <?php
    class Fanpage extends AppModel{
        public  $useDbConfig = 'your database configuration on databases.php';
        public $hasMany= array('FanpagesUser');

    }

User.php

    <?php
    class User extends AppModel{
        public  $useDbConfig = 'your database configuration on databases.php';
        public $hasMany= array('FanpagesUser');

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM