Setup Guides

Symfony Plugin – sfGuardDoctrine

2

Intro

For the most part, the README file associated to this plugin is fairly straight forward, and should be followed. This post is here to demonstrate an install method you can do manually and not have to re-load all of your data and loading fixtures.

–Update 4.9.10 –

I removed some sections from this post b/c they were deprecated. Also, check out http://trac.symfony-project.org/wiki/sfGuardPluginExtraDocumentation as it has a lot of useful information beyond the README.txt file.

Build SQL

This step will use your config/doctrine/schema.yml file and create a sql file you can use for creating the schema in your database.

php symfony doctrine:build-sql

Implement Schema

Now we are at the step that caused us to do everything the hard way (manual). If you follow the README file associated /w a plugin it will tell you to run ‘symfony doctrine:insert-sql’. If you do this you are basically wiping out any data you have in your database b/c the tables will be re-created.

Instead of wiping out existing data we are going to copy everything we want out of the data/sql/schema.sql file and put them into a data/sql/sfGuardDoctrine.sql file. This way when you import the schema to your database you will not wipe out your existing data.

touch data/sql/sfGuardDoctrine.sql
vi data/sql/schema.sql

Copy all sf_guard_* table references.

vi data/sql/sfGuardDoctrine.sql

Paste all copied data and save file. Now you are going to import the sfGuardDoctrine schema into your database:

mysql -h <db_host> -u <db_user> -p <db_name> < data/sql/sfGuardDoctrine.sql

If the command ran successfully then you should be able to connect to your database and see the new sf_guard_* tables in place.

Add User via CLI

./symfony guard:create-user <username> <password>

EOF

You should be able to finish up the setup of this plugin by referring to the plugins/sfDoctrineGuardPlugin/README file or the link posted earlier.

Symfony Admin Forms Setup (Admin Generator)

0

Symfony admin generator

Admin Generator is an awesome tool available via the symfony framework for doctrine (and propel). Based on the model it will automatically generate tedious forms for you to admin the various objects associated with your model.

php symfony doctrine:generate-admin app_name model_name --module=module_name
php symfony doctrine:build-filters
php symfony doctrine:build-forms
php symfony cc

Ok you should now be able to see your admin interface @ http://yourdomain.com/<app_name>_dev.php/<module_name>. Pretty cool huh? You should now have access to forms that allow you to manipulate data. So in a few easy steps you just saved yourself days of work.

Setting default route

vi apps/<app_name>/config/routing.yml

Before

homepage:
  url:   /
  param: { module: default, action: index }

After

homepage:
 url:   /
 param: { module: module_name, action: index }

Configure form attributes

You can now customize form attributes by editing the following file:

vi apps/app_name/modules/module_name/config/generator.yml

As you can see in the file there are a number of attributes you can configure, and we will go over some of the attributes we need to configure in order to custom tailor your admin app.

First lets change the page title, this attribute will change the default ‘<model> List’ to something more to your liking, maybe ‘<model> Management’. To do this simply add the following line under the ‘list:’ category:

Before

...
  config:
    ...
      list: ~

After

...
  config:
    ...
      list:
        title: object Management

Yep, it’s that simple. Go ahead and refresh the page. If you don’t see the page you may need to clear cache:

php symfony cc

Now we’ll edit the titles of the action views; editing or creating a new <object>

Before

...
  config:
    ...
      edit: ~
      new: ~

After

...
  config:
    ...
      edit:
        Editing <object> "%%<name_field>%%"
      new:
        New <object>

Refresh the page and click on new and edit to see the changes (if you don’t, be sure to clear cache). You can also configure how the rows from your <object> table are displayed. If your table is wide you may want to remove a few elements so you can keep the rows from wrapping, you can do that by editing the list sub category:

Before

...
  ...
    config:
      list:
        ...

After

...
  ...
    config:
      list:
        ...
        display: [=<name_column>, <another_column>, <yet_another_column>]

Now refresh your page. You should see your <name_column> first (notice it’s rendered as an html link, this is what the = modifier does), <another_column> and <yet_another_column>. Cool!

Remove Batch Actions

Batch actions refer to the ability to check multiple checkboxes then select an option from the drop down menu. If you don’t need these you can remove them:

Before

...
  ...
    ...
      config:
        list:
          ...

After

...
  ...
    ...
      config:
        list:
           batch_actions: {}

Remove object actions

If you want to remove object level actions (delete, edit by default) from each objects row:

Before

...
  ...
    config:
      list:
        ...

After

...
  ...
    config:
      list:
        object_actions: {}

Remove unwanted elements

You may notice some form elements that you do not want to display on the form. For example it’s quite possible you have a created_at and updated_at column that belongs to the object model you are working with. These object attributes should not be editable by any app users as they should be controlled by the database engine from which the model files were created. So lets go ahead and remove them so they can’t be directly edited.

vi lib/form/doctrine/<object_name>Form.class.php

Before

public function configure() {
}

After

public function configure() {
    unset($this['created_at'], $this['updated_at']);
}

Boolean Datatypes

If you have any boolean (tinyint(1)) data types in your schema you may want to refer to this post to change them so your forms will display a checkbox and not an input field.

Work in progress…

Symfony /w Doctrine Setup Guide

0

Abstract

A quick cheat guide for setting up symfony (/w doctrine). This assumes you already have your html being served from respective dir.

Setup Symfony Environment

cd /path/to/where/you/want/symfony
wget http://www.symfony-project.org/get/symfony-1.2.9.tgz
tar -xvpf symfony-1.2.9.tgz
ln -s /path/to/symfony/symfony-1.2.9 symfony

Generate  Symfony Project

cd /path/to/html
php /path/to/symfony/data/bin/symfony generate:project <project_name>

Generate Symfony Project App

–escaping-strategy prevents XSS (cross site scripting) attacks, and  –csrf-secret prevents CSRF (Cross Site Request Forgery) attacks. CSRF uses <super_secret_salt> to hash out values on all your form pages as hidden elements so we enforce that any requests made to form actions orgininated from our site, and not someone elses.

php symfony generate:app --escaping-strategy=on --csrf-secret=<super_secret_salt> <app_name>

Enable Doctrine for Symfony

vi config/ProjectConfiguration.class.php
$this->enablePlugins(array('sfDoctrinePlugin'));

OR

$this->enableAllPluginsExcept(array('sfPropelPlugin', 'sfCompat10Plugin'));

Clear Cache

php symfony cc

Publish Assets

This step lets symfony know that you have some plugins you are ready to use.

php symfony plugin:publish-assets

Remove Propel Artifacts

rm web/sfPropelPlugin
rm config/propel.ini config/schema.yml config/databases.yml

Add Doctrine Artifacts

mkdir config/doctrine

Create databases.yml File

php symfony configure:database --name=doctrine --class=sfDoctrineDatabase "mysql:host=<host_name>;dbname=<db_name>" <db_user> <db_password>

Secure databases.yml File

chmod 600 config/databases.yml

Build Schema

Building schema will create a config/doctrine/schema.yml file. It is basically a text version of your database schema. You can edit as you see fit, but for most users it isn’t necessary, but you should at least check it out to see what’s going on.

php symfony doctrine:build-schema
vi config/doctrine/schema.yml

Doctrine Options Configuration (optional)

You can over-ride doctrine model generation options by create a configureDoctrin() method in config/ProjectConfiguration.class.php file. These settings allow you to control elements of the model creation, for example; I dont like creating the extraneous ‘*Tables’ model files so I set ‘generateTableClasses’ to false, like so:

vi config/ProjectConfiguration.class.php
public function configureDoctrine()
{

    $options = array(
        'generateTableClasses' => FALSE
    );

    sfConfig::set('doctrine_model_builder_options', $options);

}

You can find more information and configuration options on the Symfony Website under Doctrine Configuration.

Symfony Model Generation

The ‘model’ refers to the files that allow access to data stored within your database. These files are created automatically based on your config/doctrine/schema.yml file and placed in /lib/model/doctrine/

php symfony doctrine:build-model

Setup Symfony Module

Only use this if you DO NOT PLAN on using Symfony’s Admin Generator, otherwise you may run into issues. In order to start editing actions and template scripts, you need to run the following command in /path/to/project/apps/<app_name>:

php symfony generate:module <app_name> <module_name>

Setup Symfony default css

This will give access to the symfony default css so that web toolbar and default symfony pages look nice.

cd web
ln -s /path/to/symfony/symfony/data/web/sf sf

Enable Login Authentication (optional)

This will protect your app by forcing a user to login to view any app files. You will have to add code to handle login authentication and is covered in my sfGuardDoctrine plugin post.

vi apps/<app_name>/config/security.yml

Before

default:
  is_secure: off

After

default:
  is_secure: on

Allow Access to Dev Controller

If you are developing on an remote environment and you need access to the dev controller (<app>_dev.php),  you will have to add your IP address to <app>_dev.php, as it defaults to localhost.

EOF

If you followed each of the above steps, you have successfully created a symfony project, app, module. You can access your project using the following links:

App: http://yourdomain.com/<app_name>_dev.php (should see success project landing page)

Module: http://yourdomain.com/<app_name>_dev.php/<module_name> (should see success module landing page)

Forms, Actions, etc will be discussed in other setup guides.

Go to Top