1
0
mirror of synced 2024-12-17 08:28:43 +03:00
doctrine2/manual/new/docs/en/real-world-examples/user-management-system.txt
jepso 9b61957154 - New feature in documentation: you can now link to other documentation sections with the following syntax:
- [doc getting-started:installation], or
    - [doc getting-started:installation Custom link text]
- Updated Text_Wiki to 1.2.0
- Documentation should now pass XHTML validator
- Formatted DSN section so that it's easier on eyes
- The single quotes in <code type='php'> won't work anymore due to the Text_Wiki update. Use double quotes instead: <code type="php">. The single quotes have been converted to double quotes in documentation files.
- Modified the links in h1-h6 headings to use the same style as the headings.
- Some refactoring
2007-07-20 08:03:04 +00:00

103 lines
2.8 KiB
Plaintext

In the following example we make a user management system where
# Each user and group are entities
# User is an entity of type 0
# Group is an entity of type 1
# Each entity (user/group) has 0-1 email
# Each entity has 0-* phonenumbers
# If an entity is saved all its emails and phonenumbers are also saved
# If an entity is deleted all its emails and phonenumbers are also deleted
# When an entity is created and saved a current timestamp will be assigned to 'created' field
# When an entity is updated a current timestamp will be assigned to 'updated' field
# Entities will always be fetched in batches
<code type="php">
class Entity extends Doctrine_Record
{
public function setUp()
{
$this->ownsOne('Email', 'Entity.email_id');
$this->ownsMany('Phonenumber', 'Phonenumber.entity_id');
$this->setAttribute(Doctrine::ATTR_LISTENER, new EntityListener());
}
public function setTableDefinition()
{
$this->hasColumn('name', 'string', 50);
$this->hasColumn('loginname', 'string', 20);
$this->hasColumn('password', 'string', 16);
}
}
class Group extends Entity
{
public function setUp()
{
parent::setUp();
$this->hasMany('User', 'Groupuser.user_id');
$this->setInheritanceMap(array('type'=>1));
}
}
class User extends Entity
{
public function setUp()
{
parent::setUp();
$this->hasMany('Group', 'Groupuser.group_id');
$this->setInheritanceMap(array('type'=>0));
}
}
class Groupuser extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('group_id', 'integer');
$this->hasColumn('user_id', 'integer');
}
}
class Phonenumber extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('phonenumber', 'string', 20);
$this->hasColumn('entity_id', 'integer');
}
}
class Email extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('address', 'string', 150, 'email|unique');
}
}
class EntityListener extends Doctrine_EventListener
{
public function onPreUpdate(Doctrine_Record $record)
{
$record->updated = time();
}
public function onPreInsert(Doctrine_Record $record)
{
$record->created = time();
}
}
// USER MANAGEMENT SYSTEM IN ACTION:
$manager = Doctrine_Manager::getInstance();
$conn = $manager->openConnection(new PDO('DSN','username','password'));
$user = new User();
$user->name = 'Jack Daniels';
$user->Email->address = 'jackdaniels@drinkmore.info';
$user->Phonenumber[0]->phonenumber = '123 123';
$user->Phonenumber[1]->phonenumber = '133 133';
$user->save();
$user->Group[0]->name = 'beer lovers';
$user->Group[0]->Email->address = 'beerlovers@drinkmore.info';
$user->Group[0]->save();
</code>