Introduction
In order to produce your first application, you need to
know how to communicate with your database.
To communicate with it, the database must be
configured properly for use with your framework.
And before the configuration can be made, you
should know how the frameworks join with database
systems and choose your preferred object-relational
mapping (ORM) solution.
ORM
Object-relational mapping (ORM) is a programming
technique which allows you to map your database
tables into PHP objects.
Although there are plenty of good ORM frameworks
out there, it is definitely useful to learn how to build
at-least one among them .
In case youre not sure what ORM really is, just
think of it as having a PHP object per table in your
database.
If you have a user table, then you would have
a user class which would then allow you to add,
update and delete user data from your table.
Consider an example
What table are we mapping?
For example, we will assume we have a users table in
our database which we want to map into an ORM class.
Lets have a look at what fields our users table has:
id
: int
: primary key
name
: varchar
surname : varchar
email
: varchar
CAKE PHP
In CakePHP, you configure the database with the
DATABASE_CONFIG class.
CakePHP expects database configuration details to be in
a file at app/Config/database.php.
It has a $default variable, which is an array containing
all necessary information about the default environment.
class DATABASE_CONFIG
{
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cakephpuser',
'password' => 'c4k3roxx!',
'database' => 'my_cakephp_project');
}
datasource :
The name of the datasource this configuration array is for.
Examples: Database/Mysql, Database/Sqlserver,
Database/Postgres, Database/Sqlite.
Persistent :
Whether or not to use a persistent connection to the
database.
host :
The database servers hostname (or IP address).
login:
The username for the account.
password:
The password for the account.
database:
The name of the database for this connection to use.
Zend Framework:
In Zend Framework you have to choose the specific
driver i.e., Zend_Db.
Zend_Db and its related classes provide a
simple SQL database interface for Zend Framework.
The Zend_Db_Adapter is the basic class that is used to
connect your PHP application to an RDBMS.
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = login
resources.db.params.password = secret
resources.db.params.dbname = dbname