| |
MySQL And CakePHP: A Match Made In Heaven
By Qushawn Clark
Expert Author
Article Date: 2012-01-18
CakePHP is a PHP framework that is become more and more popular because it is so easy to use and makes that task of building web applications a much less daunting task.
Many of you out there use Cake and know the benefits, but those of you who don't use it or haven't been using it long need to be enlightened. First off, you should get and proceed to set up CakePHP, because its awesome, and all of us PHP programmers should know it. One of the main benefits, as you can probably tell from the title of the article, is that it makes using MySQL a cinch. Assuming you already have MySQL server already set up on your machine, you would have to get to the /Config/database.php.default file and make a few changes to get things working. The first thing you should do is make a copy of that file in the same directory and remove the "default" from the end of the filename. Next, go ahead and open it up and you should see something like this:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Now, go ahead and fill in the necessary information, which consists of the hostname, database password, user, and database name. Once again, im assuming that all of this stuff already exists in MySQL. Once that is done, you will be able to access the database very easily from pretty much anywhere in your application. Normally with PHP its necessary to use the mysql_query function and type out the whole query by hand and make sure that everything is perfect. If you have to do a lot of them, it can be a real pain in the you-know-what, but with Cake you just do something like this:
$this->Thing->find();
or this:
$this->Thing->save();
Of course, you are allowed to elaborate on these statements as much as you want, such as being able to specify whether you want your results in a list based on primary keys, or if you wish to grab all of the information for each record. You can also specify specific fields to grab, and also other specific conditions for the query. This is just some basic information, but maybe next time I'll show you all some cool things you can do with this stuff.
About the Author:
Qushawn is a staff writer for the iEntry Network.
|
|