Development

Changeset 8262

You must first sign up to be able to contribute.

Changeset 8262

Show
Ignore:
Timestamp:
04/04/08 11:19:57 (6 months ago)
Author:
francois
Message:

[doc 1.1] documented ability to easily add fixtures in a many-to-many relationship

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.1/book/08-Inside-the-Model-Layer.txt

    r8049 r8262  
    933933 
    934934>**TIP** 
    935 >The command line also offers a task to populate your database with data based on a text file. See Chapter 16 for more information about the `propel-load-data` task and the YAML fixture files. 
     935>The command line also offers a task to populate your database with data based on a text file. See Chapter 16 for more information about the `propel:data-load` task and the YAML fixture files. 
    936936 
    937937### Generating a YAML Data Model from an Existing Database 
  • doc/branches/1.1/book/16-Application-Management-Tools.txt

    r7705 r8262  
    373373The `propel-load-data` task imports data from YAML files to a database. The connection settings come from the `databases.yml` file, and therefore need an application name to run. Optionally, you can specify an environment name by adding a `--env` option (`dev` by default). 
    374374 
    375     > symfony propel-load-data --env=prod frontend 
     375    > symfony propel:data-load --env=prod frontend 
    376376 
    377377This command reads all the YAML fixture files from the `data/fixtures/` directory and inserts the records into the database. By default, it replaces the existing database content, but if you add an `--append` option, the command will not erase the current data. 
    378378 
    379     > symfony propel-load-data --append frontend 
     379    > symfony propel:data-load --append frontend 
    380380 
    381381You can specify another fixture directory in the call. In this case, add a path relative to the project directory. 
    382382 
    383     > symfony propel-load-data frontend --dir[]=data/myfixtures 
     383    > symfony propel:data-load frontend --dir[]=data/myfixtures 
    384384 
    385385### Using Linked Tables 
     
    387387You now know how to add records to a single table, but how do you add records with foreign keys to another table? Since the primary key is not included in the fixtures data, you need an alternative way to relate records to one another. 
    388388 
    389 Let's return to the example in Chapter 8, where a blog_article table is linked to a `blog_comment` table, as shown in Figure 16-8. 
     389Let's return to the example in Chapter 8, where a `blog_article` table is linked to a `blog_comment` table, as shown in Figure 16-8. 
    390390 
    391391Figure 16-8 - A sample database relational model 
     
    403403        content:      Your prose is too verbose. Write shorter sentences. 
    404404 
    405 The `propel-load-data` task will recognize the label that you gave to an article previously in `import_data.yml`, and grab the primary key of the corresponding `Article` record to set the `article_id` field. You don't even see the IDs of the records; you just link them by their labels--it couldn't be simpler. 
     405The `propel:data-load` task will recognize the label that you gave to an article previously in `import_data.yml`, and grab the primary key of the corresponding `Article` record to set the `article_id` field. You don't even see the IDs of the records; you just link them by their labels--it couldn't be simpler. 
    406406 
    407407The only constraint for linked records is that the objects called in a foreign key must be defined earlier in the file; that is, as you would do if you defined them one by one. The data files are parsed from the top to the bottom, and the order in which the records are written is important. 
    408408 
     409**New in symfony 1.1**: This also works for many-to-many relationships, where two classes are related through a third class. For instance, an `Article` can have many `Authors`, and an `Author` can have many `Articles`. You usually use an `ArticleAuthor` class for that, corresponding to an `article_author` table with an `article_id` and an `author_id` columns. Listing 16-14 shows how to write a fixture file to define many-to-many relationships with this model. Notice the plural table name used here--this is what triggers the search for a middle class. 
     410 
     411Listing 16-14 - Adding a Record to a Table Related by a Many-to-Many relationship, in `data/fixtures/import_data.yml` 
     412 
     413    Author: 
     414      first_author: 
     415        name: John Doe 
     416        article_authors: [first_post, second_post] 
     417         
    409418One data file can contain declarations of several classes. But if you need to insert a lot of data for many different tables, your fixture file might get too long to be easily manipulated. 
    410419 
    411 The `propel-load-data` task parses all the files it finds in the `fixtures/` directory, so nothing prevents you from splitting a YAML fixture file into smaller pieces. The important thing to keep in mind is that foreign keys impose a processing order for the tables. To make sure that they are parsed in the correct order, prefix the files with an ordinal number. 
     420The `propel:data-load` task parses all the files it finds in the `fixtures/` directory, so nothing prevents you from splitting a YAML fixture file into smaller pieces. The important thing to keep in mind is that foreign keys impose a processing order for the tables. To make sure that they are parsed in the correct order, prefix the files with an ordinal number. 
    412421 
    413422    100_article_import_data.yml 
     
    447456Symfony adds SSH on top of rsync to secure the data transfer. More and more commercial hosts support an SSH tunnel to secure file uploads on their servers, and that's a good practice to avoid security breaches. 
    448457 
    449 The SSH client called by symfony uses connection settings from the `config/properties.ini` file. Listing 16-14 gives an example of connection settings for a production server. Write the settings of your own production server in this file before any synchronization. You can also define a single parameters setting to provide your own rsync command line parameters. 
    450  
    451 Listing 16-14 - Sample Connection Settings for a Server Synchronization, in `myproject/config/properties.ini` 
     458The SSH client called by symfony uses connection settings from the `config/properties.ini` file. Listing 16-15 gives an example of connection settings for a production server. Write the settings of your own production server in this file before any synchronization. You can also define a single parameters setting to provide your own rsync command line parameters. 
     459 
     460Listing 16-15 - Sample Connection Settings for a Server Synchronization, in `myproject/config/properties.ini` 
    452461 
    453462    [symfony] 
     
    502511  * The files uploaded by users should not be transferred. One of the good practices of symfony projects is to store the uploaded files in the `web/uploads/` directory. This allows you to exclude all these files from the synchronization by pointing to only one directory. 
    503512 
    504 To exclude files from rsync synchronizations, open and edit the `rsync_exclude.txt` file under the `myproject/config/` directory. Each line can contain a file, a directory, or a pattern. The symfony file structure is organized logically, and designed to minimize the number of files or directories to exclude manually from the synchronization. See Listing 16-15 for an example. 
    505  
    506 Listing 16-15 - Sample rsync Exclusion Settings, in `myproject/config/rsync_exclude.txt` 
     513To exclude files from rsync synchronizations, open and edit the `rsync_exclude.txt` file under the `myproject/config/` directory. Each line can contain a file, a directory, or a pattern. The symfony file structure is organized logically, and designed to minimize the number of files or directories to exclude manually from the synchronization. See Listing 16-16 for an example. 
     514 
     515Listing 16-16 - Sample rsync Exclusion Settings, in `myproject/config/rsync_exclude.txt` 
    507516 
    508517    .svn