Archive for category Symfony

Symfony Live 2010 #sflive2010

On the 16-17th of February the Symfony Live 2010 conference will be held in Paris. A lot promising developers and evangelists will be speakers on this conference like:

  • Fabien Potencier
  • Jonathan Wage
  • Dustin Whittle
  • Stefan Koopmanschap

My colleague and I will attend this conference and, if everything goes well, we will try to update our DevExp blog with live feedback or summarized posts on some of the most interesting sessions.

Tags: ,

How to force symfony colors on windows with PuttyCyg?

Those of you who’re developing with symfony under windows will have noticed that, when running tasks in the command prompt, no colors are used. This is because the windows command prompt isn’t compatible with the color notation.
Most of you also have cygwin installed (shame on you if you didn’t :p). But even if you run the tasks through “PuttyCyg”, which is fully compatible with the color notation, you will not benefit from the colors.

Why?

Read the rest of this entry »

Tags: , , ,

Basic forms with Symfony

SymfonyOur goal is to create a little crud that contains 4 fields

id (auto increment integer)
name (varchar 50)
value (double)
type (0 or 1)

What I will explain is how to change your fields in the form after creation with generation of a module. So we start with a clean module that contains 1 action class and 3 templates (new, edit and index) and 1 partial (form) that is included on the new and edit template.
We created this module not by hand but with the very easy command: symfony propel:generate-module.

Read the rest of this entry »

Tags: , ,

Symfony Form: unsetAllFieldsExcept()

When creating forms in symfony which extends from a Base (ORM) class, you sometimes do not need all fields that Propel/Doctrine generates for you. If you want to remove them you have several ways to do so.

1. Unset the fiels you don’t need.

public function configure() {
  unset($this['field1'], $this['field2'], ... );
}

Simple way to remove the fields you don’t want, but if you change something in your database, which has an impact on that form (new column, foreignkey, etc ), it will be added in the (generated) super class. And if you don’t update the unset function with that extra field, it will be expected in the form. Unless you have unit tests for all your forms this could brake it without knowing it.

2. Override the setup function.

/**
 * @override
 */
public function setup()
  {
    parent::setup();

    $this->setWidgets(...);

    $this->setValidators(...);
  }

Another way is to override the setup function and do your own widgets and validators initialization. This is a good way to only intialize the fields you really need, but you’ll have to do what your ORM generated for you.

3. Use a function which unsets all fields except the one you need (like the enableAllPluginsExcept function).

$this->unsetAllFieldsExcept(array(
      'field1',
      'field2',
      'field3',
      ...));

This would be usefull, because it will remove all fields you do not want to use, and even if new fields are added it will not use them. The drawback is that if you have 20 fields and only need to unset one of them, you’ll have more type work ;) .
Since it does not exist in symfony, I quickly created our own function. This function should be added in the class BaseFormPropel.class:

abstract class BaseFormPropel extends sfFormPropel
{
  public function setup() {
  }

  protected function unsetAllFieldsExcept($fields = array()) {

    $unsetFields = array_diff(array_keys($this->getWidgetSchema()->getFields()), $fields);

    foreach($unsetFields as $value) {
      unset($this[$value]);
    }
  }
}

Which one to use ?
None of them are better. But if you need to reset a majority of the fields the ORM has generated then I would go for the method 2. If you only need to unset the fields you do not want to use then I would use the function unsetAllFieldsExcept().

Which method do/would you use ?

Tags: , ,

sfWidgetFormInputCheckbox unchecked bug

Ever tried to create a Symfony form with a check box input field, which by default should be unchecked ? No ? Well read on because it’s not that easy :) .

This would be the code to print a check box input field in a Symfony form:

class CheckboxForm extends BaseCheckboxForm {
 public function configure() {
 $this->widgetSchema['field'] = new sfWidgetFormInputCheckbox();
 }
}

When you use the form to create something then by default the check box should be unchecked. And when you use this form for editing, then the check box should be checked if the object you try to edit has already been checked in the past.

If you try this code, you’ll see that the check box will always be checked. Even if you display this form to create a new object. Strange no ?

Read the rest of this entry »

Tags: , , ,

A few helpful PHP date methods

For a lot of people dates in PHP are an issue, and the PHP functions aren’t sufficient enough to do all converts you need.
Therefore you find a few easy PHP date conversion methods. Feel free to add your own methods or requests in the comments as we know that these methods don’t cover everything. You can see it for yourself when you read the rest of this entry:

  • get start and end date for the next two months in an array
  • get the datetime for a specific user culture in Symfony
  • get all days between a given start and end date and return it in an array
  • get the difference between two dates

Read the rest of this entry »

Tags: , , , ,

Useful Propel criteria methods and constants

I’m working with criteria quite often lately and must say it is a handy way of query writing. The only problem I have with criteria is that I don’t seem to find a simple overview (list) of the most important methods you can add to it. I don’t really like the Propel website as I don’t find the thing I need in a few seconds and that is a must for a lot of people. If I create a list of the most important criteria methods for myself, I rather share it with you guys…

Simple Select query with 2 criteria to check:

$c = new Criteria();
$c->add(AuthorPeer::FIRST_NAME, "Karl");
$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$authors = AuthorPeer::doSelect($c);
// $authors contains array of Author objects

In SQL this will be:

SELECT ... FROM author WHERE author.FIRST_NAME = 'Karl' AND author.LAST_NAME <> 'Marx';

It’s quite simple to write the criteria, the only thing needed to write them is a list of options.

Read the rest of this entry »

Tags: , , ,

Interesting People – part 1: Fabien Potencier

Fabien PotencierNothing better to get inspiration from then a nice cup of Nespresso in the evening. We are working with Symfony for a few months (years) now and I was wondering earlier this day: Who are the guys that are able to produce such a nice piece of software? You got to admit it that the framework these guys created is a masterpiece, must be a shitload of work and it is still growing on a daily basis.

Off course there is more then one person creating this software, but 1 man in particular is leading this project: Fabien Potencier.

Read the rest of this entry »

Tags: , , ,

Symfony Forms Framework: Merge 2 forms

Recently I had to create a form to create/update users in our system. Some time ago we decided to save are users in 2 tables. The first table would contain all login information and the second his personal information. This is a simple example of the DB design:

User db design

User db design

I would not recommend doing this for so little fields. But in our system we have a lot more fields, and it helps us to optimize our queries.

Wouldn’t it be better if we could merge the 2 forms? The answer is yes. And it’s pretty easy to do so in Symfony … too bad that it’s not documented on the Symfony website.

Read the rest of this entry »

Tags: , , ,

enhance your css

Writing css code isn’t that hard, it’s easy, clean but can be a pain in the ass to keep it readable when you have hundreds or maybe thousands of lines of code.

When sticking to specific rules it can all be made a bit more readable:

  • always use a reset of some sort
  • alphabetize
  • write comment
  • consistency
  • start in the right place
  • Understanding Class and ID
  • Power of li
  • CSS Debugging Tools

read the full text to enhance your css code

5 Ways to Instantly Write Better CSS (click here )
20 Useful CSS Tips (click here)

Tags: , , ,