A solution was posted at Symfony nerds in the comments suggesting a hardcoded onclick Ajax call in the radiobutton() method. I suggest using the remote_function() instead to simplify the code, avoid duplication and allow the helper to do the work. After all, that's what helpers are for...
<?php
echo radiobutton_tag(
"tt_product",
1,
true,
'onclick=toolPref(1)'
);
?>
<?php
echo javascript_tag("
function toolPref(val){
".remote_function(array(
'url' => 'user/tallypref',
'with' => "'tally_product='+val"))."
}"
);
?>
| [ 0 trackbacks ] | permalink | related link |




( 3 / 128 )Thought this was worth posting as it could save someone a lot of grief. I had a slave MySQL server stop updating without warning. A SELECT SLAVE STATUS showed there was an error due to an existing record. This would have occurred when a SELECT DATA INFILE populated 50,000 records and things went a little haywaire. The solution was quite simple:
1. STOP SLAVE;
2. TRUNCATE TABLE table_name;
3. START SLAVE;
This wipes out all data in the table with errors and allows the master to rebuild it from scratch. If you have foreign keys you might need to turn them off first...
| [ 0 trackbacks ] | permalink | related link |




( 3 / 113 )Want your web app to send email from your gmail or google apps hosted email account? This way your messages are stored in your sent box. Here's how to do it with a failover to localhost (in case there is a problem with the gmail server or authentication)...
try{
$connection = new Swift_Connection_SMTP(
sfConfig::get("app_gmail_host"),
Swift_Connection_SMTP::PORT_SECURE,
Swift_Connection_SMTP::ENC_TLS
);
$connection->setUsername(sfConfig::get("app_gmail_username"));
$connection->setPassword(sfConfig::get("app_gmail_password"));
$connection->attachAuthenticator(
new Swift_Authenticator_PLAIN()
);
$mailer = new Swift($connection);
$mailer->disconnect();
echo "Using gmail..." . PHP_EOL;
unset($mailer);
} catch (Exception $e) {
echo "Using localhost..." . PHP_EOL;
$connection = new Swift_Connection_NativeMail();
}
//use our working mailer
$mailer = new Swift($connection);
Note you need to disconnect, destroy and recreate your SMTP mailer or else gmail will accuse you of trying to change identity!
| [ 0 trackbacks ] | permalink | related link |




( 3 / 102 )I have a couple of symfony sites that my clients like to test without messing up their data (go figure). By modifying the controller a little I can detect the subdomain from the URL and then switch the syfmony environment accordingly. Then, using a filter, I can put a demo stylesheet in place to make the site look different as well...
By using this technique you can have exact copies of an application without duplicating a single file!
Step 1 - Set up the subdomain
Create a new subdomain in the DNS and configure apache to accept it as an alias for the live site.
<VirtualHost *:80>
ServerName site.com
ServerAlias www.site.com demo.site.com
DocumentRoot "/var/www/"
</VirtualHost>
Step 2 - Create a new database for the subdomain and configure the databases.yml file
test:
propel:
class: sfPropelDatabase
param:
dsn: mysql://root:pass@localhost/live
demo:
propel:
class: sfPropelDatabase
param:
dsn: mysql://root:pass@localhost/demo
Step 3 - Modify the controller
Symfony 1.0:
//check for DEMO site
$env = "live";
$debug = false;
$domain = $_SERVER['HTTP_HOST'];
if (strpos($domain,"demo") !== false){
$env = "demo";
$debug = true;
}
define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/../..'));
define('SF_APP', 'my_app');
define('SF_ENVIRONMENT', $env);
define('SF_DEBUG', $debug);
Or in Symfony 1.2:
$configuration = ProjectConfiguration::getApplicationConfiguration('my_app', $env, $debug);
sfContext::createInstance($configuration)->dispatch();
Step 4 - Create the filter
My favourite bit - save it in your lib folder:
<?php
class demoFilter extends sfFilter
{
public function execute ($filterChain)
{
if (SF_ENVIRONMENT == "demo" && $this->isFirstCall())
{
$this->getContext()->getResponse()->addStylesheet('demo','last');
}
// execute next filter
$filterChain->execute();
}
}
?>
Step 5 - Add your stylesheet & images etc
The above filter adds demo.css to each page, so create the file and any images it requires and upload to your existing css and images folders and you're done.
| [ 0 trackbacks ] | permalink | related link |




( 3 / 106 )I recently set up mySQL replication for a client and thought I'd share this quick and easy way of distributing database load over a few machines.
First, set up replication - it's very simple. Here's a simplified list of tasks based on the info at mysql.com:
http://forums.gentoo.org/viewtopic.php?t=241123
Then you need to adapt your site to use the master server for all write operations and distribute the read load across your slaves (and master if you like).
My setup was moving from a single server to 2 servers. The primary server runs apache and the master database. The secondary server takes all the database read traffic (high CPU low bandwidth) and also serves up media files (low CPU high bandwidth).
The way I set up the config for the databases was to allow for failover from the slave back onto the master. Now this site was built in 2003 and uses some very old PEAR classes (remember PEAR? ;-), but you get the idea:
$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);
$dsn = array(
'phptype' => 'mysql',
'username' => def_user,
'password' => def_pass,
'hostspec' => def_host,
'database' => def_dbname,
);
$db_master =& DB::connect($dsn, $options);
if (PEAR::isError($db_master)) {
showError($db_master->getMessage());
}
$db_master->setFetchMode(DB_FETCHMODE_OBJECT);
//use 2nd database server if available
//otherwise duplicate original as failover
if (defined('def_host2')){
$dsn2 = array(
'phptype' => 'mysql',
'username' => def_user2,
'password' => def_pass2,
'hostspec' => def_host2,
'database' => def_dbname2,
);
$db =& DB::connect($dsn2, $options);
if (PEAR::isError($db)) {
$db = $db_master;
}else{
$db->setFetchMode(DB_FETCHMODE_OBJECT);
}
}else{
$db = $db_master;
}
Then the next step was to modify the pages that included DELETE, UPDATE or INSERT queries to use $db_master instead of $db. The whole process took a little over an hour and the site was only offline for 20 minutes :)
| [ 0 trackbacks ] | permalink | related link |




( 3 / 140 )
How to add a Symfony command to PhpEd:
1. Menu: Tools->Settings
2. Go to Tools -> Integration
3. Click "Add Menu"
4. Enter "Symfony cc" in menu name
5. Select "Shell" from Execute with list
6. Enter php @ProjRoot@/symfony cc in command line
7. Select checkbox next to "Show this command in Workspace" group
8. Deselect "for files" checkbox
9. Select "for directories and projects"
Save and then try right-clicking on a project to run your command from the context menu.
You can also direct the output of the command line back into PhpEd's log panel - this is a good way to ensure your CLI commands are executing without errors - as the windows command prompt has a tendency to flash and disappear before your eyes...
| [ 0 trackbacks ] | permalink | related link |




( 3 / 194 )This has come in handy for a couple of mail queues I have built and thought it was worth sharing. Basically it's a small check for PHP in the running system processes. If *any* PHP script is running (besides itself) we abort the script. It would be nice to know *which* script was running but I haven't managed to do that yet :)
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
//windows, can't do thread detection
$threads = 1;
}else{
//check for PHP PID and abort
$pids = preg_split('/\s+/', `ps -o pid --no-heading -C php`);
$threads = 0;
foreach($pids as $pid) {
if(is_numeric($pid)) {
$threads++;
echo "Process " . $pid." found\r\n";
}
}
}| [ 0 trackbacks ] | permalink | related link |




( 3 / 162 )Sometimes I wonder about using mime types to validate file types. Usually using file extensions is more reliable seeing as though eah browser uses its own set of mime types fore each file extension anyway.
So here I am again modifying some core Symfony files to fix browser mime type inconsistencies, this time it's IE7 playing up. I mean honestly what's with image/x-png as a mime type?
But if you want to generate sfThumbnails you'll need to modify the sfGDAdapter class:
/**
* List of accepted image types based on MIME
* descriptions that this adapter supports
*/
protected $imgTypes = array(
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/gif',
);
/**
* Stores function names for each image type
*/
protected $imgLoaders = array(
'image/jpeg' => 'imagecreatefromjpeg',
'image/pjpeg' => 'imagecreatefromjpeg',
'image/png' => 'imagecreatefrompng',
'image/x-png' => 'imagecreatefrompng',
'image/gif' => 'imagecreatefromgif',
);
/**
* Stores function names for each image type
*/
protected $imgCreators = array(
'image/jpeg' => 'imagejpeg',
'image/pjpeg' => 'imagejpeg',
'image/png' => 'imagepng',
'image/x-png' => 'imagepng',
'image/gif' => 'imagegif',
);
| [ 0 trackbacks ] | permalink | related link |




( 3 / 131 )It's rare that I feel the need to edit core symfony files, but this is one situation where it is the best choice. Symfony has a database of mime types it references when processing file uploads. It is pretty comprehensive, but the entries for ZIP files are:
application/x-zip-compressed
application/zip
To get Firefox uploads to work you need to add this:
application/x-zip
You can do this by adding it to the mime type array on the fly:
$mimeTypes['application/x-zip'] = 'zip';
Or even better, update the mime_types.dat file by adding this:
s:17:"application/x-zip";s:3:"zip";
And don't forget to increment the array count at the beginning of the file:
a:406:
| [ 0 trackbacks ] | permalink | related link |




( 3 / 134 )AT first I thought I had found a bug in the way symfony decorates templates with layouts. But as usual, I discovered a bug in my own code :) Hopefully this will save someone else from having to debug a situation which turned out to be issue with using PHP's output buffer within a symfony action.
The problem: One of my actions was being rendered to the browser with the layout code (ie: html, head and body tags etc) in the middle of my template.
After debugging I discovered that symfony uses the output buffer to render templates, so if you mess with the buffer then you mess with the way your template gets rendered.
The renderFile() method if sfPHPView.class reads templates like this:
ob_start();
ob_implicit_flush(0);
require($_sfFile);
return ob_get_clean();
I had a utility function that was using the buffer...
ob_start();
//do stuff with the buffer
What I didn't realise was that Symfony had already called ob_start(), so my ob_start() was erasing the buffer and only sending output from that point onwards back to the render function.
The solution: If you ever need to capture the output buffer in Symfony, make sure you save the existing buffer and then render it back out when you are done.
My final code looked like this:
$buffer = ob_get_clean();
ob_start();
//do stuff with the buffer
ob_clean();
echo $buffer;
OK it's pretty simple, but diagnosing it was for from simple!
| [ 0 trackbacks ] | permalink | related link |




( 3 / 139 )Back Next



Avatar



