<commit_before>---
layout: post
title: Creating and Using Templates [第8天]
date: 2014-05-25 7:39:31
disqus: y
---

One of the most common and challenging tasks for any application involves persisting and reading information to and from a database. Fortunately, Symfony comes integrated with Doctrine, a library whose sole goal is to give you powerful tools to make this easy. In this chapter, you'll learn the basic philosophy behind Doctrine and see how easy working with a database can be.

Tips:

Doctrine is totally decoupled from Symfony and using it is optional. This chapter is all about the Doctrine ORM, which aims to let you map objects to a relational database (such as MySQL, PostgreSQL or Microsoft SQL). If you prefer to use raw database queries, this is easy, and explained in the "How to use Doctrine's DBAL Layer" cookbook entry.
You can also persist data to MongoDB using Doctrine ODM library. For more information, read the "DoctrineMongoDBBundle" documentation.

A Simple Example: A Product¶

The easiest way to understand how Doctrine works is to see it in action. In this section, you'll configure your database, create a Product object, persist it to the database and fetch it back out.

Tips:

Code along with the Example
If you want to follow along with the example in this chapter, create an AcmeStoreBundle via:

{% highlight PHP %}
$ php app/console generate:bundle --namespace=Acme/StoreBundle
{% endhighlight %}

Configuring the Database¶

Before you really begin, you'll need to configure your database connection information. By convention, this information is usually configured in an app/config/parameters.yml file:

{% highlight PHP %}

# app/config/parameters.yml
parameters:
    database_driver:    pdo_mysql
    database_host:      localhost
    database_name:      test_project
    database_user:      root
    database_password:  password

# ...

{% endhighlight %}

Tips:

Defining the configuration via parameters.yml is just a convention. The parameters defined in that file are referenced by the main configuration file when setting up Doctrine:<commit_msg>Doctrine part<commit_after>---
layout: post
title: Creating and Using Templates [第8天]
date: 2014-05-25 7:39:31
disqus: y
---

One of the most common and challenging tasks for any application involves persisting and reading information to and from a database. Fortunately, Symfony comes integrated with Doctrine, a library whose sole goal is to give you powerful tools to make this easy. In this chapter, you'll learn the basic philosophy behind Doctrine and see how easy working with a database can be.

Tips:

Doctrine is totally decoupled from Symfony and using it is optional. This chapter is all about the Doctrine ORM, which aims to let you map objects to a relational database (such as MySQL, PostgreSQL or Microsoft SQL). If you prefer to use raw database queries, this is easy, and explained in the "How to use Doctrine's DBAL Layer" cookbook entry.
You can also persist data to MongoDB using Doctrine ODM library. For more information, read the "DoctrineMongoDBBundle" documentation.

A Simple Example: A Product¶

The easiest way to understand how Doctrine works is to see it in action. In this section, you'll configure your database, create a Product object, persist it to the database and fetch it back out.

Tips:

Code along with the Example
If you want to follow along with the example in this chapter, create an AcmeStoreBundle via:

{% highlight PHP %}
$ php app/console generate:bundle --namespace=Acme/StoreBundle
{% endhighlight %}

Configuring the Database¶

Before you really begin, you'll need to configure your database connection information. By convention, this information is usually configured in an app/config/parameters.yml file:

{% highlight PHP %}

# app/config/parameters.yml
parameters:
    database_driver:    pdo_mysql
    database_host:      localhost
    database_name:      test_project
    database_user:      root
    database_password:  password

# ...

{% endhighlight %}

Tips:

Defining the configuration via parameters.yml is just a convention. The parameters defined in that file are referenced by the main configuration file when setting up Doctrine:

YAML:

{% highlight PHP %}
# app/config/config.yml
doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%<PASSWORD>%"
{% endhighlight %}

By separating the database information into a separate file, you can easily keep different versions of the file on each server. You can also easily store database configuration (or any sensitive information) outside of your project, like inside your Apache configuration, for example. For more information, see How to Set External Parameters in the Service Container.

{'fraction_non_alphanumeric': 0.053925619834710746, 'fraction_numerical': 0.005785123966942148, 'mean_word_length': 4.194206008583691, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2335209', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1185, 'n_words': 657}