How can I connect to second database with my cakephp application - cakephp

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.

Related

Existing database integration with Laravel 5.0

I am new to laravel. I've got the whole database designed in phpmyadmin. Now I want to integrate the existing database in Laravel. Is there any way to do that? If so then do I have to create models?
Yes, you can use your database in laravel but at first you have to provide your database credentials to allow the framework/Laravel to access your database. So, you can use a .env file or simply you can use the config/database.php to provide credentials, for example, to use your mySql database all you need to setup the database configuration as follows:
'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'your_database_name'),
'username' => env('DB_USERNAME', 'your_database_username'),
'password' => env('DB_PASSWORD', 'your_database_password'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
]
Then regarding your second question, yes, you have to create models for each table or you can use DB facade to run queries directly from your controllers (not recommended). For example, to access your posts table you can create a Post model in app folder which may look something like this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
// If your table name is other than posts
// then use this property to tell Laravel
// the table name.
protected $table = 'posts';
}
Then you can use something like this:
$posts = \App\Post::all(); // Get all posts
$post = \App\Post::find(1); // Get the post with id 1
If you don't want to use Eloquent Model like one I've created above then you may use something like this:
$posts = \DB::table('posts')->get(); // Get allp posts
These are some basic examples to get you start. To learn more about the use cases of models/database you should visit Laravel website and read the manual (find Database section and check Query Builder and Eloquent).

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.

cakephp error. Connection to database ...not ...: Access denied for user 'my_app'#'localhost' (using password: YES)

I am trying to start CakePHP. I made bookmarker and tested by command bin\cake server. It showed one error as connection to database could not be established:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost' (using password: YES).
I read the config/app.default.php file. It says there is a database my_app and another database test_myapp with some users. I can not find these databases in phymyadmin in xampp. Am I supposed to create the named databases and users manually? I thought CakePHP should create these automatically. Or should I give names of databases etc. which I like and create the same.I'm using xampp with windows 7 and am very new to CakePHP.
Cake will not create the database or database user for you. You need to create them yourself and then match these database credentials into the db config file. Your datasource in app/Config/app.php file should look something similar to:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'my_db_user',
'password' => 'my_db_user_password',
'database' => 'cake_database_name',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]
],
In this example you would have to create a database named: cake_database_name, and then add a database user named my_db_user with the password of my_db_user_password.
Edit Config/app_local.php file instead of Config/app.php with your database connection parameters, i.e. host, username, password, database name. This will solve the problem.
Be sure to create same database first and providing enough privileges to same user you're writing in app_local.php file.
See in Mysql.php:
class Mysql extends Driver
{
use MysqlDialectTrait;
use PDODriverTrait;
/**
* Base configuration settings for MySQL driver
*
* #var array
*/
protected $_baseConfig = [
'persistent' => true,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'cake',
'port' => '3306',
'flags' => [],
'encoding' => 'utf8',
'timezone' => null,
'init' => [],
];
Then in app.php write
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* 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' => 'non_standard_port_number',
'username' => 'root',
'password' => '',
'database' => 'cake',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
...],
In phpMyAdmin
create cake database.
It's all.
Edit Config/app_local.php file with your database connection parameters. You will find host, username, password, database name their.
don't edit config/app.php with database connection parameters.
Your problem will be now solved
CakePHP 4.x
Error:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost'
(using password: YES).
Reason:
No user id: "my_app" with password: "secret" [or any other] set in config/app.php
file.
Solution:
In case of WAMP, go to phpmyadmin page, then we can select cake_cms
database -> privilage-> create user id and password, same as in config
file config/app.php, refresh the http://localhost:8765/ page.
it should now show
CakePHP is able to connect to the database.
If you are using wamp, make sure port is uncomment in Datasources and 'port' => '3308'
I solved this problem by checking port at phpmyadmin top
and uncommit below line in app_local.php
'port' => '3308',
CakePHP3.9.3,app_local.php,'username`=>'root','password'=>'','database' => 'db name'

Multiple applications can share the same ACL tables?

Currently I have to share my database with many CakePHP applications and the tables are prefixed to identify each application respectively.
So, can many applications share the same ACL tables?
Or, I could change the default names of the tables ACL and add the prefix of each application, eg. app_aros, app_acos, app_aros_acos?
/* beforeFilter() # AppController */
$this->Acl->Aro->useTable = 'app_aros';
$this->Acl->Aco->useTable = 'app_acos';
This code worked but I haven't found a way to change the tablename of the model Permission...
Suggestions? What could I do?
You have change this line in app/Config/core.php in all of your apps
Configure::write('Acl.database', 'default');
to:
Configure::write('Acl.database', 'your_acl_connection');
And also add connection in app/Config/database.php
example:
public $your_acl_connection = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
Simply you will have one DB for managing all yours app ACLs.
In this DB you create all ACL tables.

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