Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Read the DB Configuration Settings From a Cake Shell?

Tags:

cakephp

I would like to write a cake shell to do a nightly backup of my database using mysqldump. I could do this as a shell script, but if I can cram it into a CakePHP shell, then I will get the added benefit of it working across both the development and live server, if I can get it to automagically read my database config settings. I will cron the cake shell and have some peace-of-mind knowing that I have frequent backups of my DB.

In my shell I'm trying to build up a string which starts with "mysqldump --user=" and I'd like to get the username from app/config/database.php. How can I get at the data in database.php?

like image 306
Randy L Avatar asked Oct 09 '10 04:10

Randy L


People also ask

What is shell in CakePHP?

The CakePHP console provides a framework for creating shell scripts. The Console uses a dispatcher-type setup to load a shell or task, and provide its parameters. Note. A command-line (CLI) build of PHP must be available on the system if you plan to use the Console.

How does CakePHP connect to multiple databases?

First open app/Config/database. php file in any code editor. The default database is public $default , if you want to use other database(db2, db3) you need to initialize new database in your Model using $useDbConfig predefined cakephp method see example.

How do you run the shell cake command?

If your shell class is in the right place, then it might be a problem that cake does not know where your app root is. You can specify this using the -app argument. Show activity on this post. Your cron command basically calls cd into the app directory and the cake command to run the shell together.


1 Answers

In cake 2.1 the format has changed to:

App::uses('ConnectionManager', 'Model');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];
like image 181
Morgan O'Neal Avatar answered Oct 06 '22 22:10

Morgan O'Neal