Monday, October 29, 2012

Testing database connection on the fly

Hi, I'm trying to perform a button in one of my views where a user (an admin) can perform a database connection test on the fly, I have an action where I have the following code and I get the error "Database connection "Sqlserver" is missing, or could not be created."

The thing is that I don't want to assign that connection to a model ($useDbConfig), I just want to test the given config parameters. Can somebody help me please? I'm with Cake 2.2.3 and a working MSSQL implementation. Thanks in advance

public function testconnection() {
$this->layout = false;
// Connection settings
$conn = 'test';
$config = array();
$config['datasource'] = 'Database/Sqlserver';
$config['persistent'] = false;
$config['host'] = $this->request->data['DbServer'];
$config['login'] = $this->request->data['DbUser'];
$config['password'] = $this->request->data['DbPassword'];
$config['database'] = $this->request->data['DbName'];
if (isset($this->request->data['DbPort'])) {
$config['port'] = $this->request->data['DbPort'];
} else {
$config['port'] = 1433;
}
// connects to database
App::import('Model', 'ConnectionManager');
try {
$connected = ConnectionManager::create($conn, $config);
} catch (Exception $connectionError) {
$connected = false;
}
if ($connected && $connected->isConnected()) {
$this->set('res', 1);
} else {
$this->set('res', $connectionError->getMessage());
}
ConnectionManager::drop($conn);
}

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: