Saturday, January 29, 2011

Create Object with More Doctrine Translation

$country = new Country();

// set a language-independent value
$country->setCountryCode('H');

// set translations
$country->Translation['hu']->name = 'Magyarország';
$country->Translation['en']->name = 'Hungary';
$country->Translation['de']->name = 'Ungarn';

$country->save();

Thursday, January 27, 2011

Truncate Table with Doctrine

$doctrine = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$doctrine->query('TRUNCATE TABLE tableName');
unset($doctrine);

Sunday, January 23, 2011

Work with Doctrine Migration in Symfony

To update your schema, classes, database without losing data, try to do this...
  1. Change your schema.yml file.
  2. Use the symfony doctrine:generate-migrations-diff command to generate the differences between the current classes and the changed schema. These changes are converted by Doctrine into a migrations files, you can find them in the lib/migration directory. In the filename you will see a version number.
  3. Type the symfony doctrine:migrate x command to update your database. x is the version number you found in the filename of the last migration file.
  4. Type the symfony doctrine:build --all-classes command to generate new classes for symfony.
Symfony will create a migration_version table in the database to store the current migration version number.

Saturday, January 22, 2011

Remove Dotted Outline On Links and Buttons

button::-moz-focus-inner,
input[type="reset"]::-moz-focus-inner,
input[type="button"]::-moz-focus-inner,
input[type="submit"]::-moz-focus-inner,
input[type="file"] > input[type="button"]::-moz-focus-inner {
    border: 1px dotted transparent;
}
* {
    outline: none;
}

Saturday, January 15, 2011

Remove 'is empty' checkbox from Filter Form in Symfony

Use the following line in the filter form's setup or configure function:
$this->getWidget('name')->setOption('with_empty', false);

Adding Config Parameters to sfWidgetFormCKEditor

$this->setWidget('content', new sfWidgetFormCKEditor(array('jsoptions' => array('toolbar' => 'Basic', 'width' => '100%', 'height' => '280px'))));

Friday, January 7, 2011

Allow Extra Fields in Symfony Forms

$this->validatorSchema->setOption('allow_extra_fields', true);

Tuesday, January 4, 2011

Sort By Foreign Key or Custom Column in Symfony Admin Generator

This solution is works with Doctrine ORM.
  1. Put this to the backend module's actions.class.php
    protected function addSortQuery($query)
      {
        $rootAlias = $query->getRootAlias();
        if (array(null, null) == ($sort = $this->getSort()))
        {
          return;
        }
        $s = $sort[0];
        $fields = $this->varHolder->get('configuration')->getFieldsDefault();
        if ($fields != null)
        {
          $field = $fields[$s];
          if ($field != null)
          {
            if (isset($field['sortBy']))
            {
              $criterion = $field['sortBy'];
              if ($criterion != null)
              {
                $s = $criterion;
              }
            }
          }
        }
        
        if (isset($field['noRootAlias']) && $field['noRootAlias'] == true)
        {
          $query->addOrderBy($s . ' ' . $sort[1]);
        }
        else
        {
          $query->addOrderBy($rootAlias.'.'.$s . ' ' . $sort[1]);
        }
      }
    
  2. Let's see first the foreign key sorting with an example. If you want to show the related object's name instead of its id, you must use the relation name (in this case 'Group'). To enable sorting, add the sortBy parameter to the field.
    config:
          ...
          fields:
            name:    { label: Name }
            Group:   { label: Group, sortBy: Group.name }  
          list:
            title:   Users
            display: [id, name, Group]
    
    That's all, the sorting now works, but the column's name is not clickable yet. You have to change the generator's template. As I use the sfAdminThemejRollerPlugin, I will do this with that.
  3. Open the \ plugins \ sfAdminThemejRollerPlugin \ data \ generator \ sfDoctrineModule \ jroller \ template \ templates \ _list_th_tabular.php file and change the 4th row from this:
    <?php if ($field->isReal()): ?>
    to this:
    <?php if ($field->isReal() || $field->getConfig('sortBy')): ?>
    Now the foreign key sorting must work.
  4. Let's see the custom column sorting with an example. The data you will want to see in the custom column is somewhere in the database. You have to add your custom column to the original sql statement. Use joins, relations or simply subqueries. You can overwrite the sql in backend module's actions.class.php:
    protected function buildQuery()
      {
         $query = parent::buildQuery();
         // do what ever you like with the query    
         // use the die($query->getSqlQuery()); row to see the original sql statement
         return $query->addSelect('*, (SELECT foo FROM bar) as foobarcolumn');
      }
    As you can see I didn't change the query output, I only add an extra column to the query.
  5. Now we have to add this column to the generator.yml...
    config:
          ...
          fields:
            name:    { label: Name }
            Group:   { label: Group, sortBy: Group.name }
            foobar:  { label: FooBar, sortBy: foobarcolumn, noRootAlias: true }
          list:
            title:   Users
            display: [id, name, Group, foobar]
    
I hope this helps. The noRootAlias param is created by me, perhaps you won't need it or you can avoid using it. If you have an easier/better solution, please tell me!

Monday, January 3, 2011

Enable Frontend Logging in Production Environment in Symfony

  1. Enable logging in frontend/config/settings.yml.
    logging_enabled: true
  2. Change the code below in the prod section of the factories.yml:
      logger:
        class:   sfNoLogger
        param:
          level:   err
          loggers: ~
    
    with this:
      logger:
        class: sfAggregateLogger
        param:
          level: debug
          loggers:
            sf_web_debug:
              class: sfWebDebugLogger
              param:
                level: debug
                condition:       %SF_WEB_DEBUG%
                xdebug_logging:  true
                web_debug_class: sfWebDebug
            sf_file_debug:
              class: sfFileLogger
              param:
                level: debug
                file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log