In cakephp 1.2 we were using mysqli without any error, but I think in cakephp2.0.6 this facility has been removed. Can anybody suggest me how we can use mysqli in cakephp2.0.6.
Following code throwing error.
'datasource' => 'Database/Mysqli',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'dev',
'prefix' => ''
The reason that Database/Mysqli doesn't work as your datasource is that Model/Datasource/Database/Mysqli.php doesn't exist, and doesn't need to exist.
CakePHP 2.x uses PDO handle database communication. There is little to no reason to use MySQLi over PDO, since PDO gives you access to nearly all of the MySQLi capabilites and more. Named parameter binding alone is reason enough to prefer it over MySQLi, not to mention platform-independence.
That said, the actual driver being used is irrelevant since Cake handles all of the database interactions for you. Unless you have hacked behind Cake's database abstraction layer in your app, just use Database/Mysql and your app should work perfectly.
In case you're curious, here's a short overview of PDO vs MySQLi.
Related
I have a problem using CakePHP with PHPUnit and Selenium and it is being INCREDIBLY difficult finding any help in the internet. I simply can't figure out how to identify in CakePHP that a request came from my Selenium agent, so that I can set the connection and database environment accordingly.
Any help would be highly appreciated! Further information regarding the best way to set my CakePHP app's database environment when requests come from Selenium are also most welcome.
The first step would be to set the user agent in Selenium to something your app would recognize as special. See the Selenium WebDriver Documentation.
Then in CakePHP you can use the global function env() to test the HTTP_USER_AGENT value. env is a wrapper for checking environment variables like $_SERVER.
For example in your database.php file:
var $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => '****',
'database' => 'production_db',
'prefix' => ''
);
function __construct() {
// set database connection settings for testing environment
if (stristr(env('HTTP_USER_AGENT'), 'selenium') {
$this->default['database'] = 'test_db';
}
}
If you can't set the user agent with Selenium, perhaps you could pass a get variable with the URL.
Example using Selenium extension for PHPUnit:
$this->setBrowserUrl('http://www.example.com?selenium=true');
In CakePHP you would access the variable in the $_GET array.
I've got a CakePHP based web app that needs some initial configuration. I'd like to do the equivalent of the mysql source command to set up a bunch of tables / initial rows, then execute a $this->User->save() command to create the root account (I think this needs to be done via code since it'll use the salt value for the local install of CakePHP, which might/should be different than the one on my dev machine), etc, etc.
My hack-y solution is to expose a public method on a controller that does this, direct my browser to it, then set stuff up (via the Configure::load and Configure::dump) so that the route from that URL to the method is removed after the installation is complete.
Does CakePHP provide any support for 'installing' a web app?
Part of my problem is that my attempts at Googling for "CakePHP web app installation" are all overshadowed by the various tutorials (etc) about how to install CakePHP itself. My issue is not installing CakePHP, it's providing an easy and safe way to set up the stuff my web app needs (like SQL database tables, etc) for it's particular needs.
It's called Cake Schemas....
The simplest thing you can do in your development environment is run the following via command line from root:
./app/Console/cake schema dump --write filename.sql
Which gives you a dump of your SQL file then you can edit the sql file directly before using it.
You specifically ask for running $this->User->save(), while learning about Schemas might be a bit complicated, you can accomplish this by running
./app/Console/cake schema generate
Which creates your schema.php, then:
App::uses('User', 'Model');
public function after($event = array()) {
if (isset($event['create'])) {
switch ($event['create']) {
case 'users':
App::uses('ClassRegistry', 'Utility');
$user = ClassRegistry::init('User');
$user->create();
$user->save(
array('User' =>
array(
'username' => 'admin',
'role' => 'admin',
'password' => 'admin'
)));
break;
}
}
}
Which makes a definition as you wish, then when you run:
./app/Console/cake schema create
Your tables get dropped, but remade as per your schema definitions and your model files, and with your specific "after" function
http://book.cakephp.org/2.0/en/console-and-shells/schema-management-and-migrations.html
I am working over Oracle DataBase and CakePHP 2.3.
As CakePHP doesn't support Oracle (there are no drivers for it), I am using Oracle procedures or php OCI8 functions in my models.
As a result of it, I am working with CakePHP without any effective database link in the eyes of CakePHP framework.
I am trying to use the Sanitize::clean method in order to clean a comment before saving it in the database and I am having troubles as it seems to look in the database for its task.
This is the resulting error:
Database connection "Mysql" is missing, or could not be created.
And this is how I try to sanitize it:
$comment = Sanitize::clean($this->request->data['comment']);
It works perfectly well if i just do this:
$comment = $this->request->data['comment'];
Is it possible somehow to use Sanitize::clean without any configured database at CakePHP 2.3?
Thanks
The function Sanitize::clean() expects 2 arguments, by default when you do not give a second argument CakePHP uses its default values and tries to connect to a DB with the 'default' connection. After a quick glance at the Sanitize Class in the library, it appears that it is the 'escape' option that requires a DB connection. It is called by default to make a string SQL-safe.
So in your case, as you don't need a SQL connection, this request should do the trick:
$comment = Sanitize::clean($this->request->data['comment'], array('escape' => false);
Check the CookBook for more information on the Sanitize class.
I am working on a legacy CakePHP 1.3 app and while I have years of experience with PHP, I am still finding my feet with CakePHP. I am following Chapter 4 'How to use the bakery' in Jamie Munro's Rapid Application Development with CakePHP, however the steps he suggests do not seem to go the way I'd expect them too.
I feel a good way of explaining this is going through the steps involved:
Following the books 'Hello World' example outlined in earlier chapters, I have setup a basic CakePHP app at this location on my machine: /home/public_html/helloworld.local. I can see the 'Hello World' example in a web browser on my local machine when I access: http://helloworld.local/users/add
Chapter 4 suggests that I move to this directory: home/public_html/helloworld.local/cake/console
I then run: ./cake bake
I get prompted to enter the location of the app and I add:
/home/public_html/helloworld.local/app
I then proceed to select defaults for the next few selections and there are no problems until I run into the line:
Your database configuration was not found. Take a moment to create one.
I do not understand this since there is a database file configured in ~/public_html/helloworld.local/app/config/database.php, and when I access the helloworld app outlined earlier (available on my local machine at http://helloworld.local/users/add), there is a database connection successfully established and records can be inserted.
I have also tried re-entering my database details when offered the chance to by cake bake, but end up with the error after succesfully adding the correct details:
Fatal error: Class 'DATABASE_CONFIG' not found in
/home/public_html/helloworld.local/cake/console/libs/tasks/db_config.php
on line 260
But either way, it should have found the existing database connection details, so not sure what is going on.
For using console command like cake bake you have to use your operating system terminal(for linux)/ command prompt(for windows). So you have to do the step mentioned in step 2 and 3 in you console. You can read documentation here to know how to use console commands.
Then, make sure that you have the file home/public_html/helloworld.local/app/config/database.php. I hope you removed .default from its name and rename it to database.php. To link up your database with your cakephp project you have to specify credentials in database.php.
var $default = array('driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'password',
'database' => 'database_name',
'prefix' => ''
);
I don't have a running CakePHP 1.3 installation here at hand, but this is what is happening at that location:
// #link: https://github.com/cakephp/cakephp/blob/1.3/cake/console/libs/tasks/db_config.php#L260
config('database');
$db = new $this->databaseClassName; // i.o.w. $db = new DATABASE_CONFIG;
This line:
config('database');
Does nothing more than including the database.php configuration file, simplified to;
include_once(CONFIGS . $arg . '.php'); // i.o.w. include_once(CONFIGS . 'database.php');
(https://github.com/cakephp/cakephp/blob/1.3/cake/basics.php#L77)
So IMO two problems may cause your error;
app/config/database.php was not found
You can try to check if this outputs the right path:
die(CONFIGS . 'database.php');
There is an error in your app/config/database.php, causing the DATABASE_CONFIG class to be malformed and unable to be initialized
A word of notice
Aparently your running 'bake' for everything including setting up a new database configuration. This may overwrite your
existing database configuration. It is possible to bake only parts of your application (e.g. bake controllers only or models).
The manual on baking in CakePHP 1.3 is located here:
http://book.cakephp.org/1.3/en/The-Manual/Core-Console-Applications/Code-Generation-with-Bake.html
And this
If this is your first CakePHP project, you should realy consider the option to upgrade to CakePHP 2.x CakePHP 1.3 is really outdated and, although it's still able to run fine, I wouldn't invest too much time in 1.3 as a lot of things have changed in CakePHP 2.x. It's probably better to start with CakePHP 2.x then to start with 1.3 and learn things that no longer work in CakePHP 2.
Copy your app/Config folder to app/Console, so the final path would be app/Console/Config. That worked for me.
I have read on alot of threads here on stackoverflow and also other tutorials on the internet but can't get this to work!
I have just setup a cake project, (used it in the past v1.3.7), and everything passes besides the database connection, i get the error message: Cake is NOT able to connect to the database.
I have already gotten pdo_mysql up and running so i know thats not the problem.
And i also know that the user and password can connect to the database thru the given ip/socket that i have tried.
This is the config that im trying to use:
public $Phenomenon = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake',
'password' => 'theCakePass',
'database' => 'Phenomenon',
);
(Also tried 127.0.0.1 instead of localhost)
So my question is: How can i get more verbose error messages so i can find out whats wrong? Or simply how do i solve this?
System Specs:
Mac OS X 10.7
Apache/2.2.20
PHP 5.3.9
Try connecting using vanilla PDO (not using cake); does that work?
And are you using Cake 2.0.5?