1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/manual/docs/en/getting-started/working-with-existing-databases.txt

155 lines
3.4 KiB
Plaintext
Raw Normal View History

2007-06-13 02:18:21 +04:00
+++ 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:
<code type="sql">
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))
</code>
Now we would like to convert it into Doctrine record class. It can be achieved easily with the following code snippet:
<code type="php">
require_once('path-to-doctrine/lib/Doctrine.php');
2007-06-13 02:18:21 +04:00
spl_autoload_register(array('Doctrine', 'autoload'));
Doctrine_Manager::connection('mysql://root:dc34@localhost/test');
2007-06-13 02:18:21 +04:00
// import method takes one parameter: the import directory (the directory where
// the generated record files will be put in
Doctrine::generateModelsFromDb('myrecords');
2007-06-13 02:18:21 +04:00
</code>
That's it! Now there should be a file called File.php in your myrecords directory. The file should look like:
<code type="php">
2007-06-13 02:18:21 +04:00
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
2008-01-23 01:39:59 +03:00
abstract class BaseFile extends Doctrine_Record
2007-06-13 02:18:21 +04:00
{
2008-01-23 01:39:59 +03:00
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,
));
}
2007-06-13 02:18:21 +04:00
}
</code>
+++ Import options