Archive for October, 2009

Correct jq_button_to_remote complete handler response var

0 Comments

Symfony docs claim that in the ‘complete’ callback you have access to the response as request.responseText.

From svn

* To access the server response, use ‘request.responseText’, to
* find out the HTTP status, use ‘request.status’.

This has not been my experience. When inspecting the variables in scope, I was only able to find, and subsequently access

XMLHttpRequest.responseText;
XMLHttpRequest.status;

Manually set a breakpoint in safari (and in chrome on OSX)

0 Comments

To manually set a breakpoint add the line ‘debugger;’

So if I have some js in an onclick, and I’m trying to look at the scope there, just add ‘debugger;’ to the onclick.


click me

Adding custom fields to the admin generator

0 Comments

The documentation leaves out one thing in regard to adding your own “virtual field” to the admin generator. In addition to everything in the docs, You have to override the toArray method.

The documentation says:

Custom Fields
As a matter of fact, the fields configured in generator.yml don’t even need to correspond to actual columns defined in the schema. If the related class offers a custom getter, it can be used as a field for the list view; if there is a getter and/or a setter, it can also be used in the edit view. For instance, you can extend the BlogArticle model with a getNbComments() method similar to the one in Listing 14-10.

And you need to do those things, but there’s one missing step. The edit page gets the default for the widgets from the toArray() method, which just iterated over the columns. In order to populate your widget for your “virtual column”, you need to override toArray().

The steps in full are:

  • Add the new field to the generator.yml
  • Add a getter and a setter to the model
  • Add a widget and validator
  • Overwrite the toArray() method in the object

Doctrine example (in the model)

public function toArray($deep = true, $prefixKey = false)
    {
        $arr = parent::toArray($deep, $prefixKey);
        $arr['virtual_column'] = $this->getVirtualColumn();
        return $arr;
    }

    public function getVirtualColumn() {
        return 'hellow world';
    }

    public function setVirtualColumn($string) {
        //do something
    }

Customizing Admin Generator Filters in Symfony Using Doctrine

0 Comments

How to extend the existing filters in the symfony doctrine admin generator

Here is our (simplified) schema:

Group: [id, group_name]
User: [username, group_id]
Phonenumber: [user_id, num]

Groups contain many users, and users contain many phone numbers.

The prebuilt admin generator will allow you to filter on any of the existing fields in the table for the object that you are viewing, so if we were to visit /phonenumber/index then we would see all the phonenumbers and be able to filter by user.

However, if we want to filter, for instance, by group, then we need to follow a few steps:

  • Add group_id to phonenumber/config/generator.yml filter list
    config:
        filter:
            display: [user_id, group_id]
    
  • Add widget/validator to PhonenumberFormFilter
    We need to alter the filter form to add the select box for groups, and add the validator so that we can sanitize and get the value back.

    public function configure()
    {
        $this->setWidget('group_id',
                         new sfWidgetFormDoctrineChoice(array('model' => 'Group',
                                                              'add_empty' => true)));
    
        $this->setValidator('group_id',
                            new sfValidatorDoctrineChoice(array('required' => false,
                                                                'model' => 'Group',
                                                                'column' => 'id')));
    }
    
  • Add field to PhonenumberFormFilter
    Just so that the filter knows what to filter on we add this method.

    public function getFields()
      {
          return array_merge((array('group_id' => 'ForeignKey''),
                             parent::getFields());
      }
    
  • Alter query to PhonenumberFormFilter
    This is where we alter the actual query. We are adding a join against the user table, and then a where with a group_id.

    public function addGroupIdColumnQuery(Doctrine_Query $query, $field, $value)
      {
           $query->innerJoin(sprintf('%s.%s', $query->getRootAlias(), 'User u'))
                  ->where('u.group_id = ?', $value);
      }
    
  • This method could certainly have gone in the PhonenumberTable class; in which case it would look like this
    public function addGroupIdColumnQuery(Doctrine_Query $query, $field, $value)
      {
          Doctrine::getTable('Phonenumber')->applyGroupIdFilter($query, $value);
      }