1
0
mirror of synced 2024-12-16 16:16:04 +03:00
doctrine2/manual/new/docs/en/real-world-examples/user-management-system.txt

103 lines
2.8 KiB
Plaintext
Raw Normal View History

2007-06-13 02:18:21 +04:00
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
2007-06-14 18:59:49 +04:00
<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());
2007-06-13 02:18:21 +04:00
}
2007-06-14 18:59:49 +04:00
public function setTableDefinition()
{
$this->hasColumn('name', 'string', 50);
$this->hasColumn('loginname', 'string', 20);
$this->hasColumn('password', 'string', 16);
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 19:13:57 +04:00
class Group extends Entity
2007-06-14 18:59:49 +04:00
{
public function setUp()
{
2007-06-13 02:18:21 +04:00
parent::setUp();
2007-06-14 18:59:49 +04:00
$this->hasMany('User', 'Groupuser.user_id');
$this->setInheritanceMap(array('type'=>1));
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 18:59:49 +04:00
class User extends Entity
{
public function setUp()
{
2007-06-13 02:18:21 +04:00
parent::setUp();
2007-06-14 18:59:49 +04:00
$this->hasMany('Group', 'Groupuser.group_id');
$this->setInheritanceMap(array('type'=>0));
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 18:59:49 +04:00
class Groupuser extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('group_id', 'integer');
$this->hasColumn('user_id', 'integer');
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 18:59:49 +04:00
class Phonenumber extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('phonenumber', 'string', 20);
$this->hasColumn('entity_id', 'integer');
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 18:59:49 +04:00
class Email extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('address', 'string', 150, 'email|unique');
2007-06-13 02:18:21 +04:00
}
}
2007-06-14 18:59:49 +04:00
class EntityListener extends Doctrine_EventListener
{
public function onPreUpdate(Doctrine_Record $record)
{
2007-06-13 02:18:21 +04:00
$record->updated = time();
}
2007-06-14 18:59:49 +04:00
public function onPreInsert(Doctrine_Record $record)
{
2007-06-13 02:18:21 +04:00
$record->created = time();
}
}
// USER MANAGEMENT SYSTEM IN ACTION:
$manager = Doctrine_Manager::getInstance();
2007-06-14 18:59:49 +04:00
$conn = $manager->openConnection(new PDO('DSN','username','password'));
2007-06-13 02:18:21 +04:00
$user = new User();
2007-06-14 18:59:49 +04:00
$user->name = 'Jack Daniels';
$user->Email->address = 'jackdaniels@drinkmore.info';
$user->Phonenumber[0]->phonenumber = '123 123';
$user->Phonenumber[1]->phonenumber = '133 133';
2007-06-13 02:18:21 +04:00
$user->save();
2007-06-14 18:59:49 +04:00
$user->Group[0]->name = 'beer lovers';
$user->Group[0]->Email->address = 'beerlovers@drinkmore.info';
2007-06-13 02:18:21 +04:00
$user->Group[0]->save();
2007-06-14 19:13:57 +04:00
</code>