CakePHP 2.3 with SqlServer on debian squeeze - cakephp

I'm confused about all the requirements to get cakephp 2.x to talk to MSSQL (2k8R2). I'm running debian squeeze and installed php5-sybase.
I'd be using this as a second datasource defined as
public $qadb = array(
'datasource' => 'Database/Sqlserver',
'persistent' => false,
'host' => 'xxx.xxx.xxx.xxx',
'login' => 'myuser',
'password' => 'mypass',
'database' => 'mydb',
'prefix' => '',
//'encoding' => 'utf8',
);
When I'm trying to run cake bake with this datasource I get
Error: Database connection "Sqlserver" is missing, or could not be created.
I looked at http://www.php.net/manual/en/ref.pdo-sqlsrv.php which references installing ODBC for linux, but I thought the sybase package would take care of what I need. Any further info anyone has on this would be appreciated.

Related

How to I specify Tinker to use a different database connection?

I have two database connections. One for my application and another for testing.
In my ..\config\database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'testing' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', 'localhost'),
'database' => env('DB_TEST_DATABASE', 'forge'),
'username' => env('DB_TEST_USERNAME', 'forge'),
'password' => env('DB_TEST_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am able to change the database connection in seeding using
php artisan db:seed --database=testing
I wanted to use tinker for the connection 'testing' but unable to change it. Is there any way to change the database connection for tinker similar with database seeding?
As your question starts with using one database for testing/development and one for production, you should look into using different environments, this will allow you to have no change in your code between deployment & local testing.
This task can easily be achieved by specifying your environment:
php artisan tinker --env=local
By default, if you specify no --env, you will be using /your-app/.env
When using local you read variables from /your-app/.env.local
For your specific use case:
php artisan db:seed --env=local
Further reading for Laravel 5.1: https://laravel.com/docs/5.1/configuration
Latest version: https://laravel.com/docs/configuration
NB: You should avoid checking in the ".env" file to VCS, the .env.local should be OK to share, but it is best practice to not bundle production credentials with your VCS.
To set the default database connection to 'mysql_test' from within tinker I use this command:
>>> use DB
>>> DB::setDefaultConnection('mysql_test');
It is especially useful when you want to test your migrations and seeders without messing up your existing (working) local database.
Change default connection
$model_instance = new App\YourModel();
$model_instance->setConnection('new_connection');
$data = $model_instance->find(1);

Cakephp 3 Bake all issue with MSSQLSRV

I am using Cakephp 3 and MSSQLSRV 2014. I made all the necessary changes to connect to MSSQL server. In GUI, I can see that cakephp can connect to MSSQL. Please see the below screenshot.
So now when I go to bin directory to bake the application, I get below error unable to load MSSQL driver, which is already installed:
Here are my datasource settings in app.php file:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlserver',
'persistent' => false,
'host' => 'localhost\SQLEXPRESS',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
'port' => '1433',
'username' => 'sa',
'password' => 'password',
'database' => 'ServerMatrix',
'encoding' => 65001,
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
I am thinking its probably a bug that should be reported to CakePHP community, but wanted to get some help from stack-overflow community to see if they have encountered such issue.
In command line interface typed php --ini and opened loaded php.ini file and added SQLSrv extensions. That solved the problem for baking an application.
Thanks to #ndm.

mysql_real_escape_string is not working in cakephp

mysql_real_escape_string is not working in cakephp .
I am getting error like below .
mysql_real_escape_string(): Access denied for user 'root'#'localhost' (using password: NO) [APP/Controller/add.php, line 123]
database connection:
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xyz',
'password' => 'password',
'database' => 'xyz',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xyz',
'password' => 'password',
'database' => 'xyz',
'prefix' => '',
//'encoding' => 'utf8',
);
}
?>
<?php
$price1=implode("'~'",array_map('mysql_real_escape_string',$this->request->data['iupdate']['price']));
?>
localhost its working fine but in server getting an error .
I don't know cake php, but IMHO, you just cannot use mysql_real_escape_string, because:
firstly, it is deprecated.
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
secondly, according to php doc :
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments.
So, you are trying to connect to your production server via the values set in the php.ini and of course, you cannot connect with root privileges for security reasons.
As I told you: I don't know cakePHP, but I am pretty sure, there is a function to escape the strings, or -better- the strings are automatically escaped using a PDO prepare statement
If you found this during installing cakephp 2.0 and more then replace 'login' => 'xyz', with 'username' => 'xyz'.
Simple trick solves it all.

How can I connect to second database with my cakephp application

my application's requirement is that to display data of magento database table forexample admin_user that is reside at locally connected pc.
so I need to keep as it is my cakephp database values in displaying-modifying-etc, and in only one page magento's database value needs to be printed and updated.
I have kept 2 variables in databse.php
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'myappdatabase',
'prefix' => '',
//'encoding' => 'utf8',
);
var $vsdatabase = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '192.168.1.36',
'login' => 'root',
'password' => '',
'database' => 'magento',
'prefix' => '',
);
and in controller,
App::import('Model','ConnectionManager');
$db = ConnectionManager::getDataSource('vsdatabase');
$database = $db->config['database'];
$data = $this->User->query("select * from $database.admin_user as t1");
the host I like to keep as written above means default is from my local database and other is remote PC's magento database
If I both host keep same then its working but if write different then It's not working
So I solve this problem?
Plz help me in finding out solution
What you do is plain wrong and even if you would use that code it should belong into a model not a controller.
Simply create a new model named after the magento table, but I would prefix it with magento or something. The model has to be configured to use this db connection.
class MagentoUser extends AppModel {
public $useDbConfig = 'vsdatabase';
}
You can also init models with other aliases and data sources on the fly using ClassRegistry. See http://api20.cakephp.org/class/class-registry#method-ClassRegistryinit
And stay away from using plain SQL queries, you will very likely ending up writing insecure queries and lose other features of the CakePHP ORM.

Using SQLite3 with CakePHP 2.0

I'm trying to run SQLite3 with CakePHP 2.0
In these questions I saw that it's possible to do that in CakePHP 1.3:
- Using Sqlite3 with CakePHP
- How do I connect CakePHP to a SQLite database?
However, the solutions are not valid for CakePHP 2.0.
I configured the file 'database.php' and I got success on the starting page of CakePHP. It was able to connect to the database (but I do not know where to find the .db3 database file).
I used the following code:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_blog_tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);
I'm trying to find out:
Where should my cake_blog_tutorial.db3 file be kept
Is the datasource different for SQLite3, for example 'Database/Sqlite3'?
Thank you for your help!
In short, the answer is that Sqlite3 databases in CakePHP 2.0 take something like the following configuration:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
The sqlite file is then automatically created in the webroot directory (unless you prepend a relative path to the database name).
Incidentally, you can use in-memory Sqlite databases (for testing purposes, for example) by changing the database name to ":memory:", e.g.:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'database' => ':memory:',
'prefix' => '',
//'encoding' => 'utf8',
);

Resources