A typical Developer Blog
by Gordon Franke
Icon

What brings us Symfony2 2.2

Symfony2 version 2.2 Beta 2 is out so let us look what will it brings us.

Read the rest of this entry »

How can i override user attribute in my functional test?

Some times your code depend on a user attribute, so you want set the user attribute in your functional test.

The Problem is that the doCall method in the sfBrowser class rebuild the context and in the end calls theshutdown method from user and storage object to write the session data.

So you must create your own Browser class and override the doCall function and simple move the shutdown calls to the beginning.

Browser Class:

class myBrowser extends sfBrowser
{
  /**
   * Calls a request to a uri.
   */
  protected function doCall()
  {
    // manually shutdown user to save current session data
    if (isset($this->context) AND $this->context->getUser())
    {
      $this->context->getUser()->shutdown();
      $this->context->getStorage()->shutdown();
    }
 
    // recycle our context object
    $this->context = $this->getContext(true);
 
    sfConfig::set('sf_test', true);
 
    // we register a fake rendering filter
    sfConfig::set('sf_rendering_filter', array('sfFakeRenderingFilter', null));
 
    $this->resetCurrentException();
 
    // dispatch our request
    ob_start();
    $this->context->getController()->dispatch();
    $retval = ob_get_clean();
 
    // append retval to the response content
    $this->context->getResponse()->setContent($retval);
  }
}

Test code:

include dirname(__FILE__).'/../../bootstrap/functional.php';
 
$browser = new sfTestFunctional(new myBrowser());
 
$browser->
  get('/site-one');
 
// override the session data
$browser->getUser()->setAttribute('key', $value);
 
$browser->
  get('/site-one');

Symfony and doctrine compiled version

In a new symfony project i have tested the doctrine compiled version. since 1.3.7 symfony support this.

http://www.symfony-project.org/blog/2010/09/22/symfony-1-3-7-1-4-7

I have learned two things

first when you compile doctrine use the –driver option. with mysql you can save ~15% file size.

Second use Doctrine_Core not Doctrine. The class Doctrine is deprecated and not in the compiled file. So you get a fatal error.
To fix the class name automatically see:
Change from Doctrine to Doctrine_Core with one command

Simple script to find missing translation strings

How can i use field credentials for symfony admin generator forms?

When you would restrict the display of a field or fieldset group on user credential. you must do the following

1. open the action class and add

public function preExecute()
{
  parent::preExecute();
 
  $this->configuration->setUser($this->getUser());
}

2. open the *GeneratorConfiguration class and add

protected $user;
 
public function setUser($user)
{
  $this->user = $user;
}
 
public function getUser()
{
  return $this->user;
}
 
public function getEditDisplay()
{
  $fieldsets = parent::getEditDisplay();
  if (!$this->getUser()->hasCredential('admin'))
  {
    unset(
      $fieldsets['Auth'][2], // field
      $fieldsets['Rights']   // field group
    );
  }
 
  return $fieldsets;
}
 
public function getNewDisplay()
{
  ... // see getEditDisplay
}
 
public function getFormOptions()
{
  return array('user' => $this->getUser());
}

3. last remove the fields from the form, open the *Form class and add

public function configure()
{
  ...
  if (!$this->getOption('user')->hasCredential('admin'))
  {
    unset($this['is_super_admin'], $this['groups_list'], $this['permissions_list']);
  }
  ...
}

How can i automatic add ids to my trans-unit nodes in my XML Xliff Tanslation files?

When you validate you XML Xliff file you have often the problem that a trans-unit node has no or a already existing id. So i have wrote a small script for that problem.

fix_id.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
 
$doc = new DOMDocument();
$doc->load($argv[1]);
 
$xpath = new DOMXPath($doc);
 
$query = '//trans-unit';
 
$entries = $xpath->query($query);
foreach ($entries as $i => $entry) {
    $entry->setAttribute('id', $i+1);
}
 
$doc->save($argv[1]);

Execute the following command in your symfony1 root directory ;)

for FILE in `find apps/*/i18n -name *\.xml`; do php fix_id.php $FILE; done;

GTUG Battle Hackathon

The event was at google here in munich. On which project we actual work would anounced at 15. march. It would be a chrome plugin ;)

Google Battle Hackathon 2010

Google Battle Hackathon 2010

Where can i get “Pro Git” book as pdf?

First make step get the git repository

git clone git://github.com/progit/progit.git

when you use ubuntu you must install some packages

sudo aptitude install ruby pandoc texlive-xetex texlive-latex-recommended

then build the pdf in your preferred language. run this command from the latex folder

ruby makepdf en

How can i simple mark all required form fields in symfony 1.3?

Create a function in your BaseForm class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static function listenToPostConfigure($event)
{
  $form            = $event->getSubject();
  $widgetSchema    = $form->getWidgetSchema();
  $validatorSchema = $form->getValidatorSchema();
 
  $fields = $form->getFormFieldSchema()->getWidget()->getFields();
  foreach ($fields as $key => $object)
  {
    $label = $form->getFormFieldSchema()->offsetGet($key)->renderLabelName();
    if (isset($validatorSchema[$key]) and $validatorSchema[$key]->getOption('required') == true)
    {
      $title  = $key . '_field_is_required';
      $label .= '<sup>getFormFormatter()->translate($title) . '">*</sup>';
    }
    $widgetSchema->setLabel($key, $label);
  }
}

Add a listener to the form.post_configure event with your function

1
2
$dispatcher = $this->getEventDispatcher();
$dispatcher->connect('form.post_configure', array('BaseForm', 'listenToPostConfigure'));

How can i change the netbeans color schema to look like symfony documentations?

Simple extract this file into your netbeans-6.8/nb6.8 folder:
SymfonyColorScheme.zip

Thanks Ze Technology for the color schema

http://www.ze-technology.com/2009/12/11/netbeans-aux-couleurs-de-symfony/