Tuesday, October 16, 2012

mb_ucfirst

function mb_ucfirst($string, $encoding)
{
    $strlen = mb_strlen($string, $encoding);
    $firstChar = mb_substr($string, 0, 1, $encoding);
    $then = mb_substr($string, 1, $strlen - 1, $encoding);
    return mb_strtoupper($firstChar, $encoding) . $then;
}

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();
    }
    else
    {
      $this->delete();
      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.

Solution
  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):
    /etc/mysql/my.cnf
    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

    php.ini:
    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();
    $this->form->bind($request[$this->form->getName()]);
    if ($this->form->isValid())
    {
      $this->pager = new sfDoctrinePager('Article');
      $this->pager->setQuery($this->form->getQuery());
      $this->setTemplate('list');
    }
    //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();