Changeset 8262
- Timestamp:
- 04/04/08 11:19:57 (6 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
doc/branches/1.1/book/08-Inside-the-Model-Layer.txt
r8049 r8262 933 933 934 934 >**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. 936 936 937 937 ### Generating a YAML Data Model from an Existing Database doc/branches/1.1/book/16-Application-Management-Tools.txt
r7705 r8262 373 373 The `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). 374 374 375 > symfony propel -load-data--env=prod frontend375 > symfony propel:data-load --env=prod frontend 376 376 377 377 This 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. 378 378 379 > symfony propel -load-data--append frontend379 > symfony propel:data-load --append frontend 380 380 381 381 You can specify another fixture directory in the call. In this case, add a path relative to the project directory. 382 382 383 > symfony propel -load-datafrontend --dir[]=data/myfixtures383 > symfony propel:data-load frontend --dir[]=data/myfixtures 384 384 385 385 ### Using Linked Tables … … 387 387 You 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. 388 388 389 Let's return to the example in Chapter 8, where a blog_articletable is linked to a `blog_comment` table, as shown in Figure 16-8.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. 390 390 391 391 Figure 16-8 - A sample database relational model … … 403 403 content: Your prose is too verbose. Write shorter sentences. 404 404 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.405 The `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. 406 406 407 407 The 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. 408 408 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 411 Listing 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 409 418 One 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. 410 419 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.420 The `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. 412 421 413 422 100_article_import_data.yml … … 447 456 Symfony 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. 448 457 449 The SSH client called by symfony uses connection settings from the `config/properties.ini` file. Listing 16-1 4gives 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-1 4- Sample Connection Settings for a Server Synchronization, in `myproject/config/properties.ini`458 The 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 460 Listing 16-15 - Sample Connection Settings for a Server Synchronization, in `myproject/config/properties.ini` 452 461 453 462 [symfony] … … 502 511 * 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. 503 512 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-1 5for an example.505 506 Listing 16-1 5- Sample rsync Exclusion Settings, in `myproject/config/rsync_exclude.txt`513 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-16 for an example. 514 515 Listing 16-16 - Sample rsync Exclusion Settings, in `myproject/config/rsync_exclude.txt` 507 516 508 517 .svn