Posts tagged Doctrine

Symfony (Admin Generator) with Doctrine using Checkboxes Pitfall

4

In working with Symfony’s Admin Generator I was having one hell of a time trying to get a boolean column (tinyint(1)) to show up on the form as a checkbox. At first I was trying to over-ride the behavior in the lib/form/doctrine/<object>Form.class.php file, which worked on render but failed on submit.

After asking in #symfony on irc.freenode.net I was informed I needed to change the data type in config/doctrine/schema.yml file from the exact mapping via the mysql data type. Check it out:

vi config/doctrine/schema.yml

Before

Object:
  tableName: someName
  columns:
    some_boolean_column:
      type: integer(1)
      default: '0'
      notnull: true

After

Object:
  tableName: someName
    columns:
      some_boolean_column:
        type: boolean

After this I ran the following:

php symfony doctrine:build-model
php symfony doctrine:build-filters
php symfony doctrine:build-forms
php symfony cc

After that the form rendered and validated as expected.

Doctrine (within Symfony) Timestampable

0

For most tables there is usually a need to track when a row was created or updated. Doctrine allows for this type of behavior but in a different way. If your table has created_at and updated_at columns (or just one or the other) you can tell doctrine to automatically set the correct timestamp. The datatype I use for my created_at and updated_at columns are ‘datetime’ so my schema would look like:

...
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
...

To ensure that your columns are updated you will need to edit your config/doctrine/schemal.yml file and add the following lines:

Before

tableName: objects
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(50)
      notnull: true

After

tableName: objects
actAs:
  Timestampable:
    created:
      name: created_at
      type: timestamp
    updated:
      name: updated_at
      type: timestamp
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(50)
      notnull: true

Now that you edited your schema file you will need to rebuild your model so that the php model files know that you will be updating these columns during saves:

php symfony doctrine:build-model

And as always, clear your cache so that your code will be using the latest version of model files:

php symfony cc

For more information on timestample /w doctrine, visit their website.

Doctrine vs Propel

0

Given 1 table that houses a particular object /w related attributes, say something like car; color, num_of_doors, fuel_type.

Doctrine = only 3 Files

  • Car.class
  • CarTable.class.php
  • base/CarBase.class.php

Propel = 5 Files

  • Car.php
  • CarPeer.php
  • map/CarMapBuilder.php
  • om/CarObject.php
  • om/CarObjectPeer.php

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