Tuesday, September 25, 2012

Do Not Save Null Values in Embedded Form in Symfony

Think there is a Product object with many ProductPrice objects. The Product form contains some ProductPrice forms as embed forms. Prices are not mandatory fields. Now if you save the form, there will be null price rows in the ProductPrice table. Do the following in the ProductPrice model(!) class!
  public function save(Doctrine_Connection $conn = null)
    if($this->price > 0)
      return parent::save();
      return null;
This will check the price (there is a price field in the ProductPrice table) is greater than 0 or not. If greater, it will save to the database. If not, it will delete the row in the table (if presents) and stop the saving process for this price object.

Doctrine: SQLSTATE[23000]: Integrity constraint violation duplicate entry

If you are using a table where some fields are I18N and ex. the title field is sluggable, you will realize that you cannot update(!) the object with the same title. See an example:

The page is multilingual (Hungarian and English). You create a product with TEFAL XY Hungarian and TEFAL XY English tilte (the same title used in each language). You can saved the form / object, it created successfully. Now you change the Hungarian title to TEFAL X. Save. Then change the English title to TEFAL X, too. You will get an error:

Doctrine: SQLSTATE[23000]: Integrity constraint violation duplicate entry

This is a bug in doctrine 1.2.x.

  1. Copy lib / vendor / symfony / lib / plugins / sfDoctrinePlugin / lib / vendor / doctrine / Doctrine / Template / Listener / Sluggable.php inside your own lib directory.
  2. In the getUniqueSlug method, change this line:
    $whereString .= ' AND r.' . implode(' != ? AND r.', $table->getIdentifierColumnNames()) . ' != ?';
    to this:
    $whereString .= ' AND (r.' . implode(' != ? OR r.', $table->getIdentifierColumnNames()) . ' != ?)';
  3. Save. Clear cache. Be happy.

Monday, September 24, 2012

Hide Errors at Top of the Embed Form in Symfony

If you use embed forms, you will realise that the validatation errors appear twice, once near the input fields (in the embed form) and once at top of the embed form. To solve this....

I'm using the jroller plugin to generate auto admin:

plugins / sfAdminThemejRoller / data / generator / sfDoctrineModule / jroller / template / templates / _form_field.php

Use this (my solution):

    [?php if ($form[$name]->hasError() && get_class($form[$name]) != 'sfFormFieldSchema'): ?]
        [?php echo $form[$name]->renderError() ?]
    [?php endif; ?]

Thursday, September 20, 2012

MySQL Tuning

  1. Optimize tables (to correct overheads)
  2. Set query cache size bigger if Qcache_lowmem_prunes (on the runtime information page) is red.
Useful commands:

  • Show all query cache settings:
    SHOW VARIABLES LIKE  '%query_cache%';
  • Set a bigger query cache size (256 MB) ON RUNTIME:
    SET GLOBAL query_cache_size = 256000000;
  • Set a bigger query cache size in config (will work after mysql restart):
    Search for "query_cache_size" then change...
    query_cache_size = 256M

Saturday, September 15, 2012

Install Symfony 2 on Debian

  1. Download the Standard version and put the Symfony folder to the root.
  2. Open /Symfony/web/config.php with browser to check the configuration. If needed, remove the ip address check from the begining of the file. I had to install things and do some changes:

    apt-get update
    apt-get install php-pear php5-dev apache2-threaded-dev build-essential libicu-dev
    pecl install apc
    pecl install intl

    short_open_tag = Off
    date.timezone = Europe/Budapest (for me)
    extension = apc.so
    extension = intl.so

    Save then restart apache.

Friday, September 14, 2012

Filter Data On Frontend with Symfony Form Filter

Source: http://stackoverflow.com/questions/3765495/symfony-how-to-filter-data-on-the-frontend-like-in-the-backend

If you want to do it exactly like it is done on the backend, you can use the admin generator on frontend applications. A more general and customizable way would be to simply create list and filter actions and use Symfony's form filters. Here's a basic example for a model class "Article":

In an actions class:
class articleActions extends sfActions
  public function executeList(sfWebRequest $request)
    $this->form = new ArticleFormFilter();
    $this->pager = new sfDoctrinePager('Article');

  public function executeFilter(sfWebRequest $request)
    $this->form = new ArticleFormFilter();
    if ($this->form->isValid())
      $this->pager = new sfDoctrinePager('Article');
    //handle invalid form here
In view, iterate throw pager like this:
foreach($pager->getResults() as $article)
Doctrine FormFilter's are fairly similar to Doctrine forms. Get started by configuring the form inside of FormFilter::configure();

ob_start: PHP Output To Variable / String

<?php ob_start(); ?>
Hello world, <?php echo "My Website"; ?>
<?php include('something.php') ?>
Thank you.
<?php $var = ob_get_clean(); ?>

Friday, September 7, 2012

The user specified as a definer ('root'@'%') does not exist

mysql -u root -p
grant all privileges on *.* to `root`@`%` identified by 'password';
flush privileges;

Install Apache 2, PHP 5, MySQL 5 on Debian

apt-get update

apt-get install apache2
apt-get install php5
apt-get install libapache2-mod-php5
apt-get install mysql-server mysql-client mysql-common php5-mysql
apt-get install php5-xsl php5-gd php5-curl

Install the Latest APC on Debian
Install PHPMYADMIN on Debian

Install PHPMYADMIN on Debian

1. Download the latest PHPMYADMIN.
2. Put here: /usr/share/phpmyadmin
3. Create phpmyadmin.conf in /etc/apache2/conf.d :
Alias /phpmyadmin /usr/share/phpmyadmin

        Options Indexes FollowSymLinks
        DirectoryIndex index.php

4. /etc/init.d/apache2 restart

Thursday, September 6, 2012

How to Save Empty Form Fields as NULL in Symfony

If you leave a field empty in your form, Symfony will save it as a blank value. However sometimes you want to save it as NULL...

Create a method for the field in the form class:
  public function updateManufacturerColumn($value)
    return (!$value) ? null : $value;
Manufacturer is the name of the field now.

Post Validator in Symfony

  public function configure()
    $this->getValidator('day')->setOption('required', 'true');
    $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
      new sfValidatorCallback(array('callback' => array($this, 'checkFields'))),
  public function checkFields($validator, $values)
    if(empty($values['manufacturer']) && empty($values['category_apollo_kod']) && empty($values['product_apollo_kod']))
      throw new sfValidatorErrorSchema($validator, array('manufacturer' => new sfValidatorError($validator, 'custom error')));
    return $values;