1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/manual/docs/en/getting-started/starting-new-project.txt

42 lines
1.7 KiB
Plaintext
Raw Normal View History

2007-06-13 02:18:21 +04:00
Doctrine_Record is the basic component of every doctrine-based project. There should be atleast one Doctrine_Record for each of your database tables. Doctrine_Record follows the [http://www.martinfowler.com/eaaCatalog/activeRecord.html Active Record pattern]
2007-08-02 00:05:50 +04:00
Doctrine always adds a primary key column named 'id' to tables that doesn't have any primary keys specified. Only thing you need to for creating database tables is defining a class which extends Doctrine_Record and setting a setTableDefinition method with hasColumn() method calls and by exporting those classes.
2007-06-13 02:18:21 +04:00
2007-08-02 00:05:50 +04:00
Lets say we want to create a database table called 'user' with columns id(primary key), name, username, password and created. Provided that you have already installed Doctrine these few lines of code are all you need:
2007-06-13 02:18:21 +04:00
2007-08-02 00:05:50 +04:00
User.php :
2007-08-02 00:08:53 +04:00
<code type="php">
2007-08-02 00:05:50 +04:00
class User extends Doctrine_Record
{
public function setTableDefinition()
{
2007-06-13 02:18:21 +04:00
// set 'user' table columns, note that
2007-08-02 00:08:53 +04:00
// id column is auto-created as no primary key is specified
2007-06-13 02:18:21 +04:00
2007-08-02 00:05:50 +04:00
$this->hasColumn('name', 'string',30);
$this->hasColumn('username', 'string',20);
$this->hasColumn('password', 'string',16);
$this->hasColumn('created', 'integer',11);
2007-06-13 02:18:21 +04:00
}
}
</code>
2007-08-02 00:05:50 +04:00
For exporting the user class into database we need a simple build script:
<code type="php">
//require the base Doctrine class
2007-08-02 00:05:50 +04:00
require_once('lib/Doctrine.php');
//register the autoloader
2007-08-02 00:05:50 +04:00
spl_autoload_register(array('Doctrine', 'autoload'));
require_once('User.php');
//set up a connection
Doctrine_Manager::connection('mysql://user:pass@localhost/test');
2007-08-02 00:05:50 +04:00
//export the classes
Doctrine::createTablesFromArray(array('User'));
2007-08-02 00:08:53 +04:00
</code>
2007-06-13 02:18:21 +04:00
We now have a user model that supports basic CRUD opperations!