+++ Introduction
A common case when looking for ORM tools like Doctrine is that the database and the code that access it is growing large/complex. A more substantial tool is needed then manual SQL code.
Doctrine has support for generating Doctrine_Record classes from your existing database. There is no need for you to manually write all the Doctrine_Record classes for your domain model.
+++ Making the first import
Let's consider we have a mysql database called test with a single table called 'file'.
The file table has been created with the following sql statement:
CREATE TABLE file (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
name VARCHAR(150),
size BIGINT,
modified BIGINT,
type VARCHAR(10),
content TEXT,
path TEXT,
PRIMARY KEY(id))
Now we would like to convert it into Doctrine record class. It can be achieved easily with the following code snippet:
require_once('path-to-doctrine/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
Doctrine_Manager::connection('mysql://root:dc34@localhost/test');
// import method takes one parameter: the import directory (the directory where
// the generated record files will be put in
Doctrine::generateModelsFromDb('myrecords');
That's it! Now there should be a file called File.php in your myrecords directory. The file should look like:
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
abstract class BaseFile extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('file');
$this->hasColumn('id', 'integer', 4, array (
'alltypes' =>
array (
0 => 'integer',
),
'ntype' => 'int(10) unsigned',
'unsigned' => 1,
'values' =>
array (
),
'primary' => true,
'notnull' => true,
'autoincrement' => true,
));
$this->hasColumn('name', 'string', 150, array (
'alltypes' =>
array (
0 => 'string',
),
'ntype' => 'varchar(150)',
'fixed' => false,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('size', 'integer', 8, array (
'alltypes' =>
array (
0 => 'integer',
),
'ntype' => 'bigint(20)',
'unsigned' => 0,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('modified', 'integer', 8, array (
'alltypes' =>
array (
0 => 'integer',
),
'ntype' => 'bigint(20)',
'unsigned' => 0,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('type', 'string', 10, array (
'alltypes' =>
array (
0 => 'string',
),
'ntype' => 'varchar(10)',
'fixed' => false,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('content', 'string', null, array (
'alltypes' =>
array (
0 => 'string',
1 => 'clob',
),
'ntype' => 'text',
'fixed' => false,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('path', 'string', null, array (
'alltypes' =>
array (
0 => 'string',
1 => 'clob',
),
'ntype' => 'text',
'fixed' => false,
'values' =>
array (
),
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
}
}
+++ Import options