Development

Changeset 11054

You must first sign up to be able to contribute.

Changeset 11054

Show
Ignore:
Timestamp:
08/22/08 17:29:11 (3 months ago)
Author:
halfer
Message:

Received file from Fabien, made some changes and added some warnings. Still have Forms stuff to do.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • doc/branches/1.1/tutorial/my-first-project.txt

    r10381 r11054  
    22======================== 
    33 
    4 So, you want to try it on? Let's build together a fully-functional web app in 
    5 one hour. You name it. A bookseller application? Ok, another idea. A blog! 
     4>**Caution** 
     5>This tutorial is presently being converted from the 1.0 version. It is entirely possible 
     6>to get a basic version running on 1.1, but you may need to get a few hints from more 
     7>experienced users in the [users' forum](http://www.symfony-project.org/forum/index.php/f/15/). 
     8 
     9So, you want to give it a go? Let's build together a fully-functional web app in 
     10one hour. You name it. A bookseller application? Okay, another idea. A blog! 
    611That's a good one. Let's go. 
    712 
    8 We'll assume that you are working with Apache installed and launched on your 
    9 localhost. You will also need PHP 5.1.3 or newer. 
     13We'll assume that you are working with Apache installed and running on your 
     14local host. You will also need PHP 5.1.3 or newer. 
    1015 
    1116Install symfony and initialize the project 
    1217------------------------------------------ 
    1318 
    14 To go fast, we will use the symfony sandbox. 
    15  
    16 It is an empty symfony project where all the required libraries are already included, 
     19To go fast, we will use the symfony sandbox. This is an empty symfony project where all 
     20the required libraries are already included, 
    1721and where the basic configuration is already done. The great advantage of the sandbox 
    1822over other types of installation is that you can start experimenting with symfony 
    1923immediately. 
    2024 
    21 Get it here: [sf_sandbox_1_1.tgz](http://www.symfony-project.org/get/sf_sandbox_1_1.tgz), 
    22 and unpack it in your root web directory. It is recommended to keep the permissions as 
    23 they are in the tar file (for example by using -p with tar command). Refer to the 
    24 included `README` file for more information. The resulting file structure should look like: 
     25Get it here: [sf_sandbox_1_1.tgz](http://www.symfony-project.org/get/sf_sandbox_1_1.tgz) 
     26or here: [sf_sandbox_1_1.zip](http://www.symfony-project.org/get/sf_sandbox_1_1.zip), 
     27and unpack it in your root web directory. On Linux systems, it is recommended to keep the 
     28permissions as they are in the tar file (for example by using -p with tar command). 
     29Refer to the included `README` file for more information. The resulting file structure 
     30should look like this: 
    2531 
    2632    www/ 
     
    2834        apps/ 
    2935          frontend/ 
    30         batch/ 
    3136        cache/ 
    3237        config/ 
    3338        data/ 
    34           sql/ 
    3539        doc/ 
    3640        lib/ 
    37           model/ 
    3841        log/ 
    3942        plugins/ 
     
    6568 
    6669    propel: 
    67       post: 
    68         id:         
    69         title:       varchar(255) 
    70         excerpt:     longvarchar 
    71         body:        longvarchar 
    72         created_at: 
    73       comment: 
    74         id:         
    75         post_id:   
    76         author:      varchar(255) 
    77         email:       varchar(255) 
    78         body:        longvarchar 
    79         created_at: 
     70      blog_post: 
     71        id:         
     72        title:        varchar(255) 
     73        excerpt:      longvarchar 
     74        body:        longvarchar 
     75        created_at: 
     76      blog_comment: 
     77        id:         
     78        blog_post_id:
     79        author:      varchar(255) 
     80        email:        varchar(255) 
     81        body:        longvarchar 
     82        created_at: 
    8083 
    8184This configuration file uses the YAML syntax. It's a very simple language that 
     
    112115located in `sf_sandbox/data/`. 
    113116 
    114 If you want to switch to MySQL for this project, just type the following command line
     117If you want to switch to MySQL for this project, use the `configure:database` task
    115118 
    116119    $ php symfony configure:database mysql://root:pa$$word@localhost/symfony_project 
     
    120123the [model chapter](http://www.symfony-project.org/book/1_1/08-Inside-the-Model-Layer)). 
    121124 
     125>**Caution** 
     126>If you use SQLite as your database engine, you will have to change some rights on *nix systems: 
     127> 
     128>     $ chmod 777 data data/sandbox.db 
     129 
    122130Now type in the command line: 
    123131 
     
    128136the same table structure. 
    129137 
     138>**Note** 
     139>At the time of writing, the file path configured for the sqlite database file in the 
     140>1.1 sandbox is not set up correctly. This however is easily fixed: just open up 
     141>`/config/propel.ini` and ensure that the following lines have 7 sets of '..' in them: 
     142> 
     143>propel.database.createUrl  = sqlite://./../../../../../../../data/sandbox.db 
     144>propel.database.url        = sqlite://./../../../../../../../data/sandbox.db 
     145> 
     146>If they do not, copy the lines as they appear here, and save the file. 
     147 
    130148To build the table structure based on the the SQL file, type: 
    131149 
     
    139157generate some forms based on our model schema: 
    140158 
    141     $ php symfony propel:build-form 
     159    $ php symfony propel:build-forms 
    142160 
    143161This task generates classes in the `sf_sandbox/lib/form/` directory. 
     
    153171interface automatically: 
    154172 
    155     $ php symfony propel:generate-crud frontend post Post --with-show 
    156     $ php symfony propel:generate-crud frontend comment Comment --with-show 
     173    $ php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions --with-show frontend post BlogPost 
     174    $ php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions frontend comment BlogComment 
    157175    $ php symfony cache:clear 
    158176 
    159     On *nix systems, you will have to change some rights: 
    160     $ chmod 777 data 
    161     $ chmod 777 data/sandbox.db 
     177>**Tip** 
     178>When using the `propel:generate-crud` task, we have used the `--non-verbose-templates` 
     179>and `--non-atomic-actions` options. If you want to learn the meaning of the available 
     180>arguments and options for a given task, you can use the special `help` task: 
     181
     182>     $ php symfony help propel:generate-crud 
    162183 
    163184You now have two modules (`post` and `comment`) that will let you manipulate 
    164 objects of the `Post` and `Comment` classes. A **module** usually represents a 
     185objects of the `BlogPost` and `BlogComment` classes. A **module** usually represents a 
    165186page or a group of pages with a similar purpose. Your new modules are located 
    166187in the `sf_sandbox/apps/frontend/modules/` directory, and they are accessible 
     
    170191    http://localhost/sf_sandbox/web/frontend_dev.php/comment 
    171192 
    172 Feel free to create a new post to make the blog look less empty. 
     193If you try to create a comment, you will have an error because symfony doesn't yet 
     194know how to convert a post object to a string. Edit the `BlogPost` class 
     195(`lib/model/BlogPost.php`) and add the `__toString()` method: 
     196 
     197    [php] 
     198    class BlogPost extends BaseBlogPost 
     199    { 
     200      public function __toString() 
     201      { 
     202        return $this->getTitle(); 
     203      } 
     204    } 
     205 
     206Now, feel free to create a new post to make the blog look less empty. 
    173207 
    174208![post CRUD](/images/tutorials/first_crud.gif) 
     
    179213(project, application, module). 
    180214 
    181 >**Note**: In the URLs above, the name of the main script - called 
     215>**Note**: In the URLs above, the name of the main script - called the 
    182216>*front controller* in symfony - was changed from `index.php` to `frontend_dev.php`. 
    183217>The two scripts access the same application (`frontend`), but in different environments. 
     
    222256    </div> 
    223257 
    224 Please be forgiving for the poor design and the use of inner-tag css, but 
    225 one hour is a short time. 
     258Please forgive the poor design and the use of inner-tag css, but 
     259one hour is rather a short amount of time! 
    226260 
    227261![post CRUD in layout](/images/tutorials/first_crud_layout.gif) 
     
    237271      metas: 
    238272        title:        The best blog ever 
    239         robots:       index, follow 
    240273        description:  symfony project 
    241274        keywords:     symfony, project 
    242275        language:     en 
     276        robots:       index, follow 
    243277 
    244278The home page itself needs to be changed. It uses the default template of the 
    245279`default` module, which is kept in the framework but not in your application 
    246 directory. To override it, you have to create a custom `main` module: 
     280directory. To override it, you can create a custom `main` module: 
    247281 
    248282    $ php symfony generate:module frontend main 
     
    253287 
    254288    [php] 
    255     public function executeIndex() 
     289    /** 
     290     * Executes index action 
     291     * 
     292     * @param sfRequest $request A request object 
     293     */ 
     294    public function executeIndex($request) 
    256295    { 
    257296    } 
     
    265304 
    266305Now, you must tell symfony which action to execute when the homepage is requested. 
    267 To that extend, edit the `sf_sandbox/apps/frontend/config/routing.yml` and change 
     306To do so, edit the `sf_sandbox/apps/frontend/config/routing.yml` and change 
    268307the `homepage` rule as follows: 
    269308 
     
    279318![New home page](/images/tutorials/first_welcome.gif) 
    280319 
    281 Go ahead, start using your new web app: Create a new test post, and a test 
    282 comment for your this post. 
     320Go ahead, start using your new web app. Make sure you've created a test post, and 
     321also create a test comment against your post. 
    283322 
    284323Find more about [views and templates](http://www.symfony-project.org/book/1_1/07-Inside-the-View-Layer). 
     
    298337    public function executeShow($request) 
    299338    { 
    300       $this->post = PostPeer::retrieveByPk($request->getParameter('id')); 
    301       $this->forward404Unless($this->post); 
     339      $this->blog_post = BlogPostPeer::retrieveByPk($request->getParameter('id')); 
     340      $this->forward404Unless($this->blog_post); 
    302341 
    303342      $c = new Criteria(); 
    304       $c->add(CommentPeer::POST_ID, $request->getParameter('id')); 
    305       $c->addAscendingOrderByColumn(CommentPeer::CREATED_AT); 
    306       $this->comments = CommentPeer::doSelect($c); 
     343      $c->add(BlogCommentPeer::BLOG_POST_ID, $request->getParameter('id')); 
     344      $c->addAscendingOrderByColumn(BlogCommentPeer::CREATED_AT); 
     345      $this->comments = BlogCommentPeer::doSelect($c); 
    307346    } 
    308347 
    309348The `Criteria` and `-Peer` objects are part of Propel's object-relational mapping. 
    310 Basically, these four lines will handle a SQL query to the `Comment` table to get 
    311 the comments related to the current `Post` (the one designated by the URL parameter `id`). 
     349Basically, these four lines will handle a SQL query to the `blog_comment` table to get 
     350the comments related to the current `blog_post` (the one designated by the URL parameter `id`). 
    312351The `$this->comments` line in the action will give access to a `$comments` variable 
    313352in the corresponding template. Now, modify the post display template 
     
    315354 
    316355    [php] 
    317     ... 
     356    // ... 
    318357    <?php use_helper('Text', 'Date') ?> 
    319358 
    320359    <hr /> 
    321360    <?php if ($comments) : ?> 
    322       <p><?php echo count($comments) ?> comment<?php if (count($comments) > 1) : ?>s<?php endif; ?> to this post.</p> 
     361      <p><?php echo count($comments) ?> comments to this post.</p> 
    323362      <?php foreach ($comments as $comment): ?> 
    324363        <p><em>posted by <?php echo $comment->getAuthor() ?> on <?php echo format_date($comment->getCreatedAt()) ?></em></p> 
     
    347386-------------------------------------- 
    348387 
     388>**Caution** 
     389>The following section refers to code that would appear in the 1.0 version of the 
     390>sandbox, however symfony 1.1 uses a new Forms system. It will take a while longer 
     391>to convert this section. Please bear with us! 
     392 
    349393When adding a comment, you can choose the `id` of the related post. 
    350394That's not very user-friendly. Let's change this, and make sure that the user 
    351395comes back to the post he was looking at after adding a comment. 
    352396 
    353 First, in the still fresh `modules/post/templates/showSuccess.php` template, 
     397First, in the `modules/post/templates/showSuccess.php` template, 
    354398add a line at the bottom: 
    355399 
    356400    [php] 
    357     <?php echo link_to('Add a comment', 'comment/create?post_id='.$post->getId()) ?> 
    358  
    359 The `link_to()` helper creates a hyperlink pointing to the `create` action of 
     401    <?php echo link_to('Add a comment', 'comment/edit?post_id='.$blog_post->getId()) ?> 
     402 
     403The `link_to()` helper creates a hyperlink pointing to the `edit` action of 
    360404the `comment` module, so you can add a comment directly from the post details page. 
    361405Next, open the `modules/comment/templates/editSuccess.php` and replace the 
     
    678722add to the `sf_sandbox/lib/model/Post.php`: 
    679723 
    680     [php]     
    681     function __toString() 
    682     { 
    683       return $this->getTitle(); 
    684     } 
    685  
     724    [php] 
    686725    public function getNbComments() 
    687726    {