{"id":31,"date":"2009-04-24T15:35:48","date_gmt":"2009-04-24T07:35:48","guid":{"rendered":"http:\/\/karyleong.net\/?p=31"},"modified":"2009-05-23T22:58:04","modified_gmt":"2009-05-23T14:58:04","slug":"doctrine-schema","status":"publish","type":"post","link":"https:\/\/karyleong.net\/?p=31","title":{"rendered":"Doctrine Schema"},"content":{"rendered":"<h2>Data Types<\/h2>\n<p>Doctrine offers several column data types. When you specify the portable Doctrine type it is automatically converted to the appropriate type of the DBMS you are using. Below is a list of the available column types that can be used as well as the type it is translated to when using the MySQL DBMS engine.<\/p>\n<table style=\"height: 247px;\" border=\"0\" cellspacing=\"0\" width=\"175\">\n<thead>\n<tr>\n<th>Type<\/th>\n<th>MySQL Type<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>integer<\/td>\n<td>integer<\/td>\n<\/tr>\n<tr>\n<td>float<\/td>\n<td>double<\/td>\n<\/tr>\n<tr>\n<td>decimal<\/td>\n<td>decimal<\/td>\n<\/tr>\n<tr>\n<td>string<\/td>\n<td>varchar<\/td>\n<\/tr>\n<tr>\n<td>array<\/td>\n<td>text<\/td>\n<\/tr>\n<tr>\n<td>object<\/td>\n<td>text<\/td>\n<\/tr>\n<tr>\n<td>blob<\/td>\n<td>longblob<\/td>\n<\/tr>\n<tr>\n<td>clob<\/td>\n<td>longtext<\/td>\n<\/tr>\n<tr>\n<td>timestamp<\/td>\n<td>datetime<\/td>\n<\/tr>\n<tr>\n<td>time<\/td>\n<td>time<\/td>\n<\/tr>\n<tr>\n<td>date<\/td>\n<td>date<\/td>\n<\/tr>\n<tr>\n<td>enum<\/td>\n<td>varchar\/enum<\/td>\n<\/tr>\n<tr>\n<td>gzip<\/td>\n<td>text<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>If you want to use MySQL built-in enum type, you need enable the native enum support in the config file.<\/p>\n<p>Now lets specify the <code>use_native_enum<\/code> attribute on our connection so that Doctrine knows to generate the native enum sql for your DBMS.<\/p>\n<pre>all:\r\n  doctrine:\r\n    class: sfDoctrineDatabase\r\n    param:\r\n      dsn: 'mysql:host=localhost;dbname=symfony12doctrine'\r\n      username: user\r\n      attributes:\r\n        use_native_enum: true<\/pre>\n<p>Below is a sample yaml schema file that implements each of the different column types.<\/p>\n<pre>User:\r\n  columns:\r\n    id:\r\n      type: integer(4)\r\n      primary: true\r\n      autoincrement: true\r\n    username: string(255)\r\n    password: string(255)\r\n    latitude: float\r\n    longitude: float\r\n    hourly_rate:\r\n      type: decimal\r\n      scale: 2\r\n    groups_array: array\r\n    session_object: object\r\n    description: clob\r\n    profile_image_binary_data: blob\r\n    created_at: timestamp\r\n    time_last_available: time\r\n    date_last_available: date\r\n    roles:\r\n      type: enum\r\n      values: [administrator, moderator, normal]\r\n      default: normal\r\n    html_header: gzip<\/pre>\n<h2>Indexes<\/h2>\n<p>You can optimize your database by defining indexes on columns which are used in conditions on your queries. Below is an example of indexing the username column of a user table since it is common to do lookups on the table by the users username.<\/p>\n<pre>User:\r\n  columns:\r\n    username: string(255)\r\n    password: string(255)\r\n  indexes:\r\n    username_index:\r\n      fields: [username]\r\n      type: unique<\/pre>\n<h2>Relationships<\/h2>\n<p>Doctrine offers the ability to map the relationships which exist in your database to the ORM so that it can be the most help when working with your data.<\/p>\n<p><a name=\"One to One\"><\/a><\/p>\n<h3>One to One<\/h3>\n<p>Here is a simple example of how to define a one-to-one relation between a User and Profile model.<\/p>\n<pre>Profile:\r\n  columns:\r\n    user_id: integer\r\n    name: string(255)\r\n    email_address:\r\n      type: string(255)\r\n      email: true\r\n  relations:\r\n    User:\r\n      local: user_id\r\n      foreign: id\r\n      type: one\r\n      foreignType: one<\/pre>\n<h3>One to Many<\/h3>\n<p>Here is a simple example of how to define a one-to-many relation between a User and Phonenumber model.<\/p>\n<pre>Phonenumber:\r\n  columns:\r\n    user_id: integer\r\n    phonenumber: string(255)\r\n  relations:\r\n    User:\r\n      foreignAlias: Phonenumbers\r\n      local: user_id\r\n      foreign: id\r\n      type: one<\/pre>\n<h3>Many to Many<\/h3>\n<p>Here is a simple example of how to define a many-to-many relation between a BlogPost and Tag model.<\/p>\n<pre>BlogPost:\r\n  columns:\r\n    user_id: integer\r\n    title: string(255)\r\n    body: clob\r\n  relations:\r\n    User:\r\n      local: user_id\r\n      foreign: id\r\n      type: one\r\n      foreignType: one\r\n      foreignAlias: BlogPosts\r\n    Tags:\r\n      class: Tag\r\n      foreignAlias: BlogPosts\r\n      refClass: BlogPostTag\r\n      local: blog_post_id\r\n      foreign: tag_id\r\n\r\nTag:\r\n  columns:\r\n    name: string(255)\r\n\r\nBlogPostTag:\r\n  columns:\r\n    blog_post_id:\r\n      type: integer\r\n      primary: true\r\n    tag_id:\r\n      type: integer\r\n      primary: true\r\n  relations:\r\n    BlogPost:\r\n      local: blog_post_id\r\n      foreign: id\r\n      foreignAlias: BlogPostTags\r\n    Tag:\r\n      local: tag_id\r\n      foreign: id\r\n      foreignAlias: BlogPostTags\r\n      foreignType: many<\/pre>\n<h2>Behaviors<\/h2>\n<p>One great feature of Doctrine is the ability to have plug n&#8217; play behavior. These behaviors can be easily included in your model definitions and you inherit functionality automatically.<\/p>\n<p><a name=\"Core Behaviors\"><\/a><\/p>\n<h3>Core Behaviors<\/h3>\n<p>Here is a list of behavior bundled with Doctrine core. You can use any of the behaviors in your models without writing any code.<\/p>\n<table class=\"doc_table\" border=\"0\" cellspacing=\"0\">\n<thead>\n<tr>\n<th>Name<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Geographical<\/td>\n<td>Adds latitude and longitude to your model and offers functionality for calculating miles\/kilometers between records.<\/td>\n<\/tr>\n<tr>\n<td>I18n<\/td>\n<td>Adds internationalization capabilities to your models.<\/td>\n<\/tr>\n<tr>\n<td>NestedSet<\/td>\n<td>Turn your models in to a traversable tree.<\/td>\n<\/tr>\n<tr>\n<td>Searchable<\/td>\n<td>Index all the data in your models and make it searchable.<\/td>\n<\/tr>\n<tr>\n<td>Sluggable<\/td>\n<td>Add a <code>slug<\/code> field to your models and have it automatically create a slug based on your configuration.<\/td>\n<\/tr>\n<tr>\n<td>SoftDelete<\/td>\n<td>Never really delete a record. Will simply set a deleted flag instead and filter all deleted records from select queries.<\/td>\n<\/tr>\n<tr>\n<td>Timestampable<\/td>\n<td>Add a <code>created_at<\/code> and <code>updated_at<\/code> column to your models have Doctrine set them when inserting and updating records.<\/td>\n<\/tr>\n<tr>\n<td>Versionable<\/td>\n<td>Turn your models in to an audit log and record all changes. Offers the ability to revert back to previous versions easily<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Sluggable:<\/h3>\n<pre>BlogPost:\r\n  actAs:\r\n    Sluggable:\r\n      fields: [title]\r\n      unique: true\r\n  columns:\r\n    user_id: integer\r\n    title: string(255)\r\n    body: clob<\/pre>\n<h3>Nesting Behaviors<\/h3>\n<pre>Gallery:\r\n  actAs:\r\n    I18n:\r\n      fields: [title, description]\r\n      actAs:\r\n        Sluggable:\r\n          fields:  [title]\r\n  columns:\r\n    title: string(255)\r\n    description: clob<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Data Types Doctrine offers several column data types. When you specify the portable Doctrine type it is automatically converted to the appropriate type of the DBMS you are using. Below is a list of the available column types that can be used as well as the type it is translated to when using the MySQL &#8230; <a title=\"Doctrine Schema\" class=\"read-more\" href=\"https:\/\/karyleong.net\/?p=31\" aria-label=\"More on Doctrine Schema\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[15,16,70],"_links":{"self":[{"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/posts\/31"}],"collection":[{"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/karyleong.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=31"}],"version-history":[{"count":5,"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/karyleong.net\/index.php?rest_route=\/wp\/v2\/posts\/31\/revisions\/45"}],"wp:attachment":[{"href":"https:\/\/karyleong.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/karyleong.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/karyleong.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}