Category Archives: Symfony

Symfony related.

Debugging doctrine:data-load

I know it drives you MAD when you try to load a bunch of features and get an error like:

1
SQLSTATE[23000]: Integrity CONSTRAINT violation: 1062 Duplicate entry '16' FOR KEY 'PRIMARY'

Luckily there’s an undocumented argument on doctrine’s data-load task:

–trace

1
2
3
4
5
6
7
8
9
10
$ php symfony doctrine:data-load --trace

>> doctrine Loading data fixtures from "/home/bla/public_html/site/data/fixtures"
>> doctrine Loading data fixtures from "/home/bla/public_html/site/plugins/sfDoctrineGuardPlugin/data/fixtures"
>> Doctrine_Connection_Mysql exec : SET NAMES 'UTF8' - ()
>> Doctrine_Connection_Mysql exec : DELETE FROM table_a - ()
>> Doctrine_Connection_Mysql exec : DELETE FROM table_b - ()
>> Doctrine_Connection_Mysql exec : DELETE FROM sf_guard_user - ()
>> Doctrine_Connection_Statement execute : INSERT INTO sf_guard_user (algorithm, is_active, is_super_admin, first_name, last_name, email_address, username, salt, password, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - (sha1, 1, 1, bla, blah, blah@adclick.pt, blah, blah48ca1e4247258a0ec29f9d1aac8, blah32ed46c3a74deebb071456114e4406eba42e, 2012-01-12 18:07:02, 2012-01-12 18:07:02)
...

Symfony Ajax Json response without view

1
2
3
4
5
6
7
8
9
10
11
12
13
class moduleActions extends sfActions
{

    public function executeAjax(sfWebRequest $request)
    {
        $arr = array();
        // ... build the array
       
        $res = json_encode($arr);
        $this->getResponse()->setHttpHeader('Content-type', 'application/json'); /* the trick: set proper header so javascript can read the json */
        return $this->renderText($res); /* this will render the text with no need for a specific view */
    }
}

Apc para o Doctrine em projectos symfony

Em projectos symfony 1.*, pode ser configurado o uso da cache Apc para o Doctrine, para tal, em config/ProjectConfiguration.class.php, criar o método:

public function configureDoctrine(Doctrine_Manager $manager)
{
$manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, new Doctrine_Cache_Apc(array(‘prefix’ => ‘PREFIXO’)));
}

É importante definir um prefixo, já que tendo vários projectos na máquina, usando a mesma chave, gera conflito.

Force filter on Admin Generator

Create a new filter on lib/filters/yourFilter.class.php:

class yourFilter extends sfFilter
{

    public function execute($filterChain)
    {
        $context = $this->context; /* @var $context sfContext */
        $request = $context->getRequest(); /* @var $request sfWebRequest */
        $user = $context->getUser(); /* @var $user sfUser */
        
        $module = $context->getModuleName();
        $new_filter = array($module . '.filters' => array('now_filter' => array('text' => 'new filter forced value')));
        $user->setAttribute($module . '.filters', $new_filter, 'admin_module');
        $filterChain->execute();

    }

}

Don’t forget to load the filter on app/module/config/filters.yml:

...
your_filter:
  class: yourFilter

Doctrine use same collation on all tables (even plugins)

It really annoys me when I set my schema collate to utf8_general_ci but plugin tables are created as latin1_swedish_ci.

Solution?

Create the following method on your ProjectConfiguration.class.php

1
2
3
4
5
6
7
8
9
class ProjectConfiguration extends sfProjectConfiguration
{
    public function configureDoctrine(Doctrine_Manager $manager)
    {
        $manager->setCollate('utf8_general_ci');
        $manager->setCharset('utf8');

    }
}

Props to: http://www.prettyscripts.com/framework/symfony/symfony-and-doctrine-default-table-collation-a-better-solution

Doctrine table inheritance and migrations bug

There seems to be a bug while using migration with table inheritance under symfony 1.4.

Every change I make on schema.yml in a model with column_aggregation table inheritance is not identified on doctrine doctrine:generate-migrations-diff task. Even though the models are properly re-build, I cannot map the changes on the migrations classes…

Word of advice: If you need to use migrations do not use doctrine’s table inheritance.

If anyone has something to say on this topic please do so!

Como alterar filtro default do symfony

Por defeito, o symfony cria para os campos de texto os filtros com comparação do tipo LIKE ‘%texto%’. Se quisermos fazer override num caso específico para que a comparação seja feito com = temos que criar um método na classe xyzFormFilter.class.php da seguinte forma (neste caso de ser um campo de email):

1
2
3
4
5
6
7
8
9
public function addEmailColumnCriteria(Criteria $criteria, $field, $values)
    {
        $value = array_pop($values);
        if ("" != $value)
        {
            /* @var $criteria Criteria */
            $criteria->add(ContactPeer::EMAIL, $value, Criteria::EQUAL);
        }
    }

et voilá!