Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

doctrine1-documentation-27: Edit real-world-examples.rst

  • Loading branch information...
commit 4ebb0bac20e3df804df1bf32cd67b7f3311187fc 1 parent d143b84
Eugene Serkin jeserkin authored
Showing with 350 additions and 193 deletions.
  1. +350 −193 source/en/manual/real-world-examples.rst
543 source/en/manual/real-world-examples.rst
View
@@ -11,106 +11,204 @@ authentication system where you have users, roles, permissions, etc.
Below is an example where we setup several models that give you a basic
user management and security system.
- class User extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('username', 'string', 255,
-array( 'unique' => true ) ); $this->hasColumn('password', 'string',
-255); } }
-
-class Role extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('name', 'string', 255); } }
-
-class Permission extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('name', 'string', 255); } }
-
-class RolePermission extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('role\_id', 'integer', null,
-array( 'primary' => true ) ); $this->hasColumn('permission\_id',
-'integer', null, array( 'primary' => true ) ); }
-
::
- public function setUp()
+ class User extends Doctrine_Record
{
- $this->hasOne('Role', array(
- 'local' => 'role_id',
- 'foreign' => 'id'
- )
- );
- $this->hasOne('Permission', array(
- 'local' => 'permission_id',
- 'foreign' => 'id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'username', 'string', 255,
+ array(
+ 'unique' => true,
+ )
+ );
+ $this->hasColumn( 'password', 'string', 255 );
+ }
}
-}
-
-class UserRole extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('user\_id', 'integer', null,
-array( 'primary' => true ) ); $this->hasColumn('role\_id', 'integer',
-null, array( 'primary' => true ) ); }
-
-::
-
- public function setUp()
+ class Role extends Doctrine_Record
{
- $this->hasOne('User', array(
- 'local' => 'user_id',
- 'foreign' => 'id'
- )
- );
- $this->hasOne('Role', array(
- 'local' => 'role_id',
- 'foreign' => 'id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'name', 'string', 255 );
+ }
}
-}
-
-class UserPermission extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('user\_id', 'integer', null,
-array( 'primary' => true ) ); $this->hasColumn('permission\_id',
-'integer', null, array( 'primary' => true ) ); }
+ class Permission extends Doctrine_Record
+ {
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'name', 'string', 255 );
+ }
+ }
-::
+ class RolePermission extends Doctrine_Record
+ {
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'role_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ $this->hasColumn( 'permission_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'Role',
+ array(
+ 'local' => 'role_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasOne( 'Permission',
+ array(
+ 'local' => 'permission_id',
+ 'foreign' => 'id',
+ )
+ );
+ }
+ }
- public function setUp()
+ class UserRole extends Doctrine_Record
{
- $this->hasOne('User', array(
- 'local' => 'user_id',
- 'foreign' => 'id'
- )
- );
- $this->hasOne('Permission', array(
- 'local' => 'permission_id',
- 'foreign' => 'id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'user_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ $this->hasColumn( 'role_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'User',
+ array(
+ 'local' => 'user_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasOne( 'Role',
+ array(
+ 'local' => 'role_id',
+ 'foreign' => 'id',
+ )
+ );
+ }
}
-}
+ class UserPermission extends Doctrine_Record
+ {
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'user_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ $this->hasColumn( 'permission_id', 'integer', null,
+ array(
+ 'primary' => true,
+ )
+ );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'User',
+ array(
+ 'local' => 'user_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasOne( 'Permission',
+ array(
+ 'local' => 'permission_id',
+ 'foreign' => 'id',
+ )
+ );
+ }
+ }
Here is the same example in YAML format. You can read more about YAML in
-the [doc yaml-schema-files :name] chapter:
-
- User: columns: username: string(255) password: string(255) relations:
-Roles: class: Role refClass: UserRole foreignAlias: Users Permissions:
-class: Permission refClass: UserPermission foreignAlias: Users
-
-Role: columns: name: string(255) relations: Permissions: class:
-Permission refClass: RolePermission foreignAlias: Roles
-
-Permission: columns: name: string(255)
-
-RolePermission: columns: role\_id: type: integer primary: true
-permission\_id: type: integer primary: true relations: Role: Permission:
-
-UserRole: columns: user\_id: type: integer primary: true role\_id: type:
-integer primary: true relations: User: Role:
-
-UserPermission: columns: user\_id: type: integer primary: true
-permission\_id: type: integer primary: true relations: User: Permission:
+the :doc:`yaml-schema-files` chapter:
+
+.. code-block:: yaml
+
+ ---
+ User:
+ columns:
+ username: string(255)
+ password: string(255)
+ relations:
+ Roles:
+ class: Role
+ refClass: UserRole
+ foreignAlias: Users
+ Permissions:
+ class: Permission
+ refClass: UserPermission
+ foreignAlias: Users
+
+ Role:
+ columns:
+ name: string(255)
+ relations:
+ Permissions:
+ class: Permission
+ refClass: RolePermission
+ foreignAlias: Roles
+
+ Permission:
+ columns:
+ name: string(255)
+
+ RolePermission:
+ columns:
+ role_id:
+ type: integer
+ primary: true
+ permission_id:
+ type: integer
+ primary: true
+ relations:
+ Role:
+ Permission:
+
+ UserRole:
+ columns:
+ user_id:
+ type: integer
+ primary: true
+ role_id:
+ type: integer
+ primary: true
+ relations:
+ User:
+ Role:
+
+ UserPermission:
+ columns:
+ user_id:
+ type: integer
+ primary: true
+ permission_id:
+ type: integer
+ primary: true
+ relations:
+ User:
+ Permission:
=================
Forum Application
@@ -119,126 +217,185 @@ Forum Application
Below is an example of a forum application where you have categories,
boards, threads and posts:
- class Forum\_Category extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('root\_category\_id', 'integer',
-10); $this->hasColumn('parent\_category\_id', 'integer', 10);
-$this->hasColumn('name', 'string', 50); $this->hasColumn('description',
-'string', 99999); }
-
::
- public function setUp()
+ class Forum_Category extends Doctrine_Record
{
- $this->hasMany('Forum_Category as Subcategory', array(
- 'local' => 'parent_category_id',
- 'foreign' => 'id'
- )
- );
- $this->hasOne('Forum_Category as Rootcategory', array(
- 'local' => 'root_category_id',
- 'foreign' => 'id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'root_category_id', 'integer', 10 );
+ $this->hasColumn( 'parent_category_id', 'integer', 10 );
+ $this->hasColumn( 'name', 'string', 50 );
+ $this->hasColumn( 'description', 'string', 99999 );
+ }
+
+ public function setUp()
+ {
+ $this->hasMany( 'Forum_Category as Subcategory',
+ array(
+ 'local' => 'parent_category_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasOne( 'Forum_Category as Rootcategory',
+ array(
+ 'local' => 'root_category_id',
+ 'foreign' => 'id',
+ )
+ );
+ }
}
-}
-
-class Forum\_Board extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('category\_id', 'integer', 10);
-$this->hasColumn('name', 'string', 100); $this->hasColumn('description',
-'string', 5000); }
-
-::
-
- public function setUp()
+ class Forum_Board extends Doctrine_Record
{
- $this->hasOne('Forum_Category as Category', array(
- 'local' => 'category_id',
- 'foreign' => 'id'
- )
- );
- $this->hasMany('Forum_Thread as Threads', array(
- 'local' => 'id',
- 'foreign' => 'board_id'
- )
- );
- }
-
-}
-
-class Forum\_Entry extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('author', 'string', 50);
-$this->hasColumn('topic', 'string', 100); $this->hasColumn('message',
-'string', 99999); $this->hasColumn('parent\_entry\_id', 'integer', 10);
-$this->hasColumn('thread\_id', 'integer', 10); $this->hasColumn('date',
-'integer', 10); }
-
-::
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'category_id', 'integer', 10 );
+ $this->hasColumn( 'name', 'string', 100 );
+ $this->hasColumn( 'description', 'string', 5000 );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'Forum_Category as Category',
+ array(
+ 'local' => 'category_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasMany( 'Forum_Thread as Threads',
+ array(
+ 'local' => 'id',
+ 'foreign' => 'board_id'
+ )
+ );
+ }
+ }
- public function setUp()
+ class Forum_Entry extends Doctrine_Record
{
- $this->hasOne('Forum_Entry as Parent', array(
- 'local' => 'parent_entry_id',
- 'foreign' => 'id'
- )
- );
- $this->hasOne('Forum_Thread as Thread', array(
- 'local' => 'thread_id',
- 'foreign' => 'id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'author', 'string', 50 );
+ $this->hasColumn( 'topic', 'string', 100 );
+ $this->hasColumn( 'message', 'string', 99999 );
+ $this->hasColumn( 'parent_entry_id', 'integer', 10 );
+ $this->hasColumn( 'thread_id', 'integer', 10 );
+ $this->hasColumn( 'date', 'integer', 10 );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'Forum_Entry as Parent',
+ array(
+ 'local' => 'parent_entry_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasOne( 'Forum_Thread as Thread',
+ array(
+ 'local' => 'thread_id',
+ 'foreign' => 'id',
+ )
+ );
+ }
}
-}
-
-class Forum\_Thread extends Doctrine\_Record { public function
-setTableDefinition() { $this->hasColumn('board\_id', 'integer', 10);
-$this->hasColumn('updated', 'integer', 10); $this->hasColumn('closed',
-'integer', 1); }
-
-::
-
- public function setUp()
+ class Forum_Thread extends Doctrine_Record
{
- $this->hasOne('Forum_Board as Board', array(
- 'local' => 'board_id',
- 'foreign' => 'id'
- )
- );
-
- $this->hasMany('Forum_Entry as Entries', array(
- 'local' => 'id',
- 'foreign' => thread_id'
- )
- );
+ public function setTableDefinition()
+ {
+ $this->hasColumn( 'board_id', 'integer', 10 );
+ $this->hasColumn( 'updated', 'integer', 10 );
+ $this->hasColumn( 'closed', 'integer', 1 );
+ }
+
+ public function setUp()
+ {
+ $this->hasOne( 'Forum_Board as Board',
+ array(
+ 'local' => 'board_id',
+ 'foreign' => 'id',
+ )
+ );
+ $this->hasMany( 'Forum_Entry as Entries',
+ array(
+ 'local' => 'id',
+ 'foreign' => 'thread_id',
+ )
+ );
+ }
}
-}
-
Here is the same example in YAML format. You can read more about YAML in
-the [doc yaml-schema-files :name] chapter:
-
- Forum\_Category: columns: root\_category\_id: integer(10)
-parent\_category\_id: integer(10) name: string(50) description:
-string(99999) relations: Subcategory: class: Forum\_Category local:
-parent\_category\_id foreign: id Rootcategory: class: Forum\_Category
-local: root\_category\_id foreign: id
-
-Forum\_Board: columns: category\_id: integer(10) name: string(100)
-description: string(5000) relations: Category: class: Forum\_Category
-local: category\_id foreign: id Threads: class: Forum\_Thread local: id
-foreign: board\_id
-
-Forum\_Entry: columns: author: string(50) topic: string(100) message:
-string(99999) parent\_entry\_id: integer(10) thread\_id: integer(10)
-date: integer(10) relations: Parent: class: Forum\_Entry local:
-parent\_entry\_id foreign: id Thread: class: Forum\_Thread local:
-thread\_id foreign: id
-
-Forum\_Thread: columns: board\_id: integer(10) updated: integer(10)
-closed: integer(1) relations: Board: class: Forum\_Board local:
-board\_id foreign: id Entries: class: Forum\_Entry local: id foreign:
-thread\_id
+the :doc:`yaml-schema-files` chapter:
+
+.. code-block:: yaml
+
+ ---
+ Forum_Category:
+ columns:
+ root_category_id: integer(10)
+ parent_category_id: integer(10)
+ name: string(50)
+ description: string(99999)
+ relations:
+ Subcategory:
+ class: Forum_Category
+ local: parent_category_id
+ foreign: id
+ Rootcategory:
+ class: Forum_Category
+ local: root_category_id
+ foreign: id
+
+ Forum_Board:
+ columns:
+ category_id: integer(10)
+ name: string(100)
+ description: string(5000)
+ relations:
+ Category:
+ class: Forum_Category
+ local: category_id
+ foreign: id
+ Threads:
+ class: Forum_Thread
+ local: id
+ foreign: board_id
+
+ Forum_Entry:
+ columns:
+ author: string(50)
+ topic: string(100)
+ message: string(99999)
+ parent_entry_id: integer(10)
+ thread_id: integer(10)
+ date: integer(10)
+ relations:
+ Parent:
+ class: Forum_Entry
+ local: parent_entry_id
+ foreign: id
+ Thread:
+ class: Forum_Thread
+ local: thread_id
+ foreign: id
+
+ Forum_Thread:
+ columns:
+ board_id: integer(10)
+ updated: integer(10)
+ closed: integer(1)
+ relations:
+ Board:
+ class: Forum_Board
+ local: board_id
+ foreign: id
+ Entries:
+ class: Forum_Entry
+ local: id
+ foreign: thread_id
==========
Conclusion
@@ -246,6 +403,6 @@ Conclusion
I hope that these real world schema examples will help you with using
Doctrine in the real world in your application. The last chapter of this
-book will discuss the [doc coding-standards coding standards] used in
+book will discuss the :doc:`coding standards <coding-standards>` used in
Doctrine and are recommended for you to use in your application as well.
-Remember, consistency in your code is key!
+Remember, consistency in your code is key!
Please sign in to comment.
Something went wrong with that request. Please try again.