This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

kelvin
kelvin

Hello all.

I was wondering if it is possible to use relations with multiple database connections. The concept is that the backend_users and all system tables are in the "main" database. Another database will be dedicated to a particular plugin being developed.

When using a relation to the backend_users it generates a SQL error because it doesn't prefix the table with the correct database. I have even tried extending that class and supplying the connection directly.

Below is the SQL error occurring.

Base table or view not found: 1146 Table 'plugindb.backend_users' doesn't exist (SQL: select notes.*, (select concat(last_name, ', ', first_name) from contacts where notes.contact_id = contacts.id) as contact, (select concat(last_name, ', ', first_name) from backend_users where notes.owner_id = backend_users.id) as owner from notes where notes.client_id = 2 and notes.client_id is not null order by created_at desc)

If this functionality does not exist then I can post something in the feature requests or look into changing this if someone could point me in the right path. Thanks in advance!

tekjava
tekjava

Not sure if this will help but check out the "Multiple database connections" section here: http://octobercms.com/docs/database/basics

When using multiple connections, you may access each connection via the connection method on the Db facade. The name passed to the connection method should correspond to one of the connections listed in your config/database.php configuration file:

$users = Db::connection('foo')->select(...);

You may also access the raw, underlying PDO instance using the getPdo method on a connection instance:

$pdo = Db::connection()->getPdo();
ichakesachin3010215
ichakesachin3010215

Hi, I am facing problem when validating form fields,

Look at following snippet: class User extends UserBase { public $connection = 'plugindb'; /**

@var string The database table used by the model. */ protected $table = 'users';

/**

Validation rules */ public $rules = [ 'name' => 'required', 'surname' => 'required', 'email' => 'required|between:3,64|email|unique:db_plugin.users', 'mobile' => 'numeric', 'password' => 'required:create|between:6,16|confirmed', 'password_confirmation' => 'required_with:password|between:6,16', ];

To validate email as unique, i need to use "db_plugin" database name as suffix with table otherwise it throws "Exception". But I want to do it dynamic like database name will be available depend on connection using. If I removed database name then it takes default database which application using.

Please suggest best approach to implement it in correct way?

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.