Symfony, performance, and High CPU usage

Server is slowing down, and noticing that your Symfony project is consuming enormous amount of CPU? Try this tweak: # factories.yml all: routing: class: sfPatternRouting param: cache: null What it does: This config disables the Symfony Routing Cache, which uses PHP serialize/unserialize functions, and can cause lots of CPU processing. In my case, a high server load of ~8 went under 1 with this simple tweak (on a dual core). Easy change, but hard to find in the docs.
Read more...

How to backup and restore your data with MysqlDump

Simple backup: From one database, to a simple sql file. mysqldump -u [username] -p [password] [databasename] > [backupfile.sql] Backup specific tables One database, several specified tables, to a single sql file. mysqldump -u [username] -p [password] [databasename] [table1 table2 ....] > [backupfile.sql] Backup multiple databases Multiple databases, to a single sql file. mysqldump -u [username] -p [password] --databases [database1 database2...] > [backupfile.sql] Backup all databases All databases found in connection, to a single file.
Read more...

Symfony directory’ variables

For the times you’ll need access to some of Symfony’s directories: Symfony dir [php] // root directory structure 'sf_cache_dir_name' = 'cache', 'sf_log_dir_name' = 'log', 'sf_lib_dir_name' = 'lib', 'sf_model_dir_name' = 'model', 'sf_web_dir_name' = 'web', 'sf_data_dir_name' = 'data', 'sf_config_dir_name' = 'config', 'sf_apps_dir_name' = 'apps', // global directory structure 'sf_app_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$sf_app, 'sf_model_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'model', 'sf_lib_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'lib', 'sf_web_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'web', 'sf_upload_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'web'.DIRECTORY_SEPARATOR.'uploads', 'sf_base_cache_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app, 'sf_cache_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$sf_app.DIRECTORY_SEPARATOR.$sf_environment, 'sf_log_dir' = $sf_root_dir.DIRECTORY_SEPARATOR.'log', 'sf_data_dir' = $sf_root_dir.
Read more...

Get MySQL’s table sizes

Want to check your database size?

SELECT table_schema "Database",
 sum( data_length + index_length ) / 1024 / 1024 "Size (MB)",
 sum( data_free )/ 1024 / 1024 "Free (MB)"
FROM information_schema.TABLES
GROUP BY table_schema ;

This will work in MySQL 5.0.2 and up. For older versions, use:

SHOW TABLE STATUS

Retrieve custom columns in Symfony

Sometimes while retrieving data, you might not need all the columns, especially on a large database query result set.

Symfony, or actually Propel (1.3) allows you to chose what columns to retrieve, and skip the hydrate process:

// Custom column, on large subset
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(TitlePeer::ID);
$c->addSelectColumn(TitlePeer::TITLE);
$resTitleRS = TitlePeer::doSelectStmt($c);
// Loop through result and create custom array
while ($row = $resTitleRS->fetch(PDO::FETCH_ASSOC)) {
  $title_id = $row['ID'];
  $title_name = $row['TITLE'];
  $arrTitleDB[$title_id] = $title_name;
}

Get the time duration from seconds

1234567891011121314151617181920212223242526 <?php /** * @param integer secs Number of seconds to convert * @return integer Time in weeks, days, hours, minutes, seconds * */ function duration($secs) { $vals = array( 'w' => (int) ($secs / 86400 / 7), 'd' => $secs / 86400 % 7, 'h' => $secs / 3600 % 24, 'm' => $secs / 60 % 60, 's' => $secs % 60 ); $ret = array(); $added = false; foreach ($vals as $key => $timeSC) { if ($timeSC > 0 || $added) { $added = true; $ret[] = $timeSC .
Read more...

Clean multiple new lines in a text

/**
 * Clean duplicates new line
 *
 */
  function cleanNewLine($text)  {
    $newLine = "\r\n";
    $strRes = '';

    $posNewLine = strpos($text, $newLine);
    if ($posNewLine===false) {
      $strRes = $text;

    } else {
      $startText = substr($text, 0, $posNewLine+2);
      $endText = substr($text, $posNewLine+2);
      $strRes .= $startText."";

      // Remove duplicate new line if exists
      $posNewLine = strpos($endText, $newLine);
      while($posNewLine===0) {
        $endText = substr($endText, $posNewLine+2);
        $posNewLine = strpos($endText, $newLine);
      }

      $strRes .= self::cleanNewLine($endText);
    }

    return $strRes;
  }

Split long words in a text

Recursive php function, to split long words:

/**
 * Cut words to max char, in a text
 *
 * @param    string text        Text to check
 * @param    integer maxChar    Max word length
 * @return   string             Verified text
 */
function checkWordLength($text, $maxChar=50) {

    $arrText = explode(' ', $text);
    foreach ($arrText as $key=>$t) {
      if (strlen($t)>$maxChar) {
        $arrText[$key] = chunk_split($t, $maxChar, ' ');
      }
    }

    return implode(' ', $arrText);
}

Use:

$text = checkWordLength($text);