Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObject specifing a connection name


Go to End


2 Posts   864 Views

Avatar
Alexw

Community Member, 2 Posts

15 December 2013 at 4:51am

Edited: 15/12/2013 4:53am

Hello,

I have a suggestion about the features of DataObjects. What about adding the ability to specify a connection name that a specific DataObject should need to use. There are situations that some tables are managed by Silverstripe but are located somewhere else or at different database name.

_config.php:

$db = new MySQLDatabase(array(
		'server' => 'localhost',
		'username' => 'myUsername',
		'password' => 'myPassword',
		'database' => 'myDatabase'
	));
DB::setConn($db, 'external');

code/MyDataObject.php

class MyDataObject extends DataObject
{
	public static $conn = 'external';

	public static $db = array(
		'IsActive' => 'Boolean'
	);

	public function getCMSFields()
	{
		return new FieldList(
			new CheckboxField('IsActive')
		);
	}
}

Kind regards,
Alexw

Avatar
martimiz

Forum Moderator, 1391 Posts

24 December 2013 at 1:58am

Edited: 24/12/2013 1:58am

Hi,

Thanks for willing to contribute to SilverStripe! Propositions won't always be picked up from within these forums though. Please check out this documentation on how to contibute: http://doc.silverstripe.org/framework/en/trunk/misc/contributing/code

In the mean time, as to multiple db connections, there are some solutions/workarounds around. For example: http://www.silverstripe.org/customising-the-cms/show/13692

I remember a discussion about this a short while ago in the core developers group (https://groups.google.com/d/forum/silverstripe-dev) but I can't seem to find it just now...

Basically afaik you cannot work with multiple connections at the same time, but you can switch between connections if need be.

Martine