Mysql INSERT … ON DUPLICATE KEY UPDATE

If you want to do some data loads in a Mysql DB regardless of insert or update statement you can use the INSERT … ON DUPLICATE KEY UPDATE request.

INSERT INTO test (firstname, lastname, skill)
      VALUES
      ('Alan', 'Cox', 98),
      ('Robert', 'Love', 85),
      ('Linus', 'Torvalds', 24),
      ('Rusty', 'Russell', 79)
ON DUPLICATE KEY UPDATE
     skill = VALUES(skill);

In this example, if the key is composed by firstname and lastname, when a duplicate key is detected, only the skill will be updated

You could find more informations on this statement here

PHP Fatal error: Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

You are using the fanstactic Gregwar/ImageBundle code to manipulate your images on Symfony2 and twig.
But instead of rendering your website, you get this error on your apache error log file

PHP Fatal error:  Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

or directly on your html genered code

<img src="
Fatal error: Method Gregwar\ImageBundle\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

this issue may be a rights problem on your cache drectory. By default, gregwar cache directory is on your web so, launch this command

chmod 777 web/cache

This error may also occur if the file doesn’t exist, for a clean code, add

{% if file_exists('uploads/' ~ site.id ~ '.jpg') %}
    {{ image(site.picture).resize(250, 50) }}
{% endif %}

or on twig code

{% if site.has_picture %}
    {{ image(site.picture).resize(250 ,50) }}
{% endif %}

For more informations about file_exists, see here Symfony2 twig add new function file_exists

You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.

After installing Symfony 2 instead of viewing your website, you see this error message :

You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.

It probably means you need to install the sqlite extension for php

apt-get install php5-sqlite

restart apache and that should to the tricks.

Gestion des droits d’accès dans le backend de Magento

Imaginons qu’on ait une entrée de menu qui ressemble à ceci :

...
        <menu>
            <mon_module translate="title" module="adminhtml">
                <children>
                    <mon_entree>
                        <title>Le titre de mon menu</title>
                        <action>maroute/adminhtml_index</action>
                    </mon_entree>
                </children>
            </mon_module>
        </menu>
...

alors si on veut pouvoir gérer les droits d’accès via le menu permission alors il suffit d’ajouter

...
        <menu>
            <mon_menu translate="title" module="adminhtml">
                <children>
                    <mon_entree>
                        <title>Le titre de mon entrée dans le menu mon_menu</title>
                        <action>maroute/adminhtml_index</action>
                    </mon_entree>
                </children>
            </mon_menu>
        </menu>
        <acl>
            <resources>
                <admin>
                    <children>
                        <mon_menu translate="title" module="mon_module">
                            <title>Le titre du menu qui va apparaître dans les permissions</title>
                            <sort_order>100</sort_order>
                            <children>
                                <mon_entree translate="title" module="mon_module">
                                    <title>Le titre de mon entrée de menu qui va apparaître dans les permissions</title>
                                    <sort_order>10</sort_order>
                                </mon_entree>
                            </children>
                        </mon_menu>
                    </children>
                </admin>
            </resources>
        </acl>...

voilà c tout…

Left join on addAttributeToFilter with EAV tables

When you want to filter a collection with custom attribute value it works but when you want to retrieve the non-corresponding entries (like with the neq, null, not null, …. statements) it doesn’t work.

Default join on collection is an inner join so if you are doing that :

$myCollection()->addAttributeToFilter('my_attribute', array('null' => true));

.. you will find no entries

If you want to change the default inner join to a left join you just have to add ‘left’ as the third parameter of the addAttributeToFilter method :

$myCollection()->addAttributeToFilter('my_attribute', array('null' => true), 'left');

and now it works…

Here is the prototype of the corresponding method in Mage_Catalog_Model_Resource_Product_Collection :

public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')

We can see that inner parameter is used by default

phpMyAdmin change timeout & other customizations

Tired to login again and again on your phpMyAdmin interface ?
You can change the default timeout in your config.inc.php file.

Add or edit this variable

$cfg['LoginCookieValidity'] = 36000; //In seconds 

The config.inc.php file is in your /etc/phpmyadmin/ folder on debian.

Some other options:

$cfg['MaxRows'] = 100; //change the number of line displayed by default
$cfg['AjaxEnable'] = false;  //enable or disable ajax 
$cfg['NavigationBarIconic'] = true;   //enable or diasable navigation icons
$cfg['PropertiesIconic'] = true;      //enable or disable properties icons

Symfony 1.x propel-insert-sql [propel-sql-exec] SQLSTATE[42000]: Syntax error or access violation: 1064

You are using a old version of symfony 1.x on a new version of mysql and when you try to insert the model on your database you get this error :

[propel-sql-exec] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘Type=InnoDB’ at line 6

The problem come from this query

CREATE TABLE `pret_externe_type`
 (
 `id_pret_externe_type` BIGINT(20)  NOT NULL AUTO_INCREMENT,
 `libelle` VARCHAR(100)  NOT NULL,
 PRIMARY KEY (`id_pret_externe_type`)
 )Type=InnoDB

Mysql do not use “Type=” option anymore, you have to replace it by “Engine=”

CREATE TABLE `pret_externe_type`
 (
 `id_pret_externe_type` BIGINT(20)  NOT NULL AUTO_INCREMENT,
 `libelle` VARCHAR(100)  NOT NULL,
 PRIMARY KEY (`id_pret_externe_type`)
 )Engine=InnoDB

To fix it the code, on symfony 1.2 go on this file /lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/sql/mysql/MysqlDDLBuilder.php and change the line 156
from

 $script .= "Type=$mysqlTableType";

to

 $script .= "Engine=$mysqlTableType";

On symfony 1.4, you have to change this file : lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/sql/mysql/MysqlDDLBuilder.php
Always on ligne 156

After code change, rebuild your SQL model and insert it to your databases

php symfony propel-build-sql
php symfony propel-insert-sql